MoonSay /Tutorials/Machine Learning Interview #11
Contents
  1. 01Introduction
  2. 02Training accuracy lies
  3. 03One mock exam
  4. 04Everyone takes a turn
  5. 05Tune, then refit
  6. 06Don’t let the test leak
  7. 07Common misconceptions
  8. 08Interview questions and model answers
  9. 09Summary
  10. References
Cross-Validation/Chapter 03 of 09

03One mock exam: the hold-out split

3.1Hide some patients

So we need patients the model has never seen. The simplest way is a hold-out split: hide a random 20\% of the patients, train on the other 80\%, and score on the hidden ones (James et al., 2013, §5.1.1). For tuning, the hidden part is called a validation set: we train once for each candidate setting and keep the setting with the best validation score (Murphy, 2022, §4.5.4). When there is plenty of data, Hastie et al. (2009, §7.2) recommend three parts: a training set to fit the models, a validation set to choose among them, and a test set to assess the final choice, for example 50\%, 25\% and 25\%. The test set “should be kept in a vault” and brought out only at the end.

3.2Same data, same model, different luck

Which 20\% should we hide? Let us split the same 569 patients 200 times at random, each time 455 for training and 114 for testing, with the number of neighbors fixed at k=15. The test accuracies land anywhere from 83.3\% to 96.5\%, with mean 89.0\% and standard deviation 2.68 percentage points (Figure 2a). Nothing changed except the coin flip that decided who was hidden.

Two hundred random 80/20 splits of the same 569 patients. (a) Test accuracy of 15-nearest neighbors on each split, with the normal curve of the sampling theory (3). (b) 15 and 41 neighbors scored on the same splits (small jitter added; points above the diagonal favor k=41). Computed for this tutorial, with the same data and splits as the companion video.
Figure 2. Two hundred random 80/20 splits of the same 569 patients. (a) Test accuracy of 15-nearest neighbors on each split, with the normal curve of the sampling theory (3). (b) 15 and 41 neighbors scored on the same splits (small jitter added; points above the diagonal favor k=41). Computed for this tutorial, with the same data and splits as the companion video.
Try itSplit the same data again and again

Each press draws one of the 200 random 80/20 splits of Figure 2: 455 patients train the model, 114 test it, and the number of neighbors stays at k=15. Left: the test accuracies drawn so far, with the curve of the sampling theory, equation (3). Right: k=15 against k=41 on the same splits.

3.3How large is the luck of the split?

Treat the trained model as fixed for a moment, and let p be the fraction of all N patients it would classify correctly. A test set is a sample of n of those patients drawn without replacement, and the test accuracy \hat p is the share of correct ones in the sample.

Proposition 2 (Variance of a hold-out accuracy). For a sample of n patients drawn at random without replacement from N,

\Var(\hat p)=\frac{p(1-p)}{n}\cdot\frac{N-n}{N-1}. \tag{3}

Let I_1,\dots,I_n indicate whether the sampled patients are classified correctly, so \hat p=\frac1n\sum_iI_i. Each I_i is 1 with probability p, so \Var(I_i)=p(1-p). Two different draws are both correct with probability p\cdot\frac{Np-1}{N-1}, because after one correct patient is drawn, Np-1 of the remaining N-1 are correct. Hence

\Cov(I_i,I_j)=p\,\frac{Np-1}{N-1}-p^2=-\frac{p(1-p)}{N-1},

and adding the n variances and the n(n-1) covariances,

\Var\Bigl(\sum_{i=1}^{n}\;I_i\Bigr)=np(1-p)-n(n-1)\frac{p(1-p)}{N-1}=np(1-p)\,\frac{N-n}{N-1}.

Dividing by n^2 gives (3).

Worked example 1 (The luck of one split). With p=0.89, n=114 and N=569: p(1-p)/n=0.89\times0.11/114, a standard deviation of 2.94 points, and the correction \sqrt{(569-114)/568}=0.895 brings it to 2.63 points. The 200 splits had 2.68. Almost all of the wobble is explained by which patients happen to be in the test set; the change of training set adds little. One patient is worth 1/114=0.88 points, so a swing of three or four borderline patients moves the score by three points.

3.4Luck against the real differences

The luck matters because the candidates we want to compare are close. Chapter 5 will find that k=41 beats k=15 by about one point. On the same 200 splits, 41 neighbors averaged 90.3\% and 15 neighbors 89.0\%, a gap of 1.35 points, about one and a half patients. Scored on the same split, the two candidates share the luck of the test set, so the comparison is fairer than the raw scores suggest (Figure 2b); even so, the gap itself varied with a standard deviation of 1.44 points, as large as the gap. The better candidate won on 142 splits, tied on 38 and lost on 20: on three splits in ten, one split would not have picked it.

So one split is unreliable: luck moves the score more than the real gaps between candidates. It also wastes data twice: the model is trained on only 80\% of the patients, and only 20\% of them ever serve as judges.

Pause and think. How large would the luck of the split be with a test set of 1{,}000 patients drawn from a very large population? How many test patients would bring it down to half a point?

Show the answer

With N much larger than n the correction in (3) is 1, so the standard deviation is \sqrt{0.89\times0.11/1000}=1.0 point. For half a point we need n=0.89\times0.11/0.005^2\approx3{,}900 patients. Precise accuracy estimates need large test sets; small data sets need a better use of the data, which is the next chapter.