K-fold cross-validation estimates a fitting procedure's prediction error by holding out each of $K$ folds in turn. For least squares, leave-one-out needs a single fit: each held-out residual is $(y_i - \hat{y}_i)/(1 - h_i)$, with $h_i$ the leverage. On 442 diabetes patients, 10-fold cross-validation of a ten-input linear regression gives an MSE of 2,987.3 (training MSE 2,859.7). In a seeded simulation (2,000 training sets of 40 points), bias followed the learning curve (22% for 2-fold with a cubic, under 1% from 10 folds); variance did not rise with $K$. It estimates the expected error over training sets, not the fitted model's own, and its naive standard error is too small. Tuning needs nested cross-validation: on the diabetes data the curve's minimum understated the tuned error by about 2%.
A single split of 442 diabetes patients into training and test sets gives an estimate of prediction error that depends heavily on which patients landed where. In Training, Validation, and Test Sets, the test MSE of the same linear regression ran from about 2,300 to 3,800 in the middle 80% of random splits with 44 test patients. And the data spent on testing are data not spent on fitting.
Cross-validation addresses both problems by taking turns. The data are divided into parts; each part is held out once while the model is fitted to the rest; and the errors on the held-out parts are combined. Every observation is used for testing exactly once and for fitting $K - 1$ times. The idea goes back at least to the 1970s, when Stone (1974) used it both to choose among predictors and to assess the chosen one, and it is now the standard way to estimate prediction error and to tune models when data are limited (Hastie, Tibshirani and Friedman, 2009, §7.10).
K-fold cross-validation
Divide the $n$ observations at random into $K$ groups of roughly equal size, the folds. For $k = 1, \dots, K$, fit the model to all the data except fold $k$, and compute its loss on the observations in fold $k$.
To write the estimate, let $\kappa(i)$ be the fold that contains observation $i$, and let $\hat{f}^{-k}$ be the model fitted with fold $k$ removed. The cross-validation estimate of prediction error is the average loss of each observation's held-out prediction (Hastie et al., 2009, eq. 7.48):
$$\mathrm{CV}(\hat{f}) = \frac{1}{n} \sum_{i=1}^{n} L\bigl(y_i, \hat{f}^{-\kappa(i)}(x_i)\bigr).$$
For squared error, $L(y, \hat{y}) = (y - \hat{y})^2$, this is the mean squared error of the $n$ held-out predictions. A common variant averages the $K$ per-fold MSEs instead; the two agree exactly when the folds have equal sizes and nearly otherwise. Typical choices are $K = 5$ or $K = 10$.
The diabetes data in ten folds
The ten-input linear regression from Multiple Linear Regression, with the same ten folds (two of 45 patients and eight of 44):
| Fold | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| MSE | 3,112.0 | 3,766.9 | 2,346.3 | 3,501.1 | 2,651.4 | 3,359.2 | 2,644.8 | 3,098.3 | 2,254.6 | 3,117.8 |
The average of the ten fold MSEs is 2,985.2; the MSE pooled over all 442 held-out predictions is 2,987.3. The training MSE of the same model fitted to all 442 patients is 2,859.7, so the training error understates the cross-validated error by about 4%, a modest gap for a model with eleven coefficients and 442 observations. The fold MSEs range from 2,255 to 3,767, with a standard deviation of 498.1. Each is a holdout estimate from about 44 patients and is as noisy as the single splits above. Their average is much steadier.
What to do with the K models
Cross-validation fits $K$ models, but none of them is the final model. They exist only to estimate how well the procedure (fit a linear regression on these ten inputs) predicts. The model to use is then fitted to all $n$ observations (Hastie et al., 2009, §7.10.1). Its error is estimated by the cross-validation figure, which describes models fitted to $n(K - 1)/K$ observations, slightly fewer than the final model gets. The consequences of that difference are taken up below.
If a test set has been kept back, as the previous article recommends, cross-validation runs on the remaining data only. It replaces the validation set, not the test set.
Leave-one-out cross-validation
The extreme case $K = n$ holds out one observation at a time: fit to the other $n - 1$, predict the one left out, and repeat for every observation. This is leave-one-out cross-validation (LOOCV); the jackknife leaves observations out in the same way to estimate bias and variance. It has no randomness, since there is only one way to leave out each observation, and each fit uses almost all the data. In general it costs $n$ fits. For least-squares regression it costs one.
The leverage shortcut
Write the linear model in matrix form as in Multiple Linear Regression: $A$ is the $n \times p$ matrix of inputs with a column of ones, $x_i^{\mathsf{T}}$ is its $i$th row, $M = A^{\mathsf{T}} A$, the coefficients are $b = M^{-1} A^{\mathsf{T}} y$, and the fitted values are $\hat{y} = A b = H y$ with the hat matrix $H = A M^{-1} A^{\mathsf{T}}$. The diagonal entry
$$h_i = x_i^{\mathsf{T}} M^{-1} x_i$$
is the leverage of observation $i$: how much its own outcome contributes to its own fitted value, $\partial \hat{y}_i / \partial y_i = h_i$. Linear Regression Assumptions gives its form for a single input. In a model with an intercept, leverages lie between $1/n$ and 1 and add up to $p$, the number of coefficients (Worked problem 1 proves both).
Remove observation $i$. The fit without it uses $M - x_i x_i^{\mathsf{T}}$ in place of $M$ and $A^{\mathsf{T}} y - x_i y_i$ in place of $A^{\mathsf{T}} y$. The inverse of a matrix changed by one outer product has a closed form, the Sherman-Morrison formula, proved in Leverage, Influence and Residual Diagnostics:
$$\bigl(M - x_i x_i^{\mathsf{T}}\bigr)^{-1} = M^{-1} + \frac{M^{-1} x_i \, x_i^{\mathsf{T}} M^{-1}}{1 - h_i}.$$
Multiplying on the left by $x_i^{\mathsf{T}}$ and using $x_i^{\mathsf{T}} M^{-1} x_i = h_i$,
$$x_i^{\mathsf{T}} \bigl(M - x_i x_i^{\mathsf{T}}\bigr)^{-1} = x_i^{\mathsf{T}} M^{-1} \Bigl(1 + \frac{h_i}{1 - h_i}\Bigr) = \frac{x_i^{\mathsf{T}} M^{-1}}{1 - h_i}.$$
The prediction for $y_i$ from the fit without it is therefore
$$\hat{y}_{(-i)} = \frac{x_i^{\mathsf{T}} M^{-1}\bigl(A^{\mathsf{T}} y - x_i y_i\bigr)}{1 - h_i} = \frac{\hat{y}_i - h_i y_i}{1 - h_i},$$
and its error, after putting $y_i$ over the same denominator,
$$y_i - \hat{y}_{(-i)} = \frac{y_i (1 - h_i) - \hat{y}_i + h_i y_i}{1 - h_i} = \frac{y_i - \hat{y}_i}{1 - h_i}.$$
The deleted residual is the ordinary residual divided by $1 - h_i$. Leave-one-out cross-validation for least squares is then (James, Witten, Hastie and Tibshirani, 2021, eq. 5.2; Hastie et al., 2009, eq. 7.51)
$$\mathrm{CV}_{(n)} = \frac{1}{n} \sum_{i=1}^{n} \Bigl(\frac{y_i - \hat{y}_i}{1 - h_i}\Bigr)^2,$$
computed from one fit to all the data. The sum of the squared deleted residuals is known as the PRESS (prediction sum of squares) statistic; Allen (1974) is, with Stone (1974), one of the key early references for cross-validation that Hastie et al. (2009, ch. 7 bibliographic notes) single out. The formula also explains why the training error is optimistic: each residual is shrunk by the factor $1 - h_i$, because each observation pulls the fit toward itself, and points of high leverage pull hardest.
A version of the shortcut, with the diagonal of $S$ in place of the leverages, holds for many linear fitting methods $\hat{y} = S y$ (Hastie et al., 2009, eq. 7.51), such as ridge regression and smoothing splines. What Cross-Validation Estimates proves exactly when it holds (its Lemma 1 and Theorem 1): when the fit without a point keeps the other points' weights, divided by $1 - S_{ii}$, which is true of ridge regression, penalized splines and kernel smoothers but not of nearest neighbors. That article also proves what this one measures below by simulation: what K-fold cross-validation estimates on average, why its estimate says nothing about the fitted model's own error in least squares with normal errors, and why its naive standard error is too small. For logistic regression, trees and most other methods there is no such formula, and the model is refitted $n$ times.
Checking it on the diabetes data
For the ten-input regression, 442 separate refits and the shortcut give the same LOOCV estimate, 3,001.753, and the 442 deleted residuals agree to within $2 \times 10^{-11}$. The leverages run from 0.0072 to 0.1276 and sum to 11, the number of coefficients. The patient with the highest leverage has an ordinary residual of $-37.31$; divided by $1 - 0.1276$, it becomes $-42.77$ when that patient is left out. With BMI as the only input, the leverages are all below 0.032 and LOOCV (3,923.0) is barely above the training MSE (3,890.5).
Replacing each $h_i$ by their average $p/n$ gives generalized cross-validation,
$$\mathrm{GCV} = \frac{1}{n} \sum_{i=1}^{n} \Bigl(\frac{y_i - \hat{y}_i}{1 - p/n}\Bigr)^2 = \frac{\text{training MSE}}{(1 - p/n)^2},$$
which needs only the trace of $H$ (Hastie et al., 2009, eq. 7.52). For the ten-input model it is 3,007.5, close to the exact LOOCV value of 3,001.8. Since $1/(1 - x)^2 \approx 1 + 2x$ for small $x$, GCV is approximately the training MSE times $1 + 2p/n$, the same correction for optimism that underlies Mallows' $C_p$ and AIC in Model Selection. Optimism and Effective Degrees of Freedom proves that correction for any linear smoother, with the trace of $S$ in place of $p$ (its Theorem 2).
How good are the estimates?
Cross-validation is an estimator, with a bias and a variance of its own. The number of folds trades one against the other, and the usual account (James et al., 2021, §5.1.4) runs as follows. Each training part has $n(K - 1)/K$ observations. With few folds, the models are fitted to noticeably less data than the final model, so they are worse and the estimate is biased upward. With $K = n$, the training sets are almost the full data and the bias almost disappears, but the $n$ fitted models are nearly identical, so their errors are highly correlated, and the average of highly correlated quantities has a high variance.
The first half of that account is certain; the second is a tendency, not a law. Both can be measured in a simulation where the true error is known.
A simulation
Training sets of $n = 40$ points are drawn with $x$ uniform on $[0, 1]$ and $y = \sin(5x) + \varepsilon$, $\varepsilon \sim N(0, 0.3^2)$, the setting of The Bias-Variance Tradeoff with random rather than fixed inputs. Two methods are fitted: a cubic polynomial by least squares, which is stable, and 1-nearest-neighbor regression, which predicts each new point by the outcome of its single closest training point and is very unstable. For each of 2,000 training sets (seed 742), the script computes the conditional error $\mathrm{Err}_{\mathcal{T}}$, the expected squared error of that particular fitted model on a new point,
$$\mathrm{Err}_{\mathcal{T}} = \sigma^2 + \int_0^1 \bigl(\hat{f}(x) - f(x)\bigr)^2 \, dx,$$
and its cross-validation estimates for several $K$. The expected error $\mathrm{Err}$ is the average of $\mathrm{Err}_{\mathcal{T}}$ over training sets: 0.1050 for the cubic and 0.1840 for 1-nearest neighbor.
| Cubic | 2-fold | 5-fold | 10-fold | 20-fold | LOOCV | 10-fold × 10 |
|---|---|---|---|---|---|---|
| Mean estimate | 0.1279 | 0.1083 | 0.1057 | 0.1050 | 0.1046 | 0.1059 |
| Bias | +0.0229 | +0.0033 | +0.0007 | +0.0000 | −0.0004 | +0.0009 |
| Standard deviation | 0.0858 | 0.0292 | 0.0267 | 0.0258 | 0.0255 | 0.0260 |
| 1-nearest neighbor | 2-fold | 5-fold | 10-fold | 20-fold | LOOCV | 10-fold × 10 |
|---|---|---|---|---|---|---|
| Mean estimate | 0.1946 | 0.1871 | 0.1850 | 0.1845 | 0.1840 | 0.1848 |
| Bias | +0.0106 | +0.0031 | +0.0010 | +0.0005 | +0.0000 | +0.0008 |
| Standard deviation | 0.0627 | 0.0567 | 0.0554 | 0.0556 | 0.0551 | 0.0534 |
Bias follows the learning curve. The expected error of a cubic fitted to 20 points is 0.1289, to 32 points 0.1094, to 36 points 0.1068 and to 39 points 0.1058; the 2-, 5-, 10-fold and leave-one-out means are within about 0.001 of these. Cross-validation estimates the error of the model fitted to the training part, so its bias is the slope of the learning curve over the data it withholds (Hastie et al., 2009, §7.10.1). With 40 observations, 2-fold cross-validation fits a cubic to 20 points, where the curve is steep, and overstates the error by 22%. By 10 folds the bias is under 1%. The 1-nearest-neighbor learning curve is flatter, so its bias is smaller at every $K$.
Variance did not rise with K. For both methods the standard deviation of the estimate fell from 2 to 5 to 10 folds and was flat or slightly lower beyond, with LOOCV the least variable estimate for the cubic and essentially tied for 1-nearest neighbor. The 2-fold estimate is the most variable, partly because some cubics fitted to 20 random points extrapolate wildly into gaps in the data. This contradicts the textbook tendency, and it is not a quirk of the simulation. Bengio and Grandvalet (2004) decompose the variance of K-fold cross-validation and conclude that there is "no general trend": the minimum variance "can be reached for $K = n$ or for an intermediate value of $K$." Hastie et al. (2009, §7.10.1) state the textbook view carefully, that leave-one-out "can have high variance", and their own simulation in §7.12 found 10-fold cross-validation less variable than leave-one-out. Which way it goes depends on the method, the loss and the data. The practical recommendation of $K = 5$ or 10 survives on other grounds: it is cheaper than leave-one-out, its bias is usually small, and repeating it with new random folds can reduce the part of its variance that comes from the fold assignment.
Repeating the split helps a little. Ten-fold cross-validation repeated with ten different random fold assignments, and averaged, lowered the standard deviation from 0.0267 to 0.0260 for the cubic and from 0.0554 to 0.0534 for 1-nearest neighbor. Repetition removes the noise of the fold assignment, not the noise of the data. On the diabetes data, 200 different assignments of the same 442 patients to ten folds gave estimates with a standard deviation of 21.4 (range 2,962 to 3,061), small next to the uncertainty that comes from having 442 patients at all.
What cross-validation estimates
A natural hope is that cross-validation estimates $\mathrm{Err}_{\mathcal{T}}$, the error of the model actually fitted to the data at hand. It does not do so well. In the simulation, the correlation across training sets between the 10-fold estimate and $\mathrm{Err}_{\mathcal{T}}$ was $-0.03$ for the cubic and $+0.47$ for 1-nearest neighbor.
For the cubic, the conditional error varies little between training sets (standard deviation 0.0124), much less than the estimate does (0.0267), and the variation the estimate does have is unrelated to it. Hastie et al. (2009, §7.12) report the same pattern, including correlations near zero or negative, and conclude, as they announce in §7.10, that cross-validation "typically estimates well only the expected prediction error" $\mathrm{Err}$, the average over training sets. Bates, Hastie and Tibshirani (2024) prove this for least squares in the Gaussian linear model: the cross-validation estimate is closer, in mean squared error, to the average error of models fitted to other training sets of the same size than to the error of the model fitted to the data at hand. The cross-validation figure for a procedure is therefore best read as a statement about the procedure: this is how well a cubic fitted to 40 points from this population predicts, on average.
The standard error is too small
It is common to report the standard deviation of the $K$ fold errors divided by $\sqrt{K}$ as the standard error of the cross-validation estimate, as in the diabetes example: $498.1 / \sqrt{10} = 157.5$. The formula would be right if the fold errors were independent. They are not: each pair of training parts shares $(K - 2)/(K - 1)$ of its observations, and an observation used to test one fold was used to fit all the others. In the simulation, this naive standard error averaged 0.0228 for the cubic with 10 folds, while the estimates actually varied with a standard deviation of 0.0267; for 1-nearest neighbor the figures were 0.0369 and 0.0554. What Cross-Validation Estimates derives the exact variance and the naive estimate's expectation under a symmetric covariance model (its Theorem 5), which shows the naive estimate too small whenever the held-out errors are positively correlated on balance. Bengio and Grandvalet (2004) prove that no estimator of the variance of K-fold cross-validation is unbiased for all distributions, and Bates et al. (2024) show that intervals built on the naive standard error can cover the true error far less often than their nominal level, proposing a nested scheme that corrects it. The naive standard error remains useful as a rough scale, which is how the one-standard-error rule uses it, but it should not be read as exact.
Tuning with cross-validation
When a model has a hyperparameter, cross-validation estimates the error for each candidate value, and the value with the smallest estimate is chosen (Hastie et al., 2009, eq. 7.49). For nearest-neighbor regression of diabetes progression on BMI and S5, with $k$ from 1 to 100 and the same ten folds, the cross-validated MSE is 5,633.2 at $k = 1$, falls to its minimum of 3,226.3 at $k = 22$, and rises slowly to 3,362.0 at $k = 100$. The final model uses $k = 22$ and all 442 patients. The pruned trees of Regression Trees were chosen the same way, and Model Selection adds the one-standard-error rule, which prefers a simpler model when the curve is flat near its minimum.
Nested cross-validation
The minimum of a cross-validation curve, 3,226.3, is not an honest estimate of the tuned model's error. It is the winner's curse again: among 100 noisy estimates, the smallest is the one most likely to be too small. To estimate the error of the whole procedure, including the tuning, the tuning has to be repeated inside each fold, so that the held-out fold plays no part in choosing $k$. This is nested cross-validation:
- Split the data into $K$ outer folds.
- For each outer fold $k$: on the other $K - 1$ folds, run an ordinary (inner) cross-validation to choose the hyperparameter; fit the model with that value to those $K - 1$ folds; predict the outer fold $k$.
- Average the outer-fold errors.
On the diabetes data, with ten outer folds and ten inner folds, the nested estimate is 3,301.0, against 3,226.3 for the minimum of the curve: the curve's minimum understates the tuned model's error by about 2%. The values of $k$ chosen in the ten outer training parts were 17, 19, 20, 20, 20, 21, 21, 21, 32 and 68, a reminder that the tuning itself is variable. Varma and Simon (2006) showed the effect is large when many settings are searched and the signal is weak: on simulated gene-expression data with no difference between classes, the cross-validated error of tuned classifiers was below 30% for a sizable share of datasets, while nested cross-validation gave "an almost unbiased estimate of the true error."
The same experiment can be run on data with no signal at all. Fifty cases have ten inputs and an outcome drawn independently of them from a standard normal distribution, so no prediction can beat the true mean of 0, whose expected squared error is 1. Nearest-neighbor regression with $k$ from 1 to 40 is tuned by 10-fold cross-validation, over 300 simulated datasets:
| Estimate | Mean over 300 datasets |
|---|---|
| Minimum of the cross-validation curve | 0.985 |
| Nested cross-validation | 1.057 |
| True error of the tuned model | 1.063 |
The minimum of the curve claims an error below 1 in 54.7% of the datasets, better than any possible predictor; the nested estimate is within 0.006 of the truth on average.
Nested cross-validation estimates the error of the procedure; it does not choose the final hyperparameter. That is still done by an ordinary cross-validation on all the data, as above. When the data allow a separate test set, the test set does the same job as the outer loop. Keeping the choice and the estimate on separate data is the sample splitting that Selective Inference and the Garden of Forking Paths compares with other ways of making a selected result honest.
Variants
- Stratified K-fold, for classification, makes each fold's class proportions match the whole dataset's. It removes one source of noise between folds, and matters most with rare classes. The Academics section's Evaluating Classifiers shows the fold-to-fold variation of text classification scores.
- Repeated K-fold runs the procedure several times with different random folds and averages, reducing the variance due to the fold assignment, as measured above.
- Monte Carlo cross-validation (repeated random subsampling) draws a fresh random training and validation split each time. Observations can be held out several times or never.
- Leave-$p$-out holds out every possible subset of $p$ observations. With $p = 1$ it is LOOCV; with $p = 2$ it already needs $n(n - 1)/2$ fits. Holding out pairs has a natural use in estimating the area under an ROC curve, which compares pairs of cases, by leaving out one positive and one negative case at a time (leave-pair-out; Airola, Pahikkala, Waegeman, De Baets and Salakoski, 2011).
- Grouped cross-validation keeps all records of a patient, a site or a document collection in the same fold, and time-series cross-validation always trains on the past and tests on the future. Both prevent the forms of leakage that a random assignment of correlated records would allow.
K-fold cross-validation with $K < n$ is sometimes called non-exhaustive, because it uses only one of the many ways to divide the data, and leave-one-out and leave-$p$-out exhaustive. The distinction mattered little here: the variation from the choice of folds was small next to the variation from the data, as the 200 fold assignments of the diabetes data showed (a standard deviation of 21.4 against a standard error near 158), and in the simulation repeating the folds lowered the standard deviation of the estimate only from 0.0267 to 0.0260.
For classification, cross-validation works with any loss: the error rate, the log loss of Loss Functions for Classification, or a ranking measure such as AUC. The bootstrap offers another way to estimate prediction error by resampling, with its own corrections for the overlap between training and test data.
Worked problems
The problems extend the article on the diabetes data and its ten folds. The review-pass block of the article's script checks every number.
Problem 1 (the range and sum of the leverages). Prove that in a least-squares model with an intercept every leverage lies between $1/n$ and 1, and that the leverages add up to the number of coefficients $p$. Check the three facts on the two diabetes models.
Solution. The hat matrix $H = A M^{-1} A^{\mathsf{T}}$ is symmetric, and it is idempotent: $H H = A M^{-1} (A^{\mathsf{T}} A) M^{-1} A^{\mathsf{T}} = H$.
- Sum. $\operatorname{trace}(H) = \operatorname{trace}(M^{-1} A^{\mathsf{T}} A) = \operatorname{trace}(I_p) = p$, by the cyclic property of the trace.
- Upper bound. The diagonal of $H H = H$ reads $h_i = \sum_j H_{ij}^2 = h_i^2 + \sum_{j \ne i} H_{ij}^2 \ge h_i^2$, so $0 \le h_i \le 1$.
- Lower bound. The column of ones $\mathbf{1}$ is a column of $A$, so $H \mathbf{1} = \mathbf{1}$, and with $J = \mathbf{1}\mathbf{1}^{\mathsf{T}}$, $H J = J$ and $J H = (H J)^{\mathsf{T}} = J$. Then $(H - J/n)^2 = H - 2J/n + J^2/n^2 = H - J/n$, because $J^2 = nJ$. So $H - J/n$ is also symmetric and idempotent, and the argument of the upper bound gives its diagonal, $h_i - 1/n$, as at least 0.
On the data, the leverages of the BMI-only model sum to 2.000000 and those of the ten-input model to 11.000000. The smallest are 0.00226, equal to $1/n = 1/442$ to five decimals (the patient whose BMI is nearest the mean), and 0.00719; the largest are 0.0314 and 0.1276. A leverage of exactly 1 would mean the fit passes through that point whatever its outcome: the shortcut would divide by zero, and the fit without that point would not be determined.
Problem 2 (a shortcut for K-fold cross-validation of least squares). Generalize the leverage shortcut from one held-out point to one held-out fold, and check it on the ten folds.
Solution. Let fold $k$ hold the rows $A_k$ and outcomes $\mathbf{y}_k$, let $H_{kk} = A_k M^{-1} A_k^{\mathsf{T}}$ be the block of the hat matrix for those rows, and $\mathbf{e}_k = \mathbf{y}_k - \hat{\mathbf{y}}_k$ their ordinary residuals from the fit to all the data. Removing the fold replaces $M$ by $M - A_k^{\mathsf{T}} A_k$, whose inverse is given by the Woodbury identity, the many-row version of Sherman-Morrison:
$$\bigl(M - A_k^{\mathsf{T}} A_k\bigr)^{-1} = M^{-1} + M^{-1} A_k^{\mathsf{T}} (I - H_{kk})^{-1} A_k M^{-1}.$$
Multiplying on the left by $A_k$ gives $A_k M^{-1} + H_{kk}(I - H_{kk})^{-1} A_k M^{-1} = (I - H_{kk})^{-1} A_k M^{-1}$, since $I + H_{kk}(I - H_{kk})^{-1} = (I - H_{kk})^{-1}$. The fold's predictions from the fit without it are therefore
$$\hat{\mathbf{y}}_{(k)} = (I - H_{kk})^{-1} A_k M^{-1}\bigl(A^{\mathsf{T}} \mathbf{y} - A_k^{\mathsf{T}} \mathbf{y}_k\bigr) = (I - H_{kk})^{-1}\bigl(\hat{\mathbf{y}}_k - H_{kk} \mathbf{y}_k\bigr),$$
and the held-out residuals are
$$\mathbf{y}_k - \hat{\mathbf{y}}_{(k)} = (I - H_{kk})^{-1}\bigl[(I - H_{kk})\mathbf{y}_k - \hat{\mathbf{y}}_k + H_{kk}\mathbf{y}_k\bigr] = (I - H_{kk})^{-1} \mathbf{e}_k.$$
With one point in the fold, $H_{kk}$ is the number $h_i$, and this is the deleted residual of the body. The formula needs $I - H_{kk}$ to be invertible, which holds when the other folds still determine the coefficients; here the largest eigenvalue of any $H_{kk}$ is 0.2682. On the ten folds, one fit and ten solves of size 44 or 45 give a pooled cross-validated MSE of 2,987.292, equal to the ten refits to within $2 \times 10^{-11}$ in every residual, and to the 2,987.3 of the body.
Problem 3 (the one-standard-error rule for nearest neighbors). Apply the one-standard-error rule of Model Selection to the choice of $k$ for nearest-neighbor regression on BMI and S5, with the same ten folds. Which $k$ does it choose, and what does "simplest" mean here?
Solution. For nearest neighbors the simplest model is the smoothest, the largest $k$, since every prediction averages more patients. The cross-validated MSE is smallest at $k = 22$, 3,226.3. The standard deviation of the ten fold MSEs at $k = 22$, divided by $\sqrt{10}$, gives a standard error of 131.5, so the threshold is 3,357.9. The largest $k$ whose cross-validated MSE is below it is $k = 86$, at 3,357.1; $k = 100$, at 3,362.0, is just above. BMI and S5 have many tied values, so these numbers depend on how ties in distance are broken; the article's script breaks them by row order. scikit-learn's KNeighborsRegressor (version 1.5.2, same folds and per-fold scaling) gives a minimum of 3,228.2 at $k = 22$, a standard error of 130.1 and again $k = 86$ with its default search, and 3,225.3 and $k = 85$ with algorithm='brute'. The rule moves the choice from 22 to 86, a much smoother fit, because the curve rises by only 136 between $k = 22$ and $k = 100$, about one standard error. As with the subset sizes of Model Selection, the rule reads a flat curve as permission to simplify, and the standard error it uses is itself too small, as the section on the naive standard error showed.
Open questions
- Does the one-standard-error choice predict better or worse than the minimum? Problem 3 moves $k$ from 22 to 86. A nested cross-validation of the rule itself, run as in the nested section but choosing $k$ by the rule in each inner loop, would compare the two procedures' honest errors on the diabetes data.
- How well do corrected intervals for the cross-validated error cover? Bates, Hastie and Tibshirani (2024) propose nested cross-validation intervals because the naive ones undercover. Running their procedure in the article's simulation, where the expected error is known (0.1050 for the cubic), and counting how often the intervals contain it over 30 seeded batches would check the claim for a small sample of 40.
- When is leave-one-out less variable than 10-fold? The simulation found leave-one-out the least variable for the cubic and tied for 1-nearest neighbor. Repeating it for nearest neighbors with $k = 1, 5, 20$ and for polynomials of degree 1 to 10 would map where the textbook ordering holds, the question that Bengio and Grandvalet (2004) show has no general answer.
Where it leads
K-fold cross-validation estimates the expected error of a fitting procedure on training sets a little smaller than the data, with a bias that follows the learning curve and a naive standard error that is too small. For least squares, leave-one-out and K-fold estimates need only one fit. Tuning inside the folds requires a nested loop for an honest estimate. The next article, The Bootstrap Method, resamples the data with replacement instead, to measure the uncertainty of any estimate and to estimate prediction error by a different route.
In Python
import numpy as np
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import GridSearchCV, KFold, cross_val_score
from sklearn.neighbors import KNeighborsRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = load_diabetes(return_X_y=True, scaled=False)
folds = KFold(n_splits=10, shuffle=True, random_state=0)
mse = -cross_val_score(LinearRegression(), X, y, cv=folds, scoring="neg_mean_squared_error")
print(mse.mean(), mse.std(ddof=1) / np.sqrt(10)) # 2985.2 157.5
# leave-one-out by the leverage shortcut
A = np.column_stack([np.ones(len(y)), X])
H = A @ np.linalg.solve(A.T @ A, A.T)
e = y - H @ y
print(np.mean((e / (1 - np.diag(H))) ** 2)) # 3001.8
# nested CV: k chosen by an inner 10-fold CV inside each outer training part
knn = make_pipeline(StandardScaler(), KNeighborsRegressor())
inner = GridSearchCV(knn, {"kneighborsregressor__n_neighbors": range(1, 101)},
cv=KFold(10, shuffle=True, random_state=1), scoring="neg_mean_squared_error")
nested = -cross_val_score(inner, X[:, [2, 8]], y, cv=folds, scoring="neg_mean_squared_error")
print(nested.mean()) # 3296.3
scikit-learn's scorers follow the convention that larger is better, so error measures come negated: "neg_mean_squared_error" returns $-\text{MSE}$ for each fold, and the minus sign in front of cross_val_score turns the values back into MSEs. Measures that are already larger-is-better, such as "r2", are not negated. Passing an integer as cv uses unshuffled folds (stratified for classifiers); passing a KFold object with shuffle=True and a seed gives random folds that can be reproduced. Putting the scaler inside the pipeline means it is refitted within every training part, and wrapping GridSearchCV in cross_val_score gives nested cross-validation. Its result, 3,296.3, differs slightly from the 3,301.0 above because GridSearchCV chooses $k$ by the average of the inner fold MSEs and cross_val_score averages the outer fold MSEs, where the article's script pools the squared errors. For leave-one-out with any model, cv=LeaveOneOut() refits $n$ times. The numbers in this article are computed by tools/experiments/data-science/cross-validation.py in the site's repository.
References
- Airola, A., Pahikkala, T., Waegeman, W., De Baets, B., and Salakoski, T. (2011). An experimental comparison of cross-validation techniques for estimating the area under the ROC curve. Computational Statistics & Data Analysis, 55(4), 1828–1844. record
- Allen, D. M. (1974). The relationship between variable selection and data agumentation and a method for prediction. Technometrics, 16(1), 125–127. record
- Bates, S., Hastie, T., and Tibshirani, R. (2024). Cross-validation: What does it estimate and how well does it do it? Journal of the American Statistical Association, 119(546), 1434–1445. record · 2 pages
- Bengio, Y., and Grandvalet, Y. (2004). No unbiased estimator of the variance of K-fold cross-validation. Journal of Machine Learning Research, 5, 1089–1105. record · 2 pages
- Hastie, T., Tibshirani, R., and Friedman, J. (2009). The Elements of Statistical Learning: Data Mining, Inference, and Prediction (2nd ed.). Springer. record · 39 pages
- James, G., Witten, D., Hastie, T., and Tibshirani, R. (2021). An Introduction to Statistical Learning with Applications in R (2nd ed.). Springer. record · 39 pages
- Stone, M. (1974). Cross-validatory choice and assessment of statistical predictions. Journal of the Royal Statistical Society, Series B, 36(2), 111–147. record
- Varma, S., and Simon, R. (2006). Bias in error estimation when using cross-validation for model selection. BMC Bioinformatics, 7, 91. record
Reviewed: verified, 23 Nov 2026 · citations checked, 23 Nov 2026 · editorial review, 23 Nov 2026 · 11 revisions