S1412 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S14 · Lesson 12 · Model selection

Choosing a model's complexity without fooling yourself, by k-fold and leave-one-out cross-validation with the leverage shortcut, by AIC and BIC, and by the Bayesian evidence with its Occam factor and re-estimated hyperparameters

Lesson 1 showed that training error keeps falling as a model grows, so it can't tell you when to stop. Three tools can: cross-validation, which measures error on data each fit didn't see; information criteria like AIC and BIC, which charge for parameters; and the Bayesian evidence, which charges automatically, through how thinly a flexible model has to spread its predictions. Each also tunes hyperparameters such as a ridge penalty or a prior's width.

  • The curve is the log evidence of a one-weight model t=wx+ϵ against its prior precision α, for 10 points with Σx² = 10, noise precision 4, and Σxt on the slider.
  • A tiny α (a very broad prior) is penalized: the model spreads its predictions over slopes the data rule out. A huge α (a prior pinned at zero) can't fit the data.
  • The peak sits near α=1/w2, where the prior's width matches the slope the data support. With Σxt = 0 there's no slope to support, and the evidence prefers switching the weight off.

Predict

A least-squares fit leaves a residual of 0.3 at a point whose leverage (its own weight in its own fitted value) is 0.6. If that point were left out of the fit, what would its residual be?

Name it

Cross-validation

Hold out part of the data, fit on the rest, and measure error on what was held out. k-fold cross-validation splits the data into k parts and fits k times, each time holding out one part; every example is tested once, by a model that didn't see it. Leave-one-out is the case k = N.

The leverage shortcut

Least squares and ridge regression predict 𝐭^=H𝐭 with the hat matrix H=Φ(ΦTΦ+λI)−1ΦT. The leave-one-out residual of example i is

ei1−hii

The average leverage is tr(H)/N=peff/N, with peff the effective number of parameters of lesson 9. Generalized cross-validation replaces each hii by the average: GCV=e2¯/(1−peff/N)2.

Information criteria

With ln⁡L the maximized log-likelihood and M parameters, choose the model with the largest

AIC: ln⁡L−M,BIC: ln⁡L−M2ln⁡N

(often written as 2M−2ln⁡L and Mln⁡N−2ln⁡L, to be minimized). BIC's penalty grows with N, so with much data it prefers smaller models than AIC.

The evidence

A model's evidence is the probability it gave the data, averaged over its prior: p(D)=∫p(D|𝐰)p(𝐰)d𝐰. When the posterior is much narrower than the prior,

ln⁡p(D)≈ln⁡p(D|𝐰MAP)+Mln⁡ΔwposteriorΔwprior

The second term, the Occam factor, is negative and grows with the number of parameters and the breadth of their priors: complexity is charged without a separate rule.

Re-estimating hyperparameters

Maximizing the evidence of Bayesian linear regression (lesson 10) over α gives

α=γ𝐦NT𝐦N,γ=∑iβλiα+βλi

with λi the eigenvalues of ΦTΦ and γ the number of well-determined parameters; iterate with 𝐦N recomputed. For one weight the fixed point is α=S2/(T2−S/β), with S = Σx² and T = Σxt, and if T2≤S/β the evidence switches the weight off.

Worked example

A model with 3 parameters reaches ln⁡L = −120.0 on 200 examples; one with 6 reaches −116.5. Which do AIC and BIC choose?

Show the solution
  1. AIC. small=−120−3=−123.0large=−116.5−6=−122.5 so AIC takes the larger model.
  2. BIC. small=−120−1.5ln⁡200=−127.9large=−116.5−3ln⁡200=−132.4 so BIC takes the smaller.
  3. Why they differ. Three extra parameters bought 3.5 nats; AIC charges 1 nat each, BIC 2.65 at N = 200.
  4. Settling it. When they disagree, cross-validation on the actual task settles it.

Answer AIC chooses the 6-parameter model, and BIC the 3-parameter one.

Worked example

Find the evidence-optimal prior precision for a one-weight model with Σx² = 10, Σxt = 10 and β = 4, by iterating from α = 1.

Show the solution
  1. First pass, at α = 1. A=1+40=41m=4×10/41=0.976γ=40/41=0.976
  2. New α. α=γ/m2=1.025
  3. Converge. Repeating changes it only in the fourth digit, converging to α=S2T2−S/β=100/97.5=1.026
  4. Compare the slope. That's close to 1/wML2 = 1: the evidence picks a prior about as wide as the slope it has to explain, slightly narrower because the measured slope includes some noise.

Answer the evidence-optimal prior precision is α = 1.026.

Worked example

A model's single parameter has a prior 10 units wide; after the data its posterior is 0.1 wide. What's its Occam factor, and how does it change if the prior is 1 unit wide and the fit is equally good?

Show the solution
  1. Prior 10 units wide. ln⁡(0.1/10)=−4.6nats
  2. Prior 1 unit wide. ln⁡(0.1/1)=−2.3nats
  3. What the evidence charges for. A prior a hundred times wider than the posterior means the model assigned most of its prior probability to parameter values the data then ruled out, and the evidence charges for that.
  4. Compare. The model with the more precise prior wins by 2.3 nats, a factor of 10 in evidence, if both fit the data equally well.

Answer the Occam factor is −4.6 nats with the 10-unit prior and −2.3 nats with the 1-unit prior.

Check

A least-squares fit with 10 parameters uses 50 points. One point has residual 1 and leverage 0.75.

(a)

What is the average leverage?

(b)

What is that point's leave-one-out residual?

Check

A ridge fit on 100 points has 2 effective parameters and a mean squared training residual of 2.

(a)

What is its generalized cross-validation score?

Check

A model with 3 parameters reaches a maximized log-likelihood of −500 on 10000 examples.

(a)

What is its AIC score (in the form ln L − M)?

(b)

Its BIC score (ln L − (M/2) ln N)?

Check

A larger model has 5 more parameters than a smaller one and raises the maximized log-likelihood by 3 nats, on 100000 examples.

(a)

By how much does it change the BIC score (positive favors the larger model)?

Check

Each of a model's 4 parameters has a prior 2 units wide and a posterior 0.5 wide.

(a)

What is the log of its Occam factor?

Check

A one-weight model has Σx² = 5 and Σxt = 8, with noise precision 1.

(a)

What prior precision maximizes its evidence?

(b)

How many well-determined parameters γ does it have there?

Check

Two models fit the data equally well, but one gives its parameters a far broader prior. Why does the evidence prefer the other?

Check

A one-weight model has Σx² = S and Σxt = T, with noise precision β, and its prior precision is set by maximizing the evidence.

Step 1

What is the maximum-likelihood weight?

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°.

Check

Write ridge1(xs, ts, lam) for one weight through the origin, cv_error(xs, ts, lam, k): the mean squared error of k-fold cross-validation with contiguous folds, and loo_shortcut(xs, ts, lam): the leave-one-out error from a single fit.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why can't training error choose a model's complexity, while cross-validation and the evidence can?

Practice

Keep practicing

9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.