๐Ÿงฎ Brain Teaser

The Optimism of Training Error

You fit a model to training data (x1,y1),โ€ฆ,(xN,yN)(x_1, y_1), \ldots, (x_N, y_N), producing predictions y^i\hat{y}_i. Define:

  • Training error: errโ€พ=1Nโˆ‘i=1NL(yi,y^i)\overline{\text{err}} = \frac{1}{N} \sum_{i=1}^N L(y_i, \hat{y}_i)
  • True (in-sample) error: Errin=1Nโˆ‘i=1NEY0[L(Yi0,y^i)]\text{Err}_{\text{in}} = \frac{1}{N} \sum_{i=1}^N \mathbb{E}_{Y^0}\left[L(Y_i^0, \hat{y}_i)\right], where Yi0Y_i^0 is a fresh response at xix_i, independent of the training data.

Using squared error loss L(y,y^)=(yโˆ’y^)2L(y, \hat{y}) = (y - \hat{y})^2, show that:

E[Errinโˆ’errโ€พ]=2Nโˆ‘i=1NCov(y^i,yi)\mathbb{E}\left[\text{Err}_{\text{in}} - \overline{\text{err}}\right] = \frac{2}{N} \sum_{i=1}^N \text{Cov}(\hat{y}_i, y_i)

This quantity is called the optimism of the training error. What does it tell you conceptually?

biasoverfittingmodel selectiondegrees of freedomcovariance

Answer: Optimism of Training Error

Key Idea / Intuition

Training error is optimistic โ€” it underestimates true prediction error โ€” because the model was fit to the same data it is evaluated on. The key insight is that the bias comes entirely from the covariance between predictions y^i\hat{y}_i and the training responses yiy_i: when your model has "seen" yiy_i, it adjusts y^i\hat{y}_i toward it, making the training loss look artificially small. The optimism measures exactly how much the model overfits, in units of covariance.


Formal Proof / Solution

Setup. Assume an additive noise model: yi=f(xi)+ฮตi,ฮตiโˆผ(0,ฯƒฮต2)y_i = f(x_i) + \varepsilon_i, \quad \varepsilon_i \sim (0, \sigma^2_\varepsilon) and Yi0=f(xi)+ฮตi0Y_i^0 = f(x_i) + \varepsilon_i^0 is an independent fresh observation at xix_i (same distribution as yiy_i, but independent of the training set).

Step 1: Expand Errin\text{Err}_{\text{in}}.

E[Errin]=1Nโˆ‘i=1NE[(Yi0โˆ’y^i)2]\mathbb{E}\left[\text{Err}_{\text{in}}\right] = \frac{1}{N}\sum_{i=1}^N \mathbb{E}\left[(Y_i^0 - \hat{y}_i)^2\right]

Since Yi0โŠฅy^iY_i^0 \perp \hat{y}_i (fresh response, independent of training): E[(Yi0โˆ’y^i)2]=E[(Yi0โˆ’f(xi))2]+E[(f(xi)โˆ’y^i)2]\mathbb{E}\left[(Y_i^0 - \hat{y}_i)^2\right] = \mathbb{E}\left[(Y_i^0 - f(x_i))^2\right] + \mathbb{E}\left[(f(x_i) - \hat{y}_i)^2\right] =ฯƒฮต2+E[(f(xi)โˆ’y^i)2]= \sigma^2_\varepsilon + \mathbb{E}\left[(f(x_i) - \hat{y}_i)^2\right]

Step 2: Expand errโ€พ\overline{\text{err}}.

E[errโ€พ]=1Nโˆ‘i=1NE[(yiโˆ’y^i)2]\mathbb{E}\left[\overline{\text{err}}\right] = \frac{1}{N}\sum_{i=1}^N \mathbb{E}\left[(y_i - \hat{y}_i)^2\right]

Write yi=f(xi)+ฮตiy_i = f(x_i) + \varepsilon_i and add/subtract f(xi)f(x_i): yiโˆ’y^i=ฮตiโˆ’(y^iโˆ’f(xi))y_i - \hat{y}_i = \varepsilon_i - (\hat{y}_i - f(x_i))

So: E[(yiโˆ’y^i)2]=E[ฮตi2]โˆ’2E[ฮตi(y^iโˆ’f(xi))]+E[(y^iโˆ’f(xi))2]\mathbb{E}\left[(y_i - \hat{y}_i)^2\right] = \mathbb{E}[\varepsilon_i^2] - 2\mathbb{E}[\varepsilon_i(\hat{y}_i - f(x_i))] + \mathbb{E}[(\hat{y}_i - f(x_i))^2] =ฯƒฮต2โˆ’2E[ฮตi(y^iโˆ’f(xi))]+E[(y^iโˆ’f(xi))2]= \sigma^2_\varepsilon - 2\mathbb{E}[\varepsilon_i(\hat{y}_i - f(x_i))] + \mathbb{E}[(\hat{y}_i - f(x_i))^2]

Step 3: Compute the optimism.

E[Errinโˆ’errโ€พ]=1Nโˆ‘i=1N(2E[ฮตi(y^iโˆ’f(xi))])\mathbb{E}\left[\text{Err}_{\text{in}} - \overline{\text{err}}\right] = \frac{1}{N}\sum_{i=1}^N \left(2\mathbb{E}[\varepsilon_i(\hat{y}_i - f(x_i))]\right)

=2Nโˆ‘i=1NE[ฮตiy^i]โˆ’E[ฮตif(xi)]= \frac{2}{N}\sum_{i=1}^N \mathbb{E}[\varepsilon_i \hat{y}_i] - \mathbb{E}[\varepsilon_i f(x_i)]

Since f(xi)f(x_i) is deterministic and E[ฮตi]=0\mathbb{E}[\varepsilon_i] = 0: E[ฮตif(xi)]=f(xi)E[ฮตi]=0\mathbb{E}[\varepsilon_i f(x_i)] = f(x_i)\mathbb{E}[\varepsilon_i] = 0

So: E[Errinโˆ’errโ€พ]=2Nโˆ‘i=1NE[ฮตiy^i]\mathbb{E}\left[\text{Err}_{\text{in}} - \overline{\text{err}}\right] = \frac{2}{N}\sum_{i=1}^N \mathbb{E}[\varepsilon_i \hat{y}_i]

Step 4: Recognize the covariance.

Cov(y^i,yi)=Cov(y^i,f(xi)+ฮตi)=Cov(y^i,ฮตi)=E[y^iฮตi]โˆ’E[y^i]E[ฮตi]\text{Cov}(\hat{y}_i, y_i) = \text{Cov}(\hat{y}_i, f(x_i) + \varepsilon_i) = \text{Cov}(\hat{y}_i, \varepsilon_i) = \mathbb{E}[\hat{y}_i \varepsilon_i] - \mathbb{E}[\hat{y}_i]\mathbb{E}[\varepsilon_i]

Since E[ฮตi]=0\mathbb{E}[\varepsilon_i] = 0: Cov(y^i,yi)=E[y^iฮตi]\text{Cov}(\hat{y}_i, y_i) = \mathbb{E}[\hat{y}_i \varepsilon_i]

Therefore: E[Errinโˆ’errโ€พ]=2Nโˆ‘i=1NCov(y^i,yi)\boxed{\mathbb{E}\left[\text{Err}_{\text{in}} - \overline{\text{err}}\right] = \frac{2}{N}\sum_{i=1}^N \text{Cov}(\hat{y}_i, y_i)}

Conceptual meaning.

  • For a linear smoother y^=Sy\hat{y} = Sy, we get โˆ‘iCov(y^i,yi)=trace(S)ฯƒฮต2\sum_i \text{Cov}(\hat{y}_i, y_i) = \text{trace}(S)\sigma^2_\varepsilon, which is why trace(S)\text{trace}(S) counts the effective degrees of freedom.
  • For OLS with pp parameters, trace(S)=p\text{trace}(S) = p, so optimism =2pฯƒ2N= \frac{2p\sigma^2}{N}, exactly the penalty in Mallows' CpC_p.
  • The more the model adapts to training noise (high covariance), the more optimistic the training error is, and the larger the gap to test error.

Source: The Elements of Statistical Learning, Hastie, Tibshirani, Friedman, 2nd ed., Section 7.4โ€“7.5

Type: ML/StatsSource: The Elements of Statistical Learning, Hastie, Tibshirani, Friedman, 2nd ed., Section 7.4โ€“7.5Edit on GitHub โ†—