Cross-Validation: Theory & Pitfalls
Bias, variance and selection bias of cross-validation estimates.
Cross-validation is the most widely used error estimator in machine learning, and one of the least understood. It does not estimate the error of the model you will deploy; its pessimistic bias is exact and predictable; its variance cannot be estimated without bias; and minimizing it over many candidates manufactures optimism. This lesson proves each of these facts and shows what to do about them.
What you’ll learn
- Prove that -fold cross-validation is unbiased for the expected error of the learner at sample size , and explain why it tracks the average error over training sets rather than the error of the model at hand.
- Derive the leave-one-out shortcut for penalized least squares by a self-consistency argument, and generalized cross-validation.
- Decompose the variance of -fold CV into three covariance terms, show that the naive variance estimate is biased by , and prove that no quadratic unbiased estimator exists.
- Quantify the selection bias of the minimum CV error, and explain why leave-one-out can fail to identify the true model.
What cross-validation estimates
Let be i.i.d. examples, a learning algorithm, and split at random (independently of the data) into folds of size . Two targets compete:
- the conditional error , the risk of the model you will deploy;
- the expected error , the average risk of the learner over training sets of size .
Take example in fold . Its loss is computed with , trained on the other examples, which are independent of . Condition on :
Take expectations over , an i.i.d. sample of size , and average over . Every term has the same expectation:
So the bias of as an estimate of is exactly , a slice of the learning curve: positive (pessimistic) when the curve is falling, largest for small and steep curves.
As an estimate of the conditional error , cross-validation does worse. Bates, Hastie & Tibshirani (2024) proved for ordinary least squares that cross-validation does not estimate the error of the model at hand: it "estimates the average prediction error of models fit on other unseen training sets drawn from the same population," and the same holds for data splitting, the bootstrap and Mallows' . The intuition: the CV estimate is built from other fits, so it carries little information about the particular luck of the full-data fit.
Leave-one-out in one fit, and GCV
For penalized least squares, with and a unique minimizer whose fitted values are linear in the labels, (least squares, ridge, smoothing splines), the leave-one-out fits come for free.
Let minimize the criterion without example , and let be with replaced by . For every , dropping the -th term can only lower the criterion, and is optimal for the rest:
The right side equals the full criterion at with labels , because its -th residual is zero. So is the fit to , and its prediction at is the -th entry of :
Solve for and subtract from . One fit gives every leave-one-out residual:
For least squares this is Allen's PRESS statistic. Generalized cross-validation replaces each by the average (Craven & Wahba, 1978; Golub, Heath & Wahba, 1979 for ridge):
GCV is invariant under rotations of the data, which keeps a few high-leverage points from dominating it, and to first order it is with and the training error standing in for .
-NN regression, where each training point counts itself among its neighbors, is a linear smoother with . What does the formula actually compute for it?
- The exact leave-one-out residual of -NN
- The leave-one-out residual of -NN: the average of the other points in the original neighborhood
- The training residual of -NN
- The leave-one-out residual of -NN
Show the answer
B. The leave-one-out residual of -NN: the average of the other points in the original neighborhood
is the average of the other neighbors. True leave-one-out -NN would bring in the -th neighbor. The shortcut needs the lemma, which holds for minimizers of a penalized least-squares criterion; -NN is not one, because removing a point changes the neighborhoods.
The variance of K-fold cross-validation
Write for the held-out losses , with per fold. By exchangeability, every has mean and variance ; two losses in the same fold have covariance (they share a model); two in different folds have covariance (their training sets overlap in points). Counting pairs,
Bengio & Grandvalet (2004) built their analysis on this structure: the covariance matrix of the errors has only three distinct eigenvalues.
Let be the mean loss on fold , with and . Then and
The naive estimate treats the folds as independent: . For equicorrelated variables the sample variance has mean , so
Overlapping training sets typically make , so the naive estimate is too small, and the "standard errors" printed by most CV routines understate the uncertainty.
Write the covariance of with the block matrix of ones within folds and the all-ones matrix, and take expectations of :
Unbiasedness must hold whatever the mean loss is, so , which also kills the term. What remains has to match for all :
Matching gives ; matching then gives ; matching gives . Contradiction: within one run of -fold CV, the between-fold covariance is invisible.
What can be done instead? Nadeau & Bengio (2003) inflate the naive variance with a correction for the overlap between training sets (the basis of the corrected resampled -test in Comparing Models Statistically). Bayle, Bayle, Janson & Mackey (2020) proved central limit theorems for cross-validation under weak stability conditions on the learner, giving asymptotically exact confidence intervals for the -fold test error. Bates, Hastie & Tibshirani (2024) showed that the usual intervals can "have coverage far below the desired level" and proposed a nested cross-validation scheme to estimate the variance, which gave approximately correct coverage in many of their examples. These do not contradict Bengio and Grandvalet: one is asymptotic under stability, the other uses extra computation, and neither is exactly unbiased for every distribution at finite .
Selection bias of the minimum
The minimum is a concave function, so Jensen's inequality gives . For the size of the gap, bound the expected maximum of with an exponential moment, for any :
Take logarithms, divide by , and choose :
Varma & Simon (2006) observed the effect on data with no signal, and Cawley & Talbot (2010) argued that a model-selection criterion's variance matters as much as its bias, because "a non-negligible variance introduces the potential for over-fitting in model selection," with degradation "often of comparable magnitude to differences in performance" between learning algorithms. Remedies: nested cross-validation (the general fix), a fresh test set, or the cheap bias correction of Tibshirani & Tibshirani (2009), computed from the fold-wise errors the search already produced.
Consistency for identification. Selecting the model that predicts best and selecting the true model are different goals. Shao (1993) showed that for choosing among linear models when a correct model exists, leave-one-out (and -fold with fixed ) selects an overfitting model with probability that does not vanish as grows; consistency requires the validation fraction to tend to one, . This mirrors the split between AIC (efficient for prediction) and BIC (consistent for identification), discussed in Bayesian Model Selection.
You want cross-validation to identify the correct linear model among nested candidates, with probability tending to one as . According to Shao (1993), how must the validation set size scale?
- (leave-one-out), since it is nearly unbiased
- (10-fold), the usual compromise
- : the validation part must eventually dominate
- Any scaling works, as long as the folds are stratified
Show the answer
C. : the validation part must eventually dominate
Leave-one-out estimates prediction error almost without bias, but the error differences between a correct model and a slightly overfitting one shrink like and are swamped by the noise of the estimate. Making the training part a vanishing fraction amplifies those differences relative to the noise.
Key takeaways
- exactly: cross-validation measures the learning procedure at a slightly smaller sample size, and it tracks the average error over training sets far better than the error of the deployed model.
- For penalized least squares, leave-one-out residuals are , from a single fit; GCV averages the leverages and is close to . The shortcut fails for -NN and other non-least-squares learners.
- ; the naive estimate is biased by , and no unbiased estimator works for all distributions (Bengio & Grandvalet).
- Minimizing CV over candidates is optimistic by up to ; use nested CV or a fresh test set. Leave-one-out predicts well but does not consistently identify the true model; that needs (Shao).
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
With examples, what is 5-fold cross-validation an unbiased estimator of?
- The risk of the model trained on all 1,000 examples
- The expected risk of the learner trained on 800 examples
- The expected risk of the learner trained on 1,000 examples
- The average training error of the five fold models
Show the answer
B. The expected risk of the learner trained on 800 examples
Each held-out loss comes from a model trained on 800 points it is independent of, so exactly. That is pessimistic for when more data help, and a weak proxy for the conditional error of the deployed model.
A CV routine reports a standard error computed as if the fold errors were independent. Compared with the true standard deviation of the CV estimate, it is typically:
- Too large, because folds are small
- Exactly right, by the central limit theorem
- Too small, because the fold errors are positively correlated through overlapping training sets
- Too small only for leave-one-out
Show the answer
C. Too small, because the fold errors are positively correlated through overlapping training sets
The naive variance estimate has expectation , where is the covariance between losses in different folds, created by the shared training data.
For ridge regression with smoother matrix , what does generalized cross-validation replace in the leave-one-out formula?
- Each leverage by the average
- Each residual by its absolute value
- The squared error by the 0–1 loss
- The refits by refits
Show the answer
A. Each leverage by the average
GCV is : one global correction instead of one per point. It is rotation-invariant and, to first order, equals with .
You compare 100 settings whose true errors are all equal; each CV estimate has independent Gaussian noise with standard deviation 1 point. Roughly how optimistic is the smallest CV estimate, in expectation?
- 0 points
- About 1 point
- About 2.5 points
- About 10 points
Show the answer
C. About 2.5 points
The expected maximum of 100 independent standard normals is about 2.51 (the bound is a little loose). So the winner looks about 2.5 points better than it is, purely by selection.
Further reading
- S. Bates, T. Hastie & R. Tibshirani, "Cross-validation: what does it estimate and how well does it do it?" Journal of the American Statistical Association 119(546), 1434–1445 (2024).
- Y. Bengio & Y. Grandvalet, "No unbiased estimator of the variance of K-fold cross-validation," Journal of Machine Learning Research 5, 1089–1105 (2004).
- P. Craven & G. Wahba, "Smoothing noisy data with spline functions: estimating the correct degree of smoothing by the method of generalized cross-validation," Numerische Mathematik 31(4), 377–403 (1978).
- J. Shao, "Linear model selection by cross-validation," Journal of the American Statistical Association 88(422), 486–494 (1993).
- G. C. Cawley & N. L. C. Talbot, "On over-fitting in model selection and subsequent selection bias in performance evaluation," Journal of Machine Learning Research 11, 2079–2107 (2010).