Foundations to Frontier
Tests
D. Classical Machine Learning › D1 Linear Models for Regression

Ridge Regression (L2 Regularisation)

Ridge regression adds a penalty λ‖β‖² to the least-squares loss, shrinking coefficients toward zero. The solution (XᵀX + λI)⁻¹Xᵀy always exists, even with more features than examples or perfectly collinear features, and trading a little bias for much less variance often lowers prediction error substantially. This page covers the objective and its closed form, the SVD view (each principal direction shrunk by d²/(d² + λ)), effective degrees of freedom, the Bayesian interpretation (Gaussian prior), and choosing λ efficiently with exact leave-one-out and generalised cross-validation. In the exercises you implement ridge with an unpenalised intercept, the full regularisation path via one SVD, effective degrees of freedom and closed-form LOOCV and GCV; then you compare ridge with ordinary least squares as the number of features approaches and exceeds the number of examples, reproducing the double-descent peak.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A leash on the coefficients

If you ask a model to fit noisy data perfectly, it may give some features huge positive weights and others huge negative ones that cancel out on the training data but go wild on new data. Ridge regression puts a leash on the weights: every unit of weight costs a small fee. The model then prefers modest weights that fit almost as well. The fee, λ, sets how short the leash is.

Ridge: shrinking the coefficients
β₁β₂
least squares: (1.63, 1.61) · ridge: (1.37, 1.44) · length 1.99 · extra training error 6.06
The grey curve is the whole ridge path from λ = 0.01 to 10⁴. With highly correlated features the error ellipses are long and thin: the data barely distinguish β₁ from β₂, and ridge settles on a shorter, more balanced vector at little cost in training error. That bias is what buys lower variance on new data. True coefficients: (2, 1).
Why engineers care

Engineering datasets often have many correlated sensors and few test runs: dozens of strain gauges, a handful of load cases. Plain least squares breaks down there. Ridge gives stable, accurate models and is the standard fix for ill-posed calibration and inverse problems (Tikhonov regularisation).

📝 Quick check (Level 0)
0/3 answered
  1. 1. Ridge regression makes the weights…
  2. 2. A larger λ means…
  3. 3. In the demo, as λ grows the ridge point moves…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

A worked example by hand

One centred feature with Sxy = Σxy = 6 and Sxx = Σx² = 10. The ridge slope is

β = Sxy/(Sxx + λ).

λ β
0 (OLS) 0.6
5 0.4
30 0.15

Ridge divides by a larger number, so the slope shrinks. With two copies of the same feature, OLS has no unique answer, but ridge splits the weight equally between them.

The steps in plain English:

  1. Standardise the features (ridge is not scale-invariant).
  2. Centre the target, so the intercept is not penalised.
  3. For a grid of λ values, solve (XᵀX + λI)β = Xᵀy.
  4. Pick λ by cross-validation.
  5. Refit on all data.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Ridge slope with Sxy = 6, Sxx = 10, λ = 5?
  2. 2. Why standardise before ridge?
  3. 3. Is the intercept usually penalised?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Objective, solution and tuning

Objective: minimise ‖y − Xβ‖² + λ‖β‖² (λ ≥ 0), with centred y and standardised X.

Solution: β̂_λ = (XᵀX + λI)⁻¹Xᵀy. It exists for any λ > 0, since XᵀX + λI is positive definite.

SVD view. Write X = UDVᵀ. Then Xβ̂_λ = Σⱼ uⱼ · dⱼ²/(dⱼ² + λ) · uⱼᵀy. Directions with small singular values (low-variance, noisy directions) are shrunk the most.

Effective degrees of freedom: df(λ) = Σ dⱼ²/(dⱼ² + λ), falling from p (at λ = 0) to 0.

Choosing λ:

  • K-fold CV; or
  • the exact leave-one-out shortcut LOO = mean[(rᵢ/(1 − hᵢᵢ))²], with H = X(XᵀX + λI)⁻¹Xᵀ, with no refitting;
  • or GCV, which replaces hᵢᵢ by their average. One SVD gives the whole path.
from sklearn.linear_model import RidgeCV
from sklearn.pipeline import make_pipeline; from sklearn.preprocessing import StandardScaler
model = make_pipeline(StandardScaler(), RidgeCV(alphas=np.logspace(-3, 4, 50))).fit(X, y)   # efficient LOO by default
print(model[-1].alpha_)

Hyperparameter: λ, chosen on a log grid. Use it when there are many correlated features, when p ≈ n or p > n, and for ill-posed inverse problems. Prefer lasso or elastic net (D1.6, D1.7) when you need feature selection.

🐍 Exercise 1: ridge regression from scratch
Fit ridge with an unpenalised intercept by centring; compute the whole regularisation path from one SVD; compute shrinkage factors and effective degrees of freedom; implement exact leave-one-out and generalised cross-validation in closed form and verify LOOCV against brute-force refitting.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Ridge shrinks most along directions with…
  2. 2. Effective degrees of freedom at λ = 0?
  3. 3. LOOCV for ridge requires…

Level 3: Advanced

★★★★☆ Advanced⏱ 35 minGraduate level, with full derivations and proofs.

Derivations and connections

Derivation. The gradient of ‖y − Xβ‖² + λ‖β‖² is −2Xᵀ(y − Xβ) + 2λβ. Setting it to zero gives (XᵀX + λI)β = Xᵀy. The objective is strictly convex for λ > 0.

Constrained form. Equivalently, minimise ‖y − Xβ‖² subject to ‖β‖² ≤ t. The solution lies where an error ellipse first touches the ball (see the demo).

Bias and variance. With the SVD:

  • the squared bias grows with λ along each direction, as λ²/(dⱼ² + λ)² times (vⱼᵀβ)²;
  • the variance shrinks, as σ²dⱼ²/(dⱼ² + λ)².

There is always some λ > 0 that lowers MSE relative to OLS (Hoerl and Kennard, 1970).

Bayesian view. With a Gaussian prior β ~ N(0, τ²I) and Gaussian noise σ², the posterior mean (and MAP) equals ridge with λ = σ²/τ² (A5.2.2, D1.9).

Other links:

  • Ridge is Tikhonov regularisation of an ill-posed inverse problem.
  • The dual form β = Xᵀ(XXᵀ + λI)⁻¹y gives kernel ridge regression (D5.54).
  • As λ → 0⁺, ridge tends to the minimum-norm least-squares solution.

Complexity. One SVD, O(np·min(n, p)), gives the whole path.

Interview questions.

  • Why does ridge never set coefficients exactly to zero? The L2 ball has no corners. The penalty’s gradient 2λβ vanishes at zero, so small coefficients are only shrunk proportionally.
  • What happens to ridge as λ → ∞? All coefficients go to 0, and the prediction becomes the mean of y.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Ridge equals MAP estimation with…
  2. 2. As λ → 0⁺ with p > n, ridge approaches…
  3. 3. Hoerl and Kennard showed that…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Ridge versus least squares as features pile up

History. Tikhonov (1943, 1963) regularised ill-posed integral equations. Hoerl and Kennard (1970) introduced ridge regression for collinear designs. Golub, Heath and Wahba (1979) proposed GCV for choosing λ. Recent work on overparameterised models (Hastie, Montanari, Rosset and Tibshirani, 2022) revisited minimum-norm least squares and found the double-descent curve.

Exercise 1.

  • At λ = 0, ridge reproduces OLS.
  • The SVD path matches direct solves at every λ, and ‖β‖ shrinks from 2.56 to 0.02 at λ = 10⁴.
  • Effective degrees of freedom fall from 6 toward 1.
  • The closed-form LOOCV (0.2671) equals brute-force refitting exactly, and GCV is within 1%.

Exercise 2: n = 60 examples, p from 10 to 120 correlated features. Signal variance 4, noise variance 1; median test MSE over 16 datasets; λ chosen on a 22-point log grid.

p OLS (minimum-norm) Ridge, GCV Ridge, 5-fold CV Ridge, oracle λ
10 0.17 0.18 0.18 0.16
30 0.98 0.61 0.56 0.55
50 7.23 1.22 1.22 1.08
58 30.9 1.53 1.43 1.30
70 5.53 1.79 1.89 1.56
120 2.62 2.22 2.22 2.01
  • With few features, OLS is as good as ridge.
  • Near p = n, least squares explodes: the smallest singular values approach zero and their noise is amplified. Ridge is 20× better.
  • Beyond p = n, the minimum-norm solution improves again: the double-descent curve. It still loses to ridge.
  • GCV and CV pick a λ within about 20% of the best possible test error, using only the training data.
🐍 Exercise 2 (research): ridge, least squares and double descent
With 60 examples and 10 to 120 correlated features, compare the test error of minimum-norm least squares with ridge regression tuned by GCV, by 5-fold cross-validation and by an oracle that sees the test error.

Ridge in AI, robotics and aerospace

  • Inverse problems: heat-flux reconstruction, load identification from strain gauges, and image deblurring (Tikhonov).
  • System identification: regularised least squares for many-parameter dynamic models.
  • Calibration: multi-sensor calibration with correlated channels.
  • Deep learning: weight decay is the same L2 penalty, applied to network weights.

References

  1. A. N. Tikhonov (1963). “Solution of incorrectly formulated problems and the regularization method.” Soviet Mathematics Doklady 4.
  2. A. E. Hoerl, R. W. Kennard (1970). “Ridge regression: biased estimation for nonorthogonal problems.” Technometrics 12(1).
  3. G. H. Golub, M. Heath, G. Wahba (1979). “Generalized cross-validation as a method for choosing a good ridge parameter.” Technometrics 21(2).
  4. T. Hastie, A. Montanari, S. Rosset, R. J. Tibshirani (2022). “Surprises in high-dimensional ridgeless least squares interpolation.” Annals of Statistics 50(2).
  5. T. Hastie, R. Tibshirani, J. Friedman (2009). The Elements of Statistical Learning, 2nd ed., section 3.4. Springer.