TL;DR
Penalized least squares is Linear Regression with a price on large weights. It picks the weights that make the squared errors on the training data small and the weights themselves small, at the same time. The price is the penalty from L2 Regularization, and its strength is a number you choose before fitting, called a hyperparameter.
The best weights have a closed form. The penalty adds a constant to the diagonal of the matrix that must be inverted (see Matrix Inverse), so the formula still works when the inputs are so correlated that ordinary least squares breaks down.
Where you'll see it: ridge regression in statistics software, the weight_decay setting when training neural networks, and as the building block of Kernel Ridge Regression.
Intuition
The question
Ordinary least squares asks which weights make the predictions closest to the targets. With plenty of data and inputs that carry different information, the answer is stable. Trouble starts when two inputs say nearly the same thing. Height in centimetres and height in inches carry the same information, so the fit cannot tell which one deserves credit. It may give one a large positive weight and the other a large negative weight. On the training data these cancel out nicely. On new data, where the two inputs drift apart, the predictions swing wildly.
Penalized least squares asks a different question: which weights fit the data well and are also small? You decide how much a unit of weight size is worth. A small price gives something close to ordinary least squares. A large price pulls every weight toward zero.
Shrinkage in one number
Take a single input with no intercept. Ordinary least squares gives a weight . With the penalty, the weight becomes multiplied by a factor , where is the sum of the squared inputs and is the penalty strength. That factor always lies between 0 and 1. The weight shrinks, but it never flips sign.
- s = 5
- s = 20
The math
The penalized objective
Given training examples, stack the inputs as the rows of a matrix of size , and the targets as a vector . For a weight vector and a strength , the objective is
The solution is the that minimises .
The first term is the sum of squared errors: holds the predictions, and the squared length of the difference from is the total squared miss. The second term is the penalty, the squared length of the weight vector (see Vector Norm). The number is not fitted from the data. You choose it, usually by checking performance on held-out data with Cross-Validation.
The intercept is normally left out of the penalty. The simple way to do this is to centre the inputs and targets first, by subtracting their means. After centring, the intercept is just the mean of , and the penalised fit uses only the slopes.
The closed-form solution
Expand the objective. Writing for the transpose of :
Take the gradient with respect to . For a symmetric matrix , the gradient of is , and the gradient of is . So
Set the gradient to zero and move the terms to one side. The identity matrix (ones on the diagonal, zeros elsewhere) lets us write :
The formula is ordinary least squares with added to . Why is the inverse safe? The matrix never has negative eigenvalues. Adding with moves every eigenvalue up by , so none of them is zero. A matrix with no zero eigenvalue is invertible, so the solution always exists, even when is singular.
What the penalty does in each direction
Write the inputs with a Singular Value Decomposition (SVD), , where are the singular values on the diagonal of . Substituting into the formula gives
Each direction in the input space is scaled by . When is large (the data varies strongly along that direction), the factor is close to , the ordinary answer. When is small (the data barely varies there, which is what correlated inputs produce), the factor is pulled toward zero. So the penalty acts mainly on the directions the data cannot pin down.
A Bayesian reading
If the noise in the targets is Gaussian (see Gaussian (Normal) Distribution) and the weights have a Gaussian prior centred at zero, then the most probable weights, the MAP estimate, are exactly the minimiser of . The strength is , where is the noise size and is the prior's spread. A strong belief that weights are small means a large .
Worked examples
Data: and . One input, no intercept.
- Sums: and .
- Objective for one weight : .
- Derivative: . Set it to zero: .
- No penalty, : . This is exact, since for both points.
- : . The weight is halved.
- : . The weight keeps shrinking but never reaches zero.
Interpretation: the penalty trades fit for size. The data alone wants slope 2, and each larger asks for a smaller slope.
Centred data: and . The true slope is about 2.
- , so .
- .
- Ordinary weight: .
- Penalized weight: . Check with the shrink factor: gives the same value.
- : . The shrink factor is .
- : . The shrink factor is , so the slope is halved.
Interpretation: a penalty of 10 is as strong as the data's own spread of inputs, and it cuts the slope in half. A penalty of 2 barely moves it.
Let the two inputs be the same column, so , with targets .
- and .
- Its determinant is , so has no inverse. Any weights with fit the data exactly, so ordinary least squares has no single answer.
- With the penalty: . The two weights must be equal by symmetry, so , and the first row gives . So .
- : the limit gives for each weight. Among all weights with , this pair has the smallest total squared size, so the penalty chooses it in the limit.
- : . Check: .
Interpretation: the penalty splits the weight evenly between the two copies and gives a unique answer. Ordinary least squares could not decide.
Suppose one standardised input is observed 50 times, so . A validation set would decide between several strengths. The shrink factors below are exact values of . The choice itself depends on your data, so no error figures are given here.
| Strength | Shrink factor | What it does |
|---|---|---|
| 0 | 1.000 | Ordinary least squares |
| 1 | 0.980 | Almost no change |
| 10 | 0.833 | Mild shrinkage |
| 50 | 0.500 | Slope halved |
| 200 | 0.200 | Strong shrinkage, risk of underfitting |
Procedure: fit on the training set for each in a log-spaced grid, measure the validation error, and keep the with the smallest error. With many inputs, the same procedure applies to the full formula .
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Many correlated inputs | Ordinary weights are unstable and cancel each other | Penalized least squares |
| You want many weights exactly zero | Ridge keeps every input, only shrinking it | Lasso Regression |
| Strength too large | The model underfits, with high bias | Lower on validation data |
| Strength zero with more inputs than examples | The fit is not unique and overfits | Use a positive |
| Inputs on different scales | The penalty treats a large-unit input as cheap and a small-unit one as expensive | Standardise the inputs first |
| Intercept left in the penalty | The mean level of the targets gets shrunk toward zero | Centre the data, or leave the intercept out of the penalty |
| The true relation is curved | A linear model cannot follow it | Kernel Ridge Regression |
| Very many inputs | Inverting a matrix costs roughly operations | Iterative solvers such as Gradient Descent |
The penalty is not free. Shrinking a weight biases its estimate toward zero. That bias is worth paying when it buys a large drop in variance, which is the usual case with noisy or correlated data.
History
Arthur Hoerl and Robert Kennard published the ridge estimator in 1970, in the journal Technometrics. Their paper, "Ridge regression: biased estimation for nonorthogonal problems", was motivated by least squares estimates that swung wildly when the predictors were nearly collinear. Their remedy was to add a constant to the diagonal of the matrix before inverting it, which is exactly the formula above.
A similar idea appeared in the mathematics of ill-posed problems. Andrey Tikhonov developed the general form of this kind of penalty in the 1960s, and the penalty is often called Tikhonov regularization in that setting. Statisticians and mathematicians were, in effect, solving the same problem from two directions.
The idea caught on for two reasons. It is a single parameter with a closed-form answer, which made it cheap to compute before fast hardware. And it has a clean Bayesian meaning, which tied it to probability. Later, neural network researchers reused the same penalty under the name weight decay, and it remains a standard setting today.
Recap
A linear model fits the training data by minimising squared error. Adding to the loss shrinks the weights, and the solution is . The added makes the inverse exist even when the inputs are correlated. You choose with validation data. The natural next steps are Lasso Regression, which penalises the absolute size of weights and so sets some to zero, and Kernel Ridge Regression, which applies the same idea to curved relationships.
Check yourself
QuestionRecall: what are the two terms of the penalized least squares objective, and what does control?Show answer
The first term, , is the sum of squared errors on the training data. The second, , is the penalty on the squared size of the weights. The strength sets the price of a large weight: gives ordinary least squares, and larger values shrink the weights harder.
QuestionComputation: for one input with and , what is the penalized weight when ?Show answer
The formula for one input is . The ordinary weight would be , so the penalty halves it.
QuestionWhy: why does adding make the solution exist even when has no inverse?Show answer
never has negative eigenvalues. A singular matrix has an eigenvalue of zero. Adding with shifts every eigenvalue up by , so none is zero. A matrix with no zero eigenvalue is invertible, so exists and the solution is unique.
QuestionApplied: you have 1000 examples and 500 highly correlated inputs. Ordinary least squares gives coefficients in the hundreds, with pairs of opposite sign. What do you do, and what should happen to those pairs?Show answer
Fit penalized least squares, choosing by cross-validation over a log-spaced grid. The large coefficients should shrink, and the opposite-sign pairs should move toward each other, because the penalty favours spreading weight evenly across correlated inputs. Check that the validation error improves; if it does not, the linear model itself may be the limit.