Skip to content
Kudos AI
Lire en français
Statistical Learning Foundations

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.

7 min readKudos AI

Prerequisites: The Bias-Variance Tradeoff

Five points fitted once with the training error read off, then each point held out in turn while the line refits without it, and the five gaps averaged into an estimate three times larger.

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:

  1. 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.
  2. 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 nn observations, hold out a single observation (xi,yi)(x_i, y_i), fit on the remaining n−1n-1, and predict the one left out. Repeat for every ii, then average:

CV(n)=1n∑i=1nMSEi,MSEi=(yi−y^i)2.\mathrm{CV}_{(n)} = \frac{1}{n}\sum_{i=1}^{n} \mathrm{MSE}_i , \qquad \mathrm{MSE}_i = (y_i - \hat y_i)^2 .

Each fit uses n−1n-1 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:

iixix_iyiy_i
112
224
335
444
555

Fitting a simple linear regression to all five gives y^=2.2+0.6x\hat y = 2.2 + 0.6x - the fit derived step by step in Linear Regression from First Principles. LOOCV refits it five times, each time omitting one row:

FoldOmittedPrediction y^i\hat y_iActual yiy_i(yi−y^i)2(y_i - \hat y_i)^2
1(1,2)(1, 2)4.000024.0000
2(2,4)(2, 4)3.142940.7347
3(3,5)(3, 5)3.750051.5625
4(4,4)(4, 4)4.857140.7347
5(5,5)(5, 5)5.500050.2500
CV(5)=4.0000+0.7347+1.5625+0.7347+0.25005=7.28195=1.4564.\mathrm{CV}_{(5)} = \frac{4.0000 + 0.7347 + 1.5625 + 0.7347 + 0.2500}{5} = \frac{7.2819}{5} = 1.4564 .

Compare that with the training MSE of the full fit, which is RSS/n=2.4/5=0.48\mathrm{RSS}/n = 2.4/5 = 0.48. The honest estimate, 1.45641.4564, is three times larger. That gap is exactly the optimism this article opened with.

Notice also fold 1: dropping (1,2)(1,2) - 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.

Python

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.

2345612345x2.0014.000020.734731.562540.734750.2500CV 1.4564train 0.48
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 nn model fits, which is expensive when nn is large or fitting is slow. k-fold cross-validation splits the observations into kk groups of roughly equal size, holds out each group in turn, and averages:

CV(k)=1k∑j=1kMSEj.\mathrm{CV}_{(k)} = \frac{1}{k}\sum_{j=1}^{k} \mathrm{MSE}_j .

LOOCV is the special case k=nk = n. Using k=5k = 5 or k=10k = 10 needs only 5 or 10 fits instead of nn.

F. Why k=5 or k=10, and not k=n

The obvious reading is that smaller kk is a pure computational compromise. It is not - there is a bias-variance tradeoff in the estimate itself.

Bias favours large kk. Each LOOCV fit uses n−1n-1 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 kk. In LOOCV the nn 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 k=5k = 5 or 1010 the training sets overlap less, the errors are less correlated, and the average is more stable.

Following James et al., k=5k = 5 and k=10k = 10 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 nn single-point holdouts: no split randomness, little bias. On our five-point dataset it gave 1.45641.4564 against a training MSE of 0.480.48.
  • k-fold with k=5k = 5 or 1010 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 y^=2.2+0.6x\hat y = 2.2 + 0.6x 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.

Related reading

4 min readTime Series

A Score That Loses to Doing Nothing

A five-nearest-neighbour model scores 0.9983 under random five-fold cross-validation on a random walk, a series whose increments are by construction unpredictable. Evaluated forward in time it scores 0.6559 with an RMSE 12.44 times larger, and loses to carrying the last observed value forward. The split, not the model, produced the first number.

StatisticsMachine Learning
7 min readStatistical Learning Foundations

The Bias-Variance Tradeoff

The exact decomposition of expected test error into squared bias, variance, and irreducible noise, demonstrated numerically with a 2,000-run simulation where all three terms are measured separately and shown to add up.

StatisticsMachine LearningMathematics
6 min readUnsupervised Learning

The Direction That Changes When You Change Units

Twelve people, two measurements, and three different first principal components: in millimetres the answer is almost pure height, in metres almost pure weight, and in centimetres an even blend - with the correlation fixed at 0.9500 throughout. What that says about what PCA maximises, why a proportion of variance explained of 99.999% can be a statement about metres rather than about people, and what standardising actually chooses.

Machine LearningStatistics
← Back to all articles