MoonSay /Tutorials/Machine Learning Interview #2
Contents
  1. 01Introduction
  2. 02Setting and notation
  3. 03Intuition
  4. 04The decomposition and its proof
  5. 05A warm-up you can do by hand
  6. 06The experiment
  7. 07Models where every term has a formula
  8. 08Diagnosing bias and variance in practice
  9. 09Fixing high bias and high variance
  10. 10Beyond the classical picture
  11. 11Interview questions and model answers
  12. 12Summary
  13. References

04The decomposition and its proof

Theorem 1 (Bias–variance decomposition). Under model (2), for any learning procedure and any fixed input x, the expected squared prediction error over training sets and noise is

\underbrace{\E_{\D,\varepsilon}\!\left[\big(y-\fhat(x;\D)\big)^2\right]}_{\mathrm{Err}(x)} \;=\; \underbrace{\sigma^2}_{\text{noise}} \;+\; \underbrace{\big(f(x)-\fbar(x)\big)^2}_{\Bias^2(x)} \;+\; \underbrace{\E_{\D}\!\left[\big(\fhat(x;\D)-\fbar(x)\big)^2\right]}_{\Var(x)} . \tag{5}

Fix x and abbreviate f=f(x), \fhat=\fhat(x;\D) and \fbar=\fbar(x). Here f and \fbar are constants, \fhat depends on the training set \D, and \varepsilon is the noise of the new test observation, which is independent of \D.

Step 1: split the error. Add and subtract f and \fbar to write the error as a sum of three pieces with clear meanings:

y-\fhat \;=\; \underbrace{\varepsilon}_{\text{noise}} \;+\; \underbrace{(f-\fbar)}_{\text{bias}} \;+\; \underbrace{(\fbar-\fhat)}_{\text{deviation of this fit from the average}} .

Step 2: square. Writing a=\varepsilon, b=f-\fbar and c=\fbar-\fhat,

(y-\fhat)^2 = a^2+b^2+c^2+2ab+2ac+2bc .

Step 3: take expectations term by term. Expectation is linear, so we may average each term separately. The three cross terms vanish:

  • \E[2ab]=2(f-\fbar)\,\E[\varepsilon]=0, because f-\fbar is a constant and the noise has mean zero.
  • \E[2ac]=2\,\E[\varepsilon]\;\E_{\D}[\fbar-\fhat]=0. The factorization uses independence of the test noise and the training set: for independent random variables, the expectation of a product is the product of expectations.
  • \E[2bc]=2(f-\fbar)\,\E_{\D}[\fbar-\fhat]=2(f-\fbar)\,(\fbar-\fbar)=0, by the very definition of \fbar as the average of \fhat.

Step 4: collect what remains. \E[a^2]=\E[\varepsilon^2]=\Var(\varepsilon)=\sigma^2, since \E\varepsilon=0. The term b^2=(f-\fbar)^2 is a constant, so its expectation is itself. Finally \E[c^2]=\E_{\D}[(\fhat-\fbar)^2]=\Var(x). Adding the three gives (5).

Figure 2 shows the three terms at one input, x\approx0.55, for twenty degree-3 polynomial fits from the simulation of Section 6. The gold dot is the truth f(x) and the purple square the average prediction \fbar(x) over 500 training sets; the blue bar between them is the bias. The orange bar marks one standard deviation of the predictions on either side of their average; its square is the variance. The gray dots are possible new observations y=f(x)+\varepsilon, whose spread is the noise. At this input the squared bias is 0.099 and the variance 0.015: a degree-3 polynomial is too stiff to follow the sine curve here, and it misses in the same direction for almost every training set.

The three terms at a single input x\approx0.55. Thin teal curves: degree-3 least-squares fits from twenty random training sets; dashed: the true f. Data: Monte Carlo simulation computed for this tutorial, replicating the simulation of the companion video.
Figure 2. The three terms at a single input x\approx0.55. Thin teal curves: degree-3 least-squares fits from twenty random training sets; dashed: the true f. Data: Monte Carlo simulation computed for this tutorial, replicating the simulation of the companion video.

4.1From one point to the whole input space

The theorem is pointwise. To summarize a procedure by one number we average over test inputs drawn from their distribution p(x):

\E_x\!\left[\mathrm{Err}(x)\right]=\sigma^2+\E_x\!\left[\Bias^2(x)\right]+\E_x\!\left[\Var(x)\right]. \tag{6}

This is the quantity a large held-out test set estimates. When we report “the bias” and “the variance” of a procedure, we usually mean the averaged terms \E_x[\Bias^2(x)] and \E_x[\Var(x)]. (If the noise variance depends on x, the first term becomes \E_x[\sigma^2(x)]; nothing else changes.)

4.2Three observations

  1. The decomposition is exact. It is an identity, not an approximation, and holds for every learning procedure under squared loss: linear models, trees, neural networks.
  2. All three terms are non-negative. Reducing one term never hurts the others mathematically; what makes it a tradeoff is that the usual knobs (model size, regularization, number of neighbors) move bias and variance in opposite directions.
  3. The noise floor is untouchable. No choice of model reduces \sigma^2. Only better inputs (more informative features) can, because they change what counts as noise.

4.3Why training error is optimistic

The proof needed the test noise \varepsilon to be independent of the training set. On the training points themselves this fails: the fitted value \fhat(x_i) was chosen partly to match the noise \varepsilon_i. We can quantify the consequence exactly for least squares. Let X be an n\times p design matrix of full column rank, y=X\beta+\varepsilon with \Cov(\varepsilon)=\sigma^2I, and fitted values \hat y=Hy with the hat matrix H=X(X^\top X)^{-1}X^\top. H is symmetric and idempotent (H^2=H), HX=X, and \tr(H)=\tr\big((X^\top X)^{-1}X^\top X\big)=\tr(I_p)=p.

Proposition 1 (Optimism of training error). With the inputs held fixed, the expected training error and the expected error on fresh responses y'=X\beta+\varepsilon' at the same inputs are

\E\Big[\tfrac1n\|y-\hat y\|^2\Big]=\sigma^2\Big(1-\frac pn\Big),\qquad \E\Big[\tfrac1n\|y'-\hat y\|^2\Big]=\sigma^2\Big(1+\frac pn\Big).

The gap, 2\sigma^2p/n, grows with the number of fitted parameters.

Since (I-H)X\beta=0, the residual is y-\hat y=(I-H)\varepsilon, so \E\|y-\hat y\|^2=\E[\varepsilon^\top(I-H)^\top(I-H)\varepsilon]=\E[\varepsilon^\top(I-H)\varepsilon]=\sigma^2\tr(I-H)=\sigma^2(n-p), using \E[\varepsilon^\top A\varepsilon]=\sigma^2\tr(A). For fresh responses, y'-\hat y=\varepsilon'-H\varepsilon with \varepsilon' independent of \varepsilon, so \E\|y'-\hat y\|^2=\E\|\varepsilon'\|^2+\E\|H\varepsilon\|^2=n\sigma^2+\sigma^2\tr(H)=\sigma^2(n+p). Divide by n.

With p=12 parameters and n=15 points, training error is expected to be only \sigma^2(1-12/15)=0.2\,\sigma^2, while error on new responses is 1.8\,\sigma^2: a ninefold difference, even when the model is correct. This is why model complexity must never be chosen by training error.

Pause and think. Why does the proof of Theorem 1 require the test noise \varepsilon to be independent of \D?

Show the answer

Otherwise the cross term \E[\varepsilon(\fbar-\fhat)] need not vanish. On training points it is negative, because the fit moves toward its own noise; Proposition 1 measures exactly how much.