Fit curves of rising degree to eight points. Watch the error on those points fall to zero while the error on new points climbs.
A curve that fits too well
Polynomials of degree 1 to 7 fitted to eight noisy points from a parabola (seed 9300); errors averaged over 200 such datasets.
Eight points from a smooth parabola plus noise. Fit polynomials of rising degree; below, the errors on the eight points and on 200 new ones, averaged over 200 such datasets.
Predict first. As the degree rises from 1 to 7, what will the two errors do?
Describe this picture
Two panels. The first holds eight training points at , drawn from the parabola , shown dashed, plus noise of standard deviation 0.5 (seed 9300), and a fitted polynomial curve. The second draws two error curves against the degree: the mean squared error on the eight training points, and on 200 fresh points from the same source, each averaged over 200 datasets built the same way (seeds 9300 to 9499), with a dashed line at the noise level, 0.25. Before the clip a question asks what the two errors will do as the degree rises from 1 to 7: both fall; the training error falls while the error on new points falls, then rises; or both rise. A tap picks one and starts the clip. In the clip, over 10 seconds, the degree steps from 1 to 7, each step a short morph, and both error curves grow by one point per step. The curve bends towards each point, and at degree 7 it passes through all eight with large swings between them. The average training error falls to 0; the average error on new points falls from 0.6361 to its lowest, 0.3299 at degree 2, then climbs to 0.7846 at degree 7. Readouts give the degree and both average errors. After the clip a slider sets the degree; it starts at 7.
Training error and error on new points
Before the clip you guessed. The training error only falls as the degree rises, down to 0 at degree 7, where eight coefficients can pass a curve through eight points exactly. Averaged over 200 datasets like the one in the picture, the error on new points falls from 0.6361 at degree 1 to its lowest, 0.3299, at degree 2, the true shape, and then rises, to 0.7846 at degree 7. One dataset alone can mislead: the one drawn here happens to fit best at degree 5, because its first point landed almost three noise standard deviations low.
A degree-7 curve does not learn the parabola. It learns the parabola plus the noise in these eight points, and the noise will not be there next time. Fitting the noise along with the signal is overfitting.
Watch out
The model with the lowest error on its own data is not the best model. A flexible enough model can always reach zero error there. What matters is the error on data it has not seen.
Training error against test error
A polynomial of degree has coefficients, and fitting it is a least-squares problem with columns, as in Least squares. Each extra column can only lower the training error; with equal to the number of points it reaches 0.
How much does the training error flatter the model? Suppose the model’s form contains the truth, the values are fixed, and the noise has variance . Then on average, per point,
The gap between them, , is the optimism of the training error. It grows with every coefficient.
Holding points back: cross-validation
The honest check is to test a model on points it was not fitted to. With few data, take turns: leave one point out, fit to the rest, and see how badly the fit misses the point left out. Do this for every point and average the squared misses:
where is the prediction at of the model fitted without point . This is leave-one-out cross-validation.
Leave one out
Four points; each held out in turn, a line and then a parabola fitted to the other three.
Four points. Hold one out, fit to the other three, and see how far the fit misses the one it never saw.
Describe this picture
Four points at with , the data of the least-squares page of the linear algebra course. In the clip each point is held out in turn and ringed: a line, then a parabola, is fitted to the other three and drawn across the panel, and the miss at the held-out point is drawn as a square. Readouts add up the squares as they come: 41.25 for the line and 2.22 for the parabola. The panel has a play button and no other control.
Fitted to all four points, the parabola misses by less than the line, but any curve with more coefficients would too. Held out, the comparison is fair: the parabola’s squares add to 2.22 and the line’s to 41.25. The parabola predicts points it has not seen far better.
A price for complexity: AIC
Cross-validation refits the model many times. A cheaper rule adds a fixed price for each coefficient to the training fit. For bell-shaped noise, with constants dropped,
and the model with the smallest wins. The first term rewards a good fit; the second charges 2 for each coefficient. The rule works when is well above the number of coefficients; it breaks down when the fit is exact, because the logarithm of 0 is not defined.
Shrinking instead: ridge
Instead of choosing a degree, keep many coefficients but pull them towards 0. Ridge regression minimises
with the intercept left out of the penalty. Here is a penalty weight, not the false-alarm chance of the testing pages. With it is ordinary least squares; as grows, the curve flattens towards a horizontal line at the mean of . The penalty plays the part of the degree, and cross-validation is the usual way to set it. This is the shrinkage of Estimators: bias and variance: a little bias for much less scatter.
Worked example
1. Fitting the four points. The line leaves , the parabola , the cubic 0: a cubic passes through any four points.
2. Leave-one-out. For the line, the predictions at the held-out points are , , and , missing by , , and : squares adding to 41.25, mean 10.31. For the parabola, which passes exactly through each set of three, the predictions are , , and , missing by , , and : squares adding to 2.222, mean 0.5556. The parabola predicts held-out points about twenty times better. The cubic cannot be cross-validated here: three points cannot fix four coefficients.
3. AIC. Line: . Parabola: . Lower wins: the parabola.
4. Optimism. With points, noise and a parabola (): expected training error , expected error on new at the same , a gap of . At degree 7 the training error is 0 and the error on new at the same is . Between the points, where the degree-7 curve swings, it is worse still.
Try it yourself. A cubic fitted to 4 points has training error 0. A very large ridge penalty flattens the fit to the mean of .
Where you’ll meet this
Choosing a model’s size. The order of an autoregressive model, the number of taps in a filter, the number of features in a predictor: each is a degree to choose, and each is chosen by held-out error or a criterion like AIC.
Machine learning. A trained model should be checked on data held out from training, because its training error is optimistic in exactly the way shown here.
Smoothing. How wide a smoothing window to use, in Smoothing and seasonality, is the same question: too narrow follows the noise, too wide loses the shape.
In signal processing · choosing a model order
Linear prediction fits an autoregressive model whose error power keeps falling as the order grows; AIC adds a price per coefficient and picks the order where the total is smallest. Linear prediction in the signal processing course makes that choice.
Where this leads · ridge and conditioning
Polynomial columns are nearly dependent, so high-degree fits are badly conditioned. Ridge regression is the damped pseudo-inverse of the linear algebra course, which shrinks each direction by , with its singular value.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Degree | coefficients | training error 0 once |
| Training error | per point on average | optimistic |
| New , same | gap | |
| Leave-one-out | refit without each point | |
| AIC | smaller is better | |
| Ridge | minimise | penalty in place of degree |
| Rule | judge a model on data it has not seen |