Contents
- 01Introduction
- 02Setting and notation
- 03Intuition
- 04The decomposition and its proof
- 05A warm-up you can do by hand
- 06The experiment
- 07Models where every term has a formula
- 08Diagnosing bias and variance in practice
- 09Fixing high bias and high variance
- 10Beyond the classical picture
- 11Interview questions and model answers
- 12Summary
- 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
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)
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.
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,
Averaging the variances of the n fitted values (the diagonal of \Cov(\hat y)),
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,
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
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,
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
Differentiating, \MSE_j'(\lambda)=2(\lambda\beta_j^2-\sigma^2)/(1+\lambda)^3, so the best penalty is
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.