---
title: Linear regression and the bias–variance trade-off
heading: 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
eyebrow: S14 · Lesson 9 · Linear regression and the bias–variance trade-off
xp: 50
---

::: hook
"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.
:::

::: play
- 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 $\hat{w} = \sum xt/(S + \lambda)$ 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 $\lambda = \sigma^2/w^2$.
- More data (larger S) needs less regularization.
:::

```sim plot
view: graph
x: l = 0.01..100 log
y: E = 0..0.8
slider: S = 2..50 step 1 = 10
slider: v = 0.5..10 step 0.5 = 4
plot: (l/(S + l))^2 | samples: 400 | label: squared bias
plot: v*S/(S + l)^2 | samples: 400 | color: ch3 | label: variance
plot: (l/(S + l))^2 + v*S/(S + l)^2 | samples: 400 | color: ch4 | label: mean squared error
readout: best λ = v | digits: 3
readout: error at the best λ = v/(S + v) | digits: 4
readout: error without regularization = v/S | digits: 4
```

::: 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?

- [ ] No: least squares is unbiased, so it's best
- [x] Yes, to about 0.29 at λ = 4
- [ ] Yes, to zero with large enough λ
- [ ] Only if the true weight is zero

::: reveal
The unregularized estimate has variance σ²/S = 0.4 and no bias. Ridge shrinks it by S/(S + λ), adding a squared bias $(\lambda w/(S + \lambda))^2$ but cutting the variance to $\sigma^2S/(S + \lambda)^2$. 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.
:::
:::

::: nameit
## Linear models

$$y(\mathbf{x}, \mathbf{w}) = \sum_{j=0}^{M-1}w_j\phi_j(\mathbf{x}) = \mathbf{w}^T\boldsymbol{\phi}(\mathbf{x})$$

with fixed basis functions $\phi_j$ (and $\phi_0 = 1$ for the bias). A polynomial basis of degree M in D inputs has $\binom{M + D}{D}$ terms, which grows quickly with dimension.

## The normal equations

Stacking $\boldsymbol{\phi}(\mathbf{x}_n)^T$ into the design matrix Φ, least squares gives $\mathbf{w} = (\Phi^T\Phi)^{-1}\Phi^T\mathbf{t}$, and with a penalty $\frac{\lambda}{2}\|\mathbf{w}\|^2$ (ridge regression)

$$\mathbf{w} = (\Phi^T\Phi + \lambda I)^{-1}\Phi^T\mathbf{t}$$

## Effective parameters

If $\Phi^T\Phi$ has eigenvalues $d_i$, ridge shrinks each direction by $d_i/(d_i + \lambda)$, and the **effective number of parameters** is $\sum_id_i/(d_i + \lambda)$: 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² $(\lambda w/(S + \lambda))^2$ and variance $\sigma^2S/(S + \lambda)^2$, minimized in sum at $\lambda = \sigma^2/w^2$, where the error is $\sigma^2/(S + \lambda)$.
:::

::: example
## Tabulate the sim's case (S = 10, w = 1, σ² = 4).

::: solution
1. **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 |

2. **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.
:::
:::

::: example
## A design's $\Phi^T\Phi$ has eigenvalues 10, 1 and 0.1, and λ = 1. How many effective parameters does ridge leave?

::: solution
1. **Effective parameters.**
   $$\begin{aligned} &\frac{10}{11} + \frac{1}{2} + \frac{0.1}{1.1} \\ &= 0.909 + 0.500 + 0.091 \\ &= 1.5 \end{aligned}$$
2. **Which directions survive.** The strongly determined direction survives almost whole, the weakest is nearly removed.
3. **Counting basis terms.** A cubic polynomial in two inputs would have $\binom{5}{2}$ = 10 terms, a quadratic in three inputs $\binom{5}{3}$ = 10, which is why basis expansions struggle in high dimensions.

**Answer:** ridge leaves 1.5 effective parameters.
:::
:::

::: example
## A two-feature fit has $\Phi^T\Phi = \begin{bmatrix}4 & 2 \\ 2 & 2\end{bmatrix}$ and $\Phi^T\mathbf t = (6, 4)$. Find the least-squares weights, and the ridge weights with λ = 1.

::: solution
1. **Least squares.** The determinant is 4 × 2 − 2² = 4, so
   $$\begin{aligned} \mathbf w &= \frac14\begin{bmatrix}2 & -2 \\ -2 & 4\end{bmatrix}(6, 4) \\ &= (1, 1) \end{aligned}$$
2. **Ridge.** Adding λ = 1 to the diagonal gives $\begin{bmatrix}5 & 2 \\ 2 & 3\end{bmatrix}$, with determinant 11, so
   $$\begin{aligned} w_1 &= \frac{3 \times 6 - 2 \times 4}{11} \\ &= 0.909 \\ w_2 &= \frac{-2 \times 6 + 5 \times 4}{11} \\ &= 0.727 \end{aligned}$$
3. **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
given: S = 5 | 20; w = 0.5 | 3; v = 1 | 4; l = 1 | 5 | 20
answer: (l*w/(S + l))^2
tolerance: 0.5%

## A weight w = {w} is estimated by ridge with Σx² = {S} and λ = {l}. What's the squared bias?

::: right
Right. $(\lambda w/(S + \lambda))^2$ = {answer}.
:::

::: misses
- [l*w/(S + l)] That's the bias; square it.
- [any] Not yet. The estimate's mean is $wS/(S + \lambda)$.
:::
:::

::: check
given: S = 5 | 20; v = 1 | 4; l = 1 | 5 | 20
answer: v*S/(S + l)^2
tolerance: 0.5%

## For the same estimate (Σx² = {S}, λ = {l}) with noise variance {v}, what's the variance?

::: right
Right. $\sigma^2S/(S + \lambda)^2$ = {answer}.
:::

::: misses
- [v/(S + l)] The variance is $\sigma^2S/(S + \lambda)^2$, which is smaller still.
- [any] Not yet. Variance of $\sum xt/(S + \lambda)$.
:::
:::

::: check
given: v = 0.5 | 4 | 10; w = 0.5 | 2 | 3
answer: v/w^2
tolerance: 0.5%

## For a single weight w = {w} with noise variance {v}, what ridge λ minimizes the mean squared error?

::: right
Right. $\sigma^2/w^2$ = {answer}.
:::

::: misses
- [v/w] Use $w^2$.
- [any] Not yet. Set the derivative of bias² + variance to zero.
:::
:::

::: check
given: a = 5 | 20; b = 0.5 | 2; c = 0.01 | 0.2; l = 0.1 | 1 | 5
answer: a/(a + l) + b/(b + l) + c/(c + l)
tolerance: 0.5%

## $\Phi^T\Phi$ has eigenvalues {a}, {b} and {c}. With λ = {l}, how many effective parameters remain?

::: right
Right. $\sum d_i/(d_i + \lambda)$ = {answer}.
:::

::: misses
- [any] Not yet. Each direction is shrunk by $d_i/(d_i + \lambda)$.
:::
:::

::: check
given: M = 2 | 3 | 5; D = 2 | 3 | 10
answer: (M + D)!/(M!*D!)
tolerance: 0.5%

## How many terms does a polynomial basis of degree {M} in {D} inputs have (including the constant)?

::: right
Right. $\binom{M + D}{D}$ = {answer}.
:::

::: misses
- [(M + 1)^D] That counts every power of every input up to M, including products of too high a total degree.
- [any] Not yet. Monomials of total degree at most M.
:::
:::

::: check
given: v = 1 | 4; S = 5 | 20; w = 0.5 | 2
answer: v/(S + v/w^2)
tolerance: 0.5%

## With Σx² = {S}, noise variance {v} and a true weight {w}, what mean squared error does the best ridge λ achieve?

::: right
Right. $\sigma^2/(S + \lambda^*)$ with $\lambda^* = \sigma^2/w^2$: {answer}.
:::

::: misses
- [v/S] That's the unregularized error.
- [any] Not yet. Substitute the best λ.
:::
:::

::: check
kind: parts
given: a = 3 | 5 | 10; b = 2 | 4; c = 0.5 | 1 | 1.5; u = 2 | 6; v = -1 | 2.5

## A two-weight least-squares problem has $\Phi^T\Phi = \begin{bmatrix}a & c \\ c & b\end{bmatrix}$ with a = {a}, b = {b}, c = {c}, and $\Phi^T\mathbf t$ = ({u}, {v}).

::: part
answer: (b*u - c*v)/(a*b - c^2)
tolerance: 0.5%

### What is $w_1$?

::: misses
- [u/a] The features are correlated; solve both equations together.
- [any] Invert the 2 × 2 matrix.
:::
:::

::: part
answer: (a*v - c*u)/(a*b - c^2)
tolerance: 0.5%

### What is $w_2$?

::: misses
- [v/b] Solve both equations together.
- [any] Invert the 2 × 2 matrix.
:::
:::
:::

::: check
kind: derivation
vars: w = -3..3, S = 1..100, l = 0.01..50, v = 0.01..10

## A single weight w is estimated by ridge regression with Σx² = S, penalty λ, and noise variance v.

::: step
answer: S*w/(S + l)

### What is the estimate's expected value?
:::

::: step
answer: v*S/(S + l)^2

### What is its variance?
:::

::: step
answer: (l*w/(S + l))^2 + v*S/(S + l)^2

### What is its mean squared error?
:::

::: right
Right. The bias grows with λ while the variance falls, and the sum is smallest at λ = v/w².
:::
:::

::: explain
## How can a deliberately biased estimate beat an unbiased one?

::: model
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 $S/(S + \lambda)$ 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

::: check
given: v = 0.1 | 20; w = 0.2 | 5
answer: v/w^2
tolerance: 0.5%

## Best λ: σ² = {v}, w = {w}?

::: misses
- [any] $\sigma^2/w^2$.
:::
:::

::: check
given: a = 1 | 50; b = 0.2 | 5; l = 0.05 | 10
answer: a/(a + l) + b/(b + l)
tolerance: 0.5%

## Effective parameters: eigenvalues {a}, {b}, λ = {l}?

::: misses
- [any] $\sum d/(d + \lambda)$.
:::
:::

::: check
given: M = 1 | 6; D = 1 | 8
answer: (M + D)!/(M!*D!)
tolerance: 0.5%

## Polynomial terms: degree {M}, {D} inputs?

::: misses
- [any] $\binom{M + D}{D}$.
:::
:::
:::
