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

07Models where every term has a formula

Simulations show the tradeoff; closed forms explain it. In this section we derive bias and variance exactly for three classic procedures.

7.1k-nearest-neighbor regression

To predict at a point x_0, k-NN regression averages the labels of the k training inputs closest to x_0, which we write x_{(1)},\dots,x_{(k)}. Treat the training inputs as fixed, so only the labels are random. Then

\fhat(x_0)=\frac1k\sum_{l=1}^k y_{(l)} =\underbrace{\frac1k\sum_{l=1}^k f(x_{(l)})}_{\text{constant}}+\underbrace{\frac1k\sum_{l=1}^k\varepsilon_{(l)}}_{\text{random}} .

The average model is therefore \fbar(x_0)=\frac1k\sum_l f(x_{(l)}), and since the k noise terms are independent with variance \sigma^2 each, the variance of their average is k\sigma^2/k^2=\sigma^2/k. Plugging both into Theorem 1 gives (Hastie et al., 2009, Section 7.3)

\mathrm{Err}(x_0)=\sigma^2+\Big[f(x_0)-\frac1k\sum_{l=1}^k f(x_{(l)})\Big]^2+\frac{\sigma^2}{k}. \tag{9}

The number of neighbors is a pure bias–variance knob. Averaging more labels divides the variance by k, but the neighbors lie farther from x_0, so the average of f over them drifts away from f(x_0) and the bias grows. For k=1 the variance is the full \sigma^2: the prediction inherits all the noise of a single label.

k-NN on the same sine curve with 100 evenly spaced inputs and \sigma=0.3. Left: the k=14 neighbors used to predict at x=0.62, and their average. Right: the exact terms of (9), averaged over test points in [0.05,0.95]; the total error is smallest at k=14. Data: computed for this tutorial, replicating the companion video.
Figure 7. k-NN on the same sine curve with 100 evenly spaced inputs and \sigma=0.3. Left: the k=14 neighbors used to predict at x=0.62, and their average. Right: the exact terms of (9), averaged over test points in [0.05,0.95]; the total error is smallest at k=14. Data: computed for this tutorial, replicating the companion video.
Try itChoose the number of neighbors

The k-NN prediction at x_0 is the average label of the k closest training points (orange). Right: the exact terms of equation (9), averaged over test points. Press New training set to draw fresh noise and watch the fit move: that movement is the variance.

Worked example 2. With 100 evenly spaced inputs and \sigma^2=0.09: at k=1 the variance is 0.090 and the total error 0.180. At k=14 the squared bias is 0.0013, the variance 0.0064, and the total 0.0977, the minimum. At k=40 the squared bias has grown to 0.089 and the total is back to 0.181. A Monte Carlo check at k=10 with 4,000 random draws gives 0.0985, against 0.0995 from the formula.

A remark on dimension. In one dimension the 14 nearest of 100 evenly spaced points lie within a short interval. In p dimensions, the neighborhood needed to capture a fixed fraction of the data grows rapidly with p (the “curse of dimensionality”), so for the same k the neighbors are much farther away and the bias term grows. This is one reason nearest-neighbor methods struggle with many features (Hastie et al., 2009, Section 2.5).

7.2Ordinary least squares

Let X\in\mathbb{R}^{n\times p} be a fixed design matrix of full column rank and suppose first that the linear model is correct, y=X\beta+\varepsilon with \Cov(\varepsilon)=\sigma^2I. Least squares gives \hat\beta=(X^\top X)^{-1}X^\top y and fitted values \hat y=Hy, with the hat matrix H of Section 4.3.

Bias. \E[\hat\beta]=(X^\top X)^{-1}X^\top X\beta=\beta, so OLS is unbiased when the model is correct.

Variance. Using \Cov(Ay)=A\Cov(y)A^\top,

\Cov(\hat\beta)=\sigma^2(X^\top X)^{-1},\qquad \Cov(\hat y)=H\,(\sigma^2 I)\,H^\top=\sigma^2H .

Averaging the variances of the n fitted values (the diagonal of \Cov(\hat y)),

\frac1n\sum_{i=1}^n\Var(\hat y_i)=\frac{\sigma^2}{n}\tr(H)=\frac{\sigma^2 p}{n}. \tag{10}

Variance grows linearly with the number of parameters and shrinks like 1/n. Two lessons follow. Each extra feature costs \sigma^2/n of average variance whether or not it is useful; and more data reduces variance without increasing bias, so the “tradeoff” is not a law of nature (Question 2 in Section 11). For instance, p=10, n=100 and \sigma^2=0.09 give an average variance of 0.009.

Bias from a missing feature. If the truth is y=X\beta+z\gamma+\varepsilon but we regress on X only, then \E[\hat\beta]=\beta+(X^\top X)^{-1}X^\top z\,\gamma: the coefficients absorb the part of the omitted variable that is correlated with the included ones. Leaving out a relevant feature trades variance (one fewer parameter) for bias, the same tradeoff once more.

7.3Ridge regression

Ridge regression (Hoerl and Kennard, 1970) adds a penalty on the size of the coefficients,

\hat\beta_\lambda=\arg\min_\beta\ \|y-X\beta\|^2+\lambda\|\beta\|^2=(X^\top X+\lambda I)^{-1}X^\top y,\qquad\lambda>0 .

The singular value decomposition shows exactly what the penalty does. Write the thin SVD X=UDV^\top, where U\in\mathbb{R}^{n\times p} and V\in\mathbb{R}^{p\times p} have orthonormal columns u_j and v_j and D=\mathrm{diag}(d_1,\dots,d_p) holds the singular values. Then

\hat y_\lambda=X\hat\beta_\lambda=\sum_{j=1}^p u_j\,\frac{d_j^2}{d_j^2+\lambda}\,u_j^\top y . \tag{11}

Least squares (\lambda=0) projects y onto each direction u_j and keeps the projection in full. Ridge keeps only a fraction s_j=d_j^2/(d_j^2+\lambda) of it, shrinking most strongly along directions with small singular values, which are exactly the directions in which the data carry little information and least squares is most unstable (Hastie et al., 2009, Section 3.4).

From (11), with \mu=X\beta the true mean vector,

\E[\hat y_\lambda]-\mu=-\sum_{j=1}^p\frac{\lambda}{d_j^2+\lambda}\,u_j\,(u_j^\top\mu),\qquad \frac1n\sum_{i}\Var(\hat y_{\lambda,i})=\frac{\sigma^2}{n}\sum_{j=1}^p\Big(\frac{d_j^2}{d_j^2+\lambda}\Big)^2 . \tag{12}

As \lambda grows, each shrinkage factor s_j decreases from 1 toward 0: the bias grows and the variance falls. The sum \mathrm{df}(\lambda)=\tr(H_\lambda)=\sum_j s_j is called the effective degrees of freedom; it decreases from p at \lambda=0 toward 0, and replaces p in (10) as the measure of model complexity.

The orthonormal case. If X^\top X=I, each coefficient is simply shrunk: \hat\beta_{\lambda,j}=\hat\beta_j/(1+\lambda). Then

\Bias(\hat\beta_{\lambda,j})=-\frac{\lambda}{1+\lambda}\beta_j,\qquad \Var(\hat\beta_{\lambda,j})=\frac{\sigma^2}{(1+\lambda)^2},\qquad \MSE_j(\lambda)=\frac{\lambda^2\beta_j^2+\sigma^2}{(1+\lambda)^2}. \tag{13}

Differentiating, \MSE_j'(\lambda)=2(\lambda\beta_j^2-\sigma^2)/(1+\lambda)^3, so the best penalty is

\lambda^\star=\frac{\sigma^2}{\beta_j^2}.

This is the shrunken mean of Section 5 again, with c=1/(1+\lambda): noisy problems (large \sigma^2) and small true effects (small \beta_j) call for more regularization.

7.4Ridge in the polynomial experiment

Ridge can tame the unstable degree-12 polynomial of Section 6. We fit degree 12 in a Legendre polynomial basis on fifteen points, penalizing every coefficient except the intercept, for a grid of \lambda values, and measure bias and variance with 500 training sets (Figure 8). With essentially no penalty (\lambda=10^{-9}) the variance is 0.700 and the test error 0.791, with all 13 effective degrees of freedom in use. At the best penalty, \lambda\approx0.32, the squared bias is 0.009, the variance 0.047 and the test error 0.146, using about 9.4 effective degrees of freedom. A very strong penalty (\lambda=10) leaves 2.9 degrees of freedom and a squared bias of 0.30: the model underfits.

Ridge-regularized degree-12 polynomial on 15 points: bias^2, variance and test error versus the penalty \lambda; the gold dot marks the best penalty, \lambda\approx0.32 (500 training sets per value; values above 0.5 are clipped; dashed: noise floor). Data: computed for this tutorial.
Figure 8. Ridge-regularized degree-12 polynomial on 15 points: bias^2, variance and test error versus the penalty \lambda; the gold dot marks the best penalty, \lambda\approx0.32 (500 training sets per value; values above 0.5 are clipped; dashed: noise floor). Data: computed for this tutorial.