S14 · Lesson 9 · Linear regression and the bias–variance trade-off
Linear models in basis functions, the normal equations and their regularized form, the effective number of parameters, how regularization trades bias for variance, the best regularization strength for a simple case, and how many features a polynomial basis needs
"Linear" regression can fit curves: the model only has to be linear in its weights, and the inputs can pass first through any fixed set of basis functions, polynomials, sinusoids or Gaussians. That makes the fit a matrix problem with a closed-form answer, and it lets the effect of regularization be worked out exactly. The result is the bias–variance trade-off: a penalty that pulls weights toward zero makes every fit a little wrong in the same direction, but much less sensitive to noise, and the right amount minimizes the total error.
- For a single weight (true value 1) estimated from data with Σx² = S and noise variance σ², the squared bias and the variance of the ridge estimate against λ (log scale), and their sum, the mean squared error.
- With no regularization the estimate is unbiased but noisy. As λ grows, variance falls and bias rises. The total is smallest at .
- More data (larger S) needs less regularization.
Predict
A single weight, truly 1, is estimated with Σx² = 10 and noise variance 4. Can ridge regularization lower its mean squared error below the unregularized 0.4?
The unregularized estimate has variance σ²/S = 0.4 and no bias. Ridge shrinks it by S/(S + λ), adding a squared bias but cutting the variance to . At λ = σ²/w² = 4 the total is σ²/(S + λ) = 4/14 = 0.286. Too much shrinkage, though, drives the estimate to zero and the error back up to 1.
Name it
Linear models
with fixed basis functions (and for the bias). A polynomial basis of degree M in D inputs has terms, which grows quickly with dimension.
The normal equations
Stacking into the design matrix Φ, least squares gives , and with a penalty (ridge regression)
Effective parameters
If has eigenvalues , ridge shrinks each direction by , and the effective number of parameters is : well-determined directions keep their weight, poorly determined ones are suppressed.
Bias and variance
The expected squared error of an estimate splits into squared bias plus variance (plus the irreducible noise, for predictions). For one weight with Σx² = S, ridge gives bias² and variance , minimized in sum at , where the error is .
Worked example
Tabulate the sim's case (S = 10, w = 1, σ² = 4).
Show the solution
- Add squared bias and variance at each λ. With S = 10, w = 1 and σ² = 4:
| λ | bias² | variance | total |
|---|---|---|---|
| 0 | 0 | 0.400 | 0.400 |
| 1 | 0.008 | 0.331 | 0.339 |
| 4 | 0.082 | 0.204 | 0.286 |
| 10 | 0.250 | 0.100 | 0.350 |
| 40 | 0.640 | 0.016 | 0.656 |
- Find the minimum. The total is smallest at λ = 4.
Answer the total error is 0.400, 0.339, 0.286, 0.350 and 0.656 at λ = 0, 1, 4, 10 and 40, with the minimum, 0.286, at λ = 4.
Worked example
A design's has eigenvalues 10, 1 and 0.1, and λ = 1. How many effective parameters does ridge leave?
Show the solution
- Effective parameters.
- Which directions survive. The strongly determined direction survives almost whole, the weakest is nearly removed.
- Counting basis terms. A cubic polynomial in two inputs would have = 10 terms, a quadratic in three inputs = 10, which is why basis expansions struggle in high dimensions.
Answer ridge leaves 1.5 effective parameters.
Worked example
A two-feature fit has and . Find the least-squares weights, and the ridge weights with λ = 1.
Show the solution
- Least squares. The determinant is 4 × 2 − 2² = 4, so
- Ridge. Adding λ = 1 to the diagonal gives , with determinant 11, so
- Which weight shrinks more. The second shrinks more (27% against 9%) because it lies more along the matrix's weak direction, where ridge acts hardest.
Answer least squares gives w = (1, 1), and ridge with λ = 1 gives (0.909, 0.727).
Check
A weight w = 0.5 is estimated by ridge with Σx² = 5 and λ = 20. What's the squared bias?
Check
For the same estimate (Σx² = 5, λ = 20) with noise variance 1, what's the variance?
Check
For a single weight w = 0.5 with noise variance 4, what ridge λ minimizes the mean squared error?
Check
has eigenvalues 5, 0.5 and 0.2. With λ = 5, how many effective parameters remain?
Check
How many terms does a polynomial basis of degree 3 in 2 inputs have (including the constant)?
Check
With Σx² = 5, noise variance 1 and a true weight 2, what mean squared error does the best ridge λ achieve?
Check
A two-weight least-squares problem has with a = 5, b = 2, c = 1.5, and = (6, −1).
(a)
What is ?
(b)
What is ?
Check
A single weight w is estimated by ridge regression with Σx² = S, penalty λ, and noise variance v.
Step 1
What is the estimate's expected value?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is its variance?
Step 3
What is its mean squared error?
Explain why
How can a deliberately biased estimate beat an unbiased one?
An estimate's mean squared error is its squared bias plus its variance. Least squares is unbiased, but when the data are few or noisy its variance can be large, because it chases the noise in every direction. Shrinking the estimate toward zero by a factor introduces a small systematic error, but it reduces the variance by the square of that factor. For a small λ the variance falls faster than the squared bias grows, so the total drops. Only when λ is large does the bias dominate. So the right amount of regularization minimizes the total error, and in Bayesian terms (lesson 10) it's what a sensible prior does.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.