Contents
04Everyone takes a turn: K-fold cross-validation
4.1The procedure
So we switch from one split to many, and let every patient take a turn as judge. First, lock away a test set: 114 patients, stratified so that it has the same share of malignant tumors as the whole, who will never touch training or tuning. Cross-validation then works on the 455 training patients alone. Cut them at random into K=5 folds of 91. Train on four folds and validate on the fifth; rotate, so that each fold is the validation set once (Figure 3a). That is five trainings and five validation scores, and every training patient is validated exactly once. The cross-validation estimate is the average (Murphy, 2022, eq. 4.106):
the loss on fold j of the model trained without fold j. For accuracy, replace the loss by the accuracy. When the folds have different sizes n_j, weight each fold by n_j/N; then \mathrm{CV} is exactly the average, over all N training points, of the loss of the one model that did not see that point.
Worked example 2 (Five folds by hand). For k=15, the five validation folds are classified with 11, 5, 9, 12 and 14 mistakes out of 91: accuracies 87.9\%, 94.5\%, 90.1\%, 86.8\% and 84.6\%. Their average is 88.8\%. Since the folds are equal, this is also (455-51)/455=404/455: of the 455 training patients, 404 were classified correctly by a model that had not seen them. The five fold scores themselves range over ten points, the same luck as in Chapter 3; averaging them is what makes the estimate useful. Each of the five fold models was trained on 364 patients and is then thrown away: their only job was to produce the scores.
4.2Why five folds are about seven times steadier
Repeat the whole procedure with a different random partition of the same 455 patients, 200 times. The five-fold estimate stays between 87.7\% and 89.9\%, with a standard deviation of 0.44 points. A single validation split of 91 patients from the same 455, repeated 200 times, ranges from 81.3\% to 95.6\% with a standard deviation of 2.93 points, close to the 3.02 points of (3) with n=91 and N=455. Cross-validation is 6.7 times steadier (Figure 3b).
The reason is more than averaging five numbers, which alone would gain at most a factor \sqrt5=2.2. In (3), the wobble comes from which patients are judged. Cross-validation judges all 455 every time, so that source of luck disappears completely. What remains is that each patient is classified by a slightly different model in each partition, and only borderline patients near the boundary ever change their verdict.
4.3How many folds?
Three things change with K (Table 1).
- Training size. Each fold model trains on (K-1)N/K points: 227 of 455 for K=2, 364 for K=5, 409 for K=10 and 454 for leave-one-out. A model trained on less data is usually a little worse, so with small K the estimate is pessimistic when the learning curve is still rising (Hastie et al., 2009, §7.10.1).
- Variability. More folds use more of the data for training and average more scores; the reshuffle standard deviation fell from 0.44 points for K=5 to 0.33 for K=10, and leave-one-out has none. But the fold models of leave-one-out are trained on almost identical data, so their errors are highly correlated, and its estimate can vary a lot from one data set to another (Hastie et al., 2009; James et al., 2013, §7.10.1).
- Cost. K trainings per candidate.
The usual choice is K=5 or 10 (Hastie et al., 2009, §7.10.1). Kohavi (1995) recommended stratified ten-fold cross-validation, in which every fold keeps the class proportions of the whole; for our training patients (170 malignant of 455) that means about 34 malignant tumors per fold.
| method | trains on | trainings per candidate | mean estimate | SD (points) |
|---|---|---|---|---|
| one validation split, 80/20 | 364 | 1 | 88.3\% | 2.93 |
| 5-fold cross-validation | 364 | 5 | 88.9\% | 0.44 |
| 10-fold cross-validation | 409 | 10 | 88.8\% | 0.33 |
| leave-one-out | 454 | 455 | 88.8\% | 0 |
4.4Leave-one-out, and a shortcut for least squares
With K=N each fold is a single point: train on N-1 points, predict the one left out, repeat N times. For least squares, the N fits are unnecessary.
Proposition 3 (Leave-one-out for least squares, Allen, 1974; Hastie et al., 2009, §7.10.1). Let X be the N\times p input matrix (with a column of ones for the intercept), \hat y=Hy the least-squares fit with H=X(X^\top X)^{-1}X^\top, and h_{ii}=x_i^\top(X^\top X)^{-1}x_i the diagonal of H. If \hat y_i^{(-i)} is the prediction at x_i of the fit without point i, then
Let A=X^\top X. Removing point i gives A_{-i}=A-x_ix_i^\top, and the Sherman–Morrison formula gives
The fit without point i is \hat\beta_{-i}=A_{-i}^{-1}(X^\top y-x_iy_i), so
Subtracting from y_i,
The leverage h_{ii} measures how strongly point i pulls the fit toward itself; dividing by 1-h_{ii} undoes that pull. As a check, regress the worst radius of the 569 tumors on the mean radius and mean texture (p=3 coefficients). The training mean squared error is 1.360; the shortcut (5) gives a leave-one-out error of 1.385, and 569 separate fits give the same number to fourteen decimal places. The leverages lie between 0.0018 and 0.0404 and sum to \tr H=p=3. Replacing every h_{ii} by their average p/N gives generalized cross-validation, which only needs the trace (Hastie et al., 2009, §7.10.1).
Pause and think. A classifier always predicts the most common class in its training data. A data set has 50 examples of each of three classes, so the classifier is right a third of the time on new data. What does leave-one-out report?
Show the answer
0\% accuracy. When an example of class A is left out, the training data hold 49 of class A and 50 each of B and C, so the prediction is B or C, never A. Every left-out example is misclassified. This is the example of Kohavi (1995): leave-one-out can fail badly when removing one point changes the model in a systematic way.