Week 6 · Ridge and LASSO: taming overfitting
Read the words of the video
- Imagine a very eager student changing their answers to fit every little detail of practice questions, even noise. They fit practice data beautifully but fail on new questions. Our computer models can do this too, learning too much noise. We give the model a 'leash' to keep its answers sensible.
- We add a penalty for large 'weights' – the numbers the model learns. Ridge regression uses a soft leash: it gently pulls all weights smaller, but never to zero. LASSO regression uses a sharp leash: it can pull some weights exactly to zero, turning off unhelpful features.
- Let's see an example. If a model's best 'weight' for a feature is 3 without a penalty. Ridge, with a penalty, will shrink it, maybe to 1 or 0.3, but always keeps it positive. LASSO, however, with a strong enough penalty, can make that weight exactly zero, completely removing that feature.
- For your exam, remember that Ridge never makes weights exactly zero; LASSO can. Both add penalties to stop models from learning noise and overfitting. We choose the right 'leash strength', called lambda, by finding what works best on new data. Great job learning this complex idea!
The one-minute story
A model with too much freedom is like a very eager student who changes their answer to fit every little detail of the practice questions, including noise. Their weights (the numbers the model learns) swing wildly: +900 here, −870 there, cancelling each other out. It fits the practice data beautifully and falls apart on a new question.
The cure is a leash on the weights. We add a penalty for large weights to the thing we are minimising. Now the model must be useful and modest.
- Ridge puts a soft leash: every weight is pulled gently towards zero, but none is ever cut loose entirely.
- LASSO puts a sharp leash: it can pull some weights exactly to zero, which switches those features off. It chooses which features matter.
Same idea, two flavours, two different personalities.
By the end of this week you can:
- explain overfitting and why large weights are a warning sign;
- derive and use the ridge solution ;
- explain LASSO and why it gives sparse weights;
- interpret both as MAP estimation (Gaussian prior vs Laplace prior);
- pick by cross-validation and reason about bias and variance.
Words you need
- Regularisation - adding a penalty to discourage complicated models.
- - the penalty strength. means no penalty.
- penalty (ridge). penalty (LASSO).
- Shrinkage - making the weights smaller than the least-squares ones.
- Sparse - mostly zeros.
- Bias - systematic error from a model being too simple. Variance - sensitivity of the model to the particular training sample.
Idea 1: Why penalise?
If two features are almost copies of each other, least squares can give them huge opposite weights that nearly cancel. If is singular (columns dependent) it is even worse: there are infinitely many equally good solutions. Penalising the size of the weights fixes both problems: it prefers small, stable weights.
Idea 2: Ridge regression ( penalty)
Minimise fit + penalty:
Take the gradient and set it to zero: , giving
Things to notice:
- Adding adds to every eigenvalue of , so for any the matrix is invertible, even if was singular. Ridge always has a unique answer.
- : back to least squares. : .
- The objective is strictly convex (MLF week 8) so there is exactly one minimum.
- In the simplest case of one feature, : the penalty just inflates the denominator, shrinking .
Question. Data , . Fit by ridge with .
Solution. and , so .
| 0 (plain least squares) | |
| 1 | |
| 14 |
As grows the slope is pulled towards zero, but never reaches it.
Question. Two features are exact copies: and . Least squares has no unique answer. What does ridge with give?
Solution. is singular (determinant 0): any fits perfectly. With ridge: , invertible, and . By symmetry and , so for each. Ridge splits the credit equally between the twin features, and shrinks the total slightly (from to ).
Idea 3: LASSO ( penalty)
The absolute value has a sharp corner at 0 so the objective is not differentiable there, and there is no closed form in general (we use iterative methods). But it has a famous property: it sets some weights exactly to 0, a built-in feature selector.
For one feature whose unpenalised best weight is , minimising gives the soft-threshold:
and the ridge version of the same problem, , gives .
Question. Unpenalised best . Compare ridge and LASSO for and .
Solution. Ridge: and . LASSO: and .
For , LASSO has set the weight to exactly zero while ridge still keeps a small non-zero value.
The geometry (why LASSO makes zeros)
Equivalent view: minimise the squared error subject to a budget on the weights: (ridge) or (LASSO). The error has elliptical contour lines. The ridge budget region is a round disc; the LASSO region is a diamond with corners on the axes. The first contour to touch a smooth disc touches it anywhere; but it very often first touches the diamond at a corner, and a corner sits on an axis, where one weight is exactly 0.
Idea 4: The Bayesian meaning
From MLT week 5: Gaussian noise gives the squared-error fit term. Add a prior on the weights:
| Prior on | MAP objective | Method |
|---|---|---|
| Gaussian | squared error , | Ridge |
| Laplace | squared error | LASSO |
A Laplace density has a sharp spike at 0, so it "believes in zeros", which explains the sparsity. A tighter prior (small ) means a bigger .
Idea 5: Choosing and the bias-variance trade-off
- Small : flexible, low bias, high variance (overfits).
- Large : stiff, high bias, low variance (underfits).
- Training error rises with ; validation error is U-shaped: best somewhere in the middle. Pick by cross-validation.
- Standardise the features first (the penalty treats all weights equally, so features on different scales would be punished unfairly), and normally do not penalise the intercept.
- Elastic net mixes both penalties.
Your turn
Data , . Compute the ridge weight for with (check the case too).
Need a hint?
.
Show the full solution
, . : . : .
The unpenalised best weights for three features are . What are the LASSO weights (per-feature problem) with ? And ridge with ?
Need a hint?
Soft-threshold at .
Show the full solution
LASSO: ; (since ); . Ridge (): . LASSO zeroed the middle feature; ridge did not.
For each, say whether it goes up or down as increases: (a) training error (b) the size of the weights (c) variance of the predictions (d) bias.
Need a hint?
More penalty means a stiffer model.
Show the full solution
(a) up, (b) down, (c) down, (d) up.
(a) Ridge always gives a unique solution for . (b) LASSO can set weights exactly to zero. (c) Ridge is MAP with a Laplace prior. (d) Ridge has a closed-form solution.
Need a hint?
Review the table in Idea 4.
Show the full solution
(a) True. (b) True. (c) False: ridge is a Gaussian prior; LASSO is Laplace. (d) True: .
Validation errors for are . Which would you choose and why?
Need a hint?
Smallest validation error.
Show the full solution
(error ). The curve is U-shaped: smaller overfits, larger underfits.
Exam radar
MLT Quiz 2 summaries mention ridge in 7 papers. In the End Term: ridge regression in 20 papers (and "Ridge Regression (L2 Regularization)" in 13), overfitting in 19, underfitting in 15. LASSO is named in the course syllabus, so expect it too.
Typical: "which of the following is the ridge solution?", "what happens to the weights as ?", "which regulariser produces sparse weights?", "ridge as MAP with which prior?", and a one-dimensional ridge calculation.
- Ridge never gives exact zeros; LASSO can.
- Ridge penalty is on squared weights; the intercept is usually not penalised.
- Bigger means more bias, less variance.
- Gaussian prior ridge; Laplace prior LASSO (easy to swap by accident).
- Features must be on comparable scales before regularising.
Quick quiz
The ridge regression solution is...
- (X^T X)^-1 X^T y
- (X^T X + lambda I)^-1 X^T y
- (X X^T)^-1 y
- X^T y / lambda
As lambda grows very large, ridge weights tend to...
- infinity
- the least-squares weights
- zero
- one
Which penalty tends to produce exactly-zero weights?
- l2 (ridge)
- l1 (LASSO)
- no penalty
- none of them
Select all TRUE statements. (select all that apply)
- Ridge is MAP estimation with a Gaussian prior on the weights.
- Increasing lambda increases the bias of the model.
- Ridge regression can make a singular X^T X problem worse.
- LASSO has a simple closed-form solution for every data set.
Recap
- Ridge: . Always invertible; shrinks smoothly; Gaussian prior.
- LASSO: . Sparse weights; no closed form; Laplace prior; soft-threshold in 1-D.
- : bias , variance , training error . Choose by cross-validation; standardise features.
- Ridge: ; 1-D: ; per-feature shrink
- LASSO soft-threshold:
- MAP: Gaussian prior (ridge); Laplace prior LASSO
- MLF week 8 - Bowls, not bumpy hills: convexity: Ridge and LASSO are convex problems, so a global best exists and can be found.
- MLP week 4 - Polynomial regression and regularisation: Ridge and LASSO: the theory, then the code.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 18 real questions on this week All Quiz 2 papers