Cross-Validation and Resampling
Why training error is a biased estimate of test error, and how the validation set, leave-one-out, and k-fold approaches fix it, with a five-fold LOOCV computation worked out observation by observation.
Prerequisites: The Bias-Variance Tradeoff
The Bias-Variance Tradeoff ended with a diagnostic table that compares training error against test error. That comparison assumes you have a test error. Usually you do not: every observation you own has already been spent on fitting. Resampling methods solve this by repeatedly refitting the model to subsets of the data, and using the held-out parts to estimate performance on data the model has not seen.
A. Why training error is not an estimate of test error
Training error is measured on the same observations used to choose the parameters, so the fitting process has already adapted to whatever noise those points carry. It is therefore biased downward as an estimate of performance on new data, and the bias grows with model flexibility.
That is not a small correction you can ignore. It is precisely the quantity that made the degree-15 polynomial look best in the previous articles while being the worst model of the three. A selection procedure driven by training error will reliably choose the most flexible candidate available.
B. The validation set approach
The simplest fix: split the observations at random into a training set and a validation set, fit on the first, and measure error on the second. Since the validation observations played no part in fitting, the resulting error is an honest estimate.
It has two well-known drawbacks:
- The estimate is highly variable. It depends on which observations happened to land in the validation set. A different split gives a noticeably different number.
- It wastes data. Only a subset is used for fitting, and statistical methods generally perform worse on fewer observations, so the validation error tends to overestimate the error of the model you would fit on the full dataset.
C. Leave-one-out cross-validation
LOOCV addresses both. With observations, hold out a single observation , fit on the remaining , and predict the one left out. Repeat for every , then average:
Each fit uses observations, so there is far less upward bias than a half-sized training set, and because it averages over every possible single-point holdout there is no randomness in the split at all - run it twice and get the same answer.
D. LOOCV worked out completely
The dataset is small enough to do every fold explicitly. Five observations, one predictor:
| 1 | 1 | 2 |
| 2 | 2 | 4 |
| 3 | 3 | 5 |
| 4 | 4 | 4 |
| 5 | 5 | 5 |
Fitting a simple linear regression to all five gives - the fit derived step by step in Linear Regression from First Principles. LOOCV refits it five times, each time omitting one row:
| Fold | Omitted | Prediction | Actual | |
|---|---|---|---|---|
| 1 | 4.0000 | 2 | 4.0000 | |
| 2 | 3.1429 | 4 | 0.7347 | |
| 3 | 3.7500 | 5 | 1.5625 | |
| 4 | 4.8571 | 4 | 0.7347 | |
| 5 | 5.5000 | 5 | 0.2500 |
Compare that with the training MSE of the full fit, which is . The honest estimate, , is three times larger. That gap is exactly the optimism this article opened with.
Notice also fold 1: dropping - the point furthest below the line - lets the remaining four pull the fit up, and the prediction misses by 2.0 for a squared error of 4.0, more than half the total. With only five observations, one influential point dominates. This is a genuine property of the estimate, not a defect in the method.
Runs in your browser. The first run downloads the Python runtime (~10 MB), then it is cached.
Running it reproduces the table row for row and prints LOOCV MSE: 1.4564,
confirming the hand computation.
Interactive: leave one out, five times
Each fold refits the line without one point.
- Held out
- (1, 2)
- Refitted line
- ŷ = 3.80 + 0.20x
- Prediction
- 4.0000
- Squared error
- 4.0000
- LOOCV estimate CV(5)
- 1.4564
- Training MSE
- 0.48
Fold 1 holds out (1, 2). The other four points refit the line to ŷ = 3.80 + 0.20x, which predicts 4.0000 where the truth is 2: a miss of 2.0000 and a squared error of 4.0000, which is 54.9% of the five-fold total. Dropping the point furthest below the line lets the remaining four pull the fit up, so this one fold carries more than half of the estimate. With five observations, one influential point dominates.
E. k-fold cross-validation
LOOCV requires model fits, which is expensive when is large or fitting is slow. k-fold cross-validation splits the observations into groups of roughly equal size, holds out each group in turn, and averages:
LOOCV is the special case . Using or needs only 5 or 10 fits instead of .
F. Why k=5 or k=10, and not k=n
The obvious reading is that smaller is a pure computational compromise. It is not - there is a bias-variance tradeoff in the estimate itself.
Bias favours large . Each LOOCV fit uses observations, almost the full dataset, so it barely overestimates the error. Five-fold fits use only 80% of the data, so they overestimate somewhat more.
Variance favours moderate . In LOOCV the fitted models are trained on almost identical data - any two differ in just two observations - so their errors are highly positively correlated. Averaging strongly correlated quantities reduces variance far less than averaging weakly correlated ones. With or the training sets overlap less, the errors are less correlated, and the average is more stable.
Following James et al., and have been shown empirically to give test-error estimates that suffer from neither excessive bias nor very high variance. That is the reason for the convention, and it is an empirical finding rather than a theorem.
G. Two ways to get this wrong
Selecting features before cross-validating. If you screen predictors using the whole dataset and then cross-validate the model built from the survivors, the held-out folds already influenced which features were chosen. The estimate will be optimistic, sometimes dramatically. Every data-dependent choice must happen inside the loop.
Cross-validating dependent observations with a random split. The method assumes held-out observations are independent of the training ones. With time series, repeated measurements on the same subject, or grouped data, a random split leaks information across the boundary. Use a split that respects the structure - forward-chaining for time, grouped folds for clusters.
For classification, everything above carries over with the error measure changed from squared error to the misclassification rate; the logic of the folds is identical.
Key takeaways
- Training error is biased downward as an estimate of test error, and worsens with flexibility, so it cannot be used to select models.
- A validation split is honest but high-variance and wastes data.
- LOOCV averages single-point holdouts: no split randomness, little bias. On our five-point dataset it gave against a training MSE of .
- k-fold with or is the standard compromise - cheaper than LOOCV and empirically better because less correlated fits average more effectively.
- Every data-dependent decision must sit inside the resampling loop, and the fold structure must respect dependence in the data.
What's next
We have used a linear fit as a running example three times now without deriving it. That is the gap to close: Linear Regression from First Principles builds the least-squares estimator from scratch and shows where came from.
References & further reading
- Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani, An Introduction to Statistical Learning, with Applications in R, Springer (Springer Texts in Statistics 103), 2013source ↗
Copyrighted works are cited for reference only and are not hosted here; please consult the publisher for access.