๐Ÿงฎ Brain Teaser

The Optimistic Scientist: Why Cross-Validation Beats Training Error, and When It Fails

You fit a model with pp parameters to nn training points using ordinary least squares. The training MSE is:

err^train=1nโˆ‘i=1n(yiโˆ’f^(xi))2\widehat{\text{err}}_{\text{train}} = \frac{1}{n}\sum_{i=1}^n (y_i - \hat{f}(x_i))^2

and the true (expected) test MSE on a fresh draw is Err\text{Err}.

(a) Show that E[err^train]โ‰คE[Err]\mathbb{E}[\widehat{\text{err}}_{\text{train}}] \leq \mathbb{E}[\text{Err}], i.e., training error is systematically optimistic. Give a clean formula for the gap.

(b) Now suppose p=np = n (you fit as many parameters as data points). What happens to err^train\widehat{\text{err}}_{\text{train}}? What does this mean for using training error as a model selection criterion?

(c) Leave-one-out cross-validation (LOO-CV) is proposed as a fix. For linear smoothers y^=Hy\hat{y} = Hy (where HH is the hat matrix), LOO-CV has a remarkable shortcut formula:

CV(n)=1nโˆ‘i=1n(yiโˆ’y^i1โˆ’Hii)2\text{CV}_{(n)} = \frac{1}{n}\sum_{i=1}^n \left(\frac{y_i - \hat{y}_i}{1 - H_{ii}}\right)^2

Explain intuitively why the factor (1โˆ’Hii)(1 - H_{ii}) appears in the denominator. What does HiiH_{ii} measure, and why does a large HiiH_{ii} make the correction large?

optimismcross-validationhat matrixleveragebiasgeneralizationOLS

Answer: Optimism of Training Error and the LOO Shortcut

Key Idea / Intuition

Training error is optimistic because the model was tuned on the training data โ€” it gets to "see the answers" before being tested. The gap between training and test error is precisely the in-sample optimism, which grows with model complexity (degrees of freedom used). When p=np = n, OLS interpolates perfectly and training error collapses to zero โ€” a complete breakdown of it as a quality measure. LOO-CV corrects for this by simulating a held-out test, and the hat-matrix shortcut works because removing one point and refitting is equivalent (for linear smoothers) to rescaling the residual by how much that point influenced its own fitted value.


Formal Proof / Solution

Part (a): Training error is optimistically biased

For a linear model with pp parameters fit by OLS, the fitted values are y^=Hy\hat{y} = Hy where H=X(XTX)โˆ’1XTH = X(X^TX)^{-1}X^T has trace tr(H)=p\text{tr}(H) = p.

The optimism of training error is defined as: op=Errโˆ’err^train\text{op} = \text{Err} - \widehat{\text{err}}_{\text{train}}

A classical result (ESL ยง7.4) gives: E[op]=2nโˆ‘i=1nCov(y^i,yi)\mathbb{E}[\text{op}] = \frac{2}{n} \sum_{i=1}^n \text{Cov}(\hat{y}_i, y_i)

For OLS with noise variance ฯƒ2\sigma^2: Cov(y^i,yi)=ฯƒ2Hii\text{Cov}(\hat{y}_i, y_i) = \sigma^2 H_{ii}

so: E[op]=2ฯƒ2ntr(H)=2ฯƒ2pn\mathbb{E}[\text{op}] = \frac{2\sigma^2}{n} \text{tr}(H) = \frac{2\sigma^2 p}{n}

Therefore: E[Err]=E[err^train]+2ฯƒ2pn\boxed{\mathbb{E}[\text{Err}] = \mathbb{E}[\widehat{\text{err}}_{\text{train}}] + \frac{2\sigma^2 p}{n}}

The gap grows with pp (complexity) and shrinks with nn (more data). This is exactly the correction used by Mallows' CpC_p and AIC.


Part (b): Interpolation when p=np = n

When p=np = n and XX is invertible, OLS fits the data exactly: y^=Hy=Iy=yโŸนerr^train=0\hat{y} = Hy = Iy = y \quad \Longrightarrow \quad \widehat{\text{err}}_{\text{train}} = 0

Meanwhile, the true test error is: E[Err]=0+2ฯƒ2โ‹…nn=2ฯƒ2\mathbb{E}[\text{Err}] = 0 + \frac{2\sigma^2 \cdot n}{n} = 2\sigma^2

(or more precisely, grows with the noise level). Training error is maximally misleading: it says the model is perfect, while the actual generalization error can be arbitrarily bad. This makes training error useless for model selection in overparameterized regimes โ€” exactly the regime of modern neural networks.


Part (c): The LOO shortcut and the meaning of HiiH_{ii}

What is HiiH_{ii}? The diagonal entry Hii=xiT(XTX)โˆ’1xiH_{ii} = x_i^T(X^TX)^{-1}x_i measures the leverage of point ii โ€” how much point ii influences its own fitted value. High leverage means: "if I move yiy_i, my prediction y^i\hat{y}_i moves a lot."

The LOO shortcut. When you remove point ii and refit, the new prediction at xix_i is: y^i(โˆ’i)=y^iโˆ’Hii(yiโˆ’y^i)โ‹…11โˆ’Hiiโ‹…(1โˆ’Hii)\hat{y}_i^{(-i)} = \hat{y}_i - H_{ii}(y_i - \hat{y}_i) \cdot \frac{1}{1 - H_{ii}} \cdot (1-H_{ii})

More precisely, the Sherman-Morrison-Woodbury identity gives: yiโˆ’y^i(โˆ’i)=yiโˆ’y^i1โˆ’Hiiy_i - \hat{y}_i^{(-i)} = \frac{y_i - \hat{y}_i}{1 - H_{ii}}

Intuition for the correction: The residual yiโˆ’y^iy_i - \hat{y}_i when point ii is included is artificially small โ€” the model pulled y^i\hat{y}_i toward yiy_i by a fraction HiiH_{ii}. Dividing by (1โˆ’Hii)(1-H_{ii}) inflates the residual back to what it would have been if point ii hadn't been used in fitting.

  • If Hiiโ‰ˆ0H_{ii} \approx 0: point ii has little influence; residual is already honest.
  • If Hiiโ‰ˆ1H_{ii} \approx 1: the model fits point ii almost perfectly regardless of yiy_i; the correction is huge, reflecting that y^i(โˆ’i)\hat{y}_i^{(-i)} would be very different from yiy_i.

This is why the LOO-CV formula: CV(n)=1nโˆ‘i=1n(yiโˆ’y^i1โˆ’Hii)2\text{CV}_{(n)} = \frac{1}{n}\sum_{i=1}^n \left(\frac{y_i - \hat{y}_i}{1 - H_{ii}}\right)^2

is computable from a single fit โ€” no refitting required. It is one of the most elegant computational shortcuts in statistics.

Source: The Elements of Statistical Learning, Chapter 7 (Hastie, Tibshirani, Friedman)

Type: ML/StatsSource: The Elements of Statistical Learning, Chapter 7 (Hastie, Tibshirani, Friedman)Edit on GitHub โ†—