Contents
02Overfitting and why we regularize
2.1An overfit model
Overfitting means fitting the noise of the particular training set instead of the pattern that generalizes. Figure 1 shows the classic example from the video: 20 noisy points of the curve \sin(2.3\pi x+0.2), with noise standard deviation \sigma=0.3, fitted by a polynomial of degree 15, that is, a linear model with 15 features x,x^2,\dots,x^{15} (standardized) and 15 weights. Least squares has almost as many weights as points, and it uses them: the curve wiggles to pass near every point, and the coefficients explode. With essentially no penalty (\lambda=10^{-9}) the largest coefficient is 1{,}439.6 in absolute value. The training error is a tiny 0.0114, but on 3{,}000 fresh validation points the error is 0.1735, far above the noise level \sigma^2=0.09, which is the best any model could do.
Why do the weights explode? The powers x^{13},x^{14},x^{15} are almost the same function on [0,1], so the columns of X are nearly collinear. The eigenvalues of X^\top X range from 147 down to 8.9\times10^{-10}, a condition number of 1.7\times10^{11}. In a direction with a tiny eigenvalue the data barely constrain the weights, so a huge positive weight on one power and a huge negative weight on its neighbour cost almost nothing in training error. Least squares exploits that freedom to chase noise.
2.2The penalized objective
The fix is to make large weights expensive. Instead of the training loss alone we minimize (1), a training loss plus a penalty \lambda\,\Omega(w) that measures the size of the weights. The two classic choices are
The regularization strength \lambda sets the exchange rate between fit and size. At \lambda=0 we get least squares. As \lambda\to\infty only the penalty matters and all weights go to zero, so the model predicts the mean of y. In between, a weight is allowed to be large only if it buys a correspondingly large reduction of the training loss. Figure 1 shows the whole range for L2: as \lambda grows from 10^{-9} to 10^{2} the curve goes from wiggly to smooth to flat, the training error rises steadily from 0.0114 to 0.430, and the validation error first falls, to a minimum of 0.1277 at \lambda=10^{-1.81}\approx0.0157, then rises to 0.473.
An everyday analogy. A student who may write an essay of any length can pad it until it matches every point the teacher ever mentioned; a word limit forces them to keep only what matters. A penalty is a soft word limit: you may go over, but every extra word costs you.
2.3A bias–variance knob
The U-shaped validation curve is the bias–variance tradeoff of Episode 2 (see the bias–variance tutorial). Increasing \lambda makes the fit less sensitive to the particular noisy sample, which lowers variance, but pulls the weights away from the values the data prefer, which adds bias. A small amount of bias can buy a large reduction in variance, and the next theorem shows that for ridge regression this is always true.
Theorem 1 (Hoerl and Kennard, 1970a, Theorem 4.3). Let y=Xw+\varepsilon with \E\varepsilon=0, \Cov(\varepsilon)=\sigma^2I and X^\top X invertible. Then there is a \lambda>0 for which the ridge estimate \hat w_\lambda=(X^\top X+\lambda I)^{-1}X^\top y has a smaller mean squared error \E\|\hat w_\lambda-w\|^2 than least squares (\lambda=0).
Diagonalize X^\top X=V\operatorname{diag}(d_1,\dots,d_p)V^\top with orthonormal eigenvectors v_j (the columns of V) and eigenvalues d_j>0, and work in these coordinates: write \alpha=V^\top w for the true weights and \hat\alpha_\lambda=V^\top\hat w_\lambda. Since X^\top y=X^\top Xw+X^\top\varepsilon,
because v_j^\top X^\top\varepsilon has mean 0 and variance \sigma^2v_j^\top X^\top Xv_j=\sigma^2d_j. Squared bias plus variance, summed over the orthonormal coordinates, gives
At \lambda=0 this is \sigma^2\sum_j1/d_j, the least-squares error. Differentiate each term with the quotient rule:
At \lambda=0 the derivative of the bias term vanishes, so \MSE'(0)=-2\sigma^2\sum_j1/d_j^2<0. A function with a negative slope at 0 decreases for small \lambda>0, which proves the claim. (The bias term grows only like \lambda^2, while the variance falls linearly in \lambda: a little bias is almost free.)
The proof also shows where the gain comes from: the variance of least squares in direction v_j is \sigma^2/d_j, which is enormous when d_j is tiny, exactly the nearly collinear directions of the polynomial example. Ridge divides that variance by roughly (1+\lambda/d_j)^2 while adding bias only to the extent that the true weight has a component \alpha_j in that direction.
The original example. In their companion paper, Hoerl and Kennard (1970b) analysed a regression with 10 correlated factors. The eigenvalues of X^\top X (in correlation form) ranged from 3.692 down to 0.068, so \sum_j1/d_j=33.8: the least-squares estimate was expected to lie more than three times as far from the truth as with uncorrelated factors, for which the sum would be 10. Two factors with correlation 0.84 received large coefficients of opposite sign. Plotting all coefficients against \lambda, a ridge trace, they saw the estimates stabilize for \lambda between 0.2 and 0.3, and proposed choosing \lambda there.
Worked example 1. Take two standardized features with correlation 0.99, scaled so that X^\top X=\begin{pmatrix}1&0.99\\0.99&1\end{pmatrix}, true weights w=(0.5,0.5) and noise variance \sigma^2=0.01. The eigenvalues are d_1=1.99 with v_1=(1,1)/\sqrt2 and d_2=0.01 with v_2=(1,-1)/\sqrt2, and \alpha=V^\top w=(0.7071,\,0). Least squares has \MSE(0)=0.01\,(1/1.99+1/0.01)=0.005+1.000=1.005: almost all of it is variance in the “difference” direction v_2. With \lambda=0.1, (2) gives variance 0.01282 and squared bias 0.00114, a total of 0.01396, which is 72 times smaller. The best penalty is \lambda\approx0.177, with \MSE=0.0104.
2.4Choosing \lambda
The best \lambda depends on the unknown w and \sigma^2, so in practice it is chosen from data. The standard method is K-fold cross-validation (Stone, 1974): split the training data into K folds (usually 5 or 10), fit on K-1 folds and measure the error on the held-out fold, for every \lambda on a logarithmic grid (for example 10^{-4},10^{-3.5},\dots,10^{2}), and pick the \lambda with the smallest average held-out error. A popular variant, the one-standard-error rule, picks the largest \lambda whose error is within one standard error of the minimum, preferring the simpler model when the difference is within the noise (Hastie et al., 2009, §7.10). For ridge regression, generalized cross-validation gives a closed-form shortcut (Golub et al., 1979). The test set must not be used to choose \lambda; it is touched once, at the end.
Pause and think. Figure 1(c) shows the training error increasing with \lambda over the whole range. Can a penalty ever lower the training error?
Show the answer
No, as long as we minimize (1) exactly. At any \lambda>0 the minimizer could have chosen the least-squares weights, which have the smallest possible training loss; it accepts a larger training loss only because it lowers the penalty. Regularization always gives up training fit; its purpose is a lower error on new data.