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 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 , 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?
For least squares and ridge regression the leave-one-out residual is exactly : 0.3/0.4 = 0.75. A high-leverage point pulls the fit toward itself, so its training residual understates how badly the model predicts it. The shortcut gives leave-one-out cross-validation from a single fit.
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 with the hat matrix . The leave-one-out residual of example i is
The average leverage is , with the effective number of parameters of lesson 9. Generalized cross-validation replaces each by the average: .
Information criteria
With the maximized log-likelihood and M parameters, choose the model with the largest
(often written as and , 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: . When the posterior is much narrower than the prior,
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
with the eigenvalues of and γ the number of well-determined parameters; iterate with recomputed. For one weight the fixed point is , with S = Σx² and T = Σxt, and if the evidence switches the weight off.
Worked example
A model with 3 parameters reaches = −120.0 on 200 examples; one with 6 reaches −116.5. Which do AIC and BIC choose?
Show the solution
- AIC. so AIC takes the larger model.
- BIC. so BIC takes the smaller.
- Why they differ. Three extra parameters bought 3.5 nats; AIC charges 1 nat each, BIC 2.65 at N = 200.
- 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
- First pass, at α = 1.
- New α.
- Converge. Repeating changes it only in the fourth digit, converging to
- Compare the slope. That's close to = 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
- Prior 10 units wide.
- Prior 1 unit wide.
- 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.
- 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°.
Step 2
What prior precision maximizes the evidence?
Step 3
What is the variance of the maximum-likelihood weight?
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?
Training error is measured on the same data the model was fitted to, so a more flexible model always matches it at least as well, including its noise; it keeps falling as complexity grows. Cross-validation measures error only on examples each fit didn't see, so fitting noise is punished: the error falls while added flexibility captures real structure and rises once it starts memorizing. The evidence gets the same effect differently: a flexible model must spread its prior probability over many possible data sets, so it gives less probability to the one observed unless its flexibility was needed to explain it.
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.