2.6.15 · D4Model Evaluation & Selection

Exercises — Cross-validation pitfalls and nested CV

2,067 words9 min readBack to topic

Before we start, one picture fixes all our vocabulary. Look at it whenever a symbol feels unfamiliar.

Figure — Cross-validation pitfalls and nested CV

Level 1 — Recognition

Exercise 1.1 (L1)

You run 5-fold CV over , pick the with the lowest CV error, and report that lowest error as your model's accuracy. Name the single bias this introduces, and state its sign (too high or too low error?).

Recall Solution 1.1

What we did: we took the minimum over 4 CV scores and reported it. The bias: optimistic bias (a.k.a. selection bias). Because we minimise over several noisy scores, we tend to pick the that got lucky on these particular folds. Sign: the reported error is too low (accuracy too high). The minimum of several random variables sits below their average. So .

Exercise 1.2 (L1)

In nested CV, which loop's score do you report as the final performance number: inner or outer?

Recall Solution 1.2

The outer loop's averaged error. The inner loop exists only to pick ; its scores are contaminated by the selection above and must never be reported as performance.


Level 2 — Application

Exercise 2.1 (L2)

You have 100 samples and choose 5 outer folds. How many samples are in each outer test set, and how many in each outer training set?

Recall Solution 2.1

With : each test fold holds samples; the training set is the rest, samples.

Exercise 2.2 (L2)

Continuing: inside each 80-sample outer-training set you run 4 inner folds. How many samples train the model on each inner iteration, and how many are the inner validation set?

Recall Solution 2.2

With on 80 samples: inner validation samples; inner training samples.

Exercise 2.3 (L2)

You try 6 values of , use 5 outer and 4 inner folds. How many model fits does the full nested procedure require (count the inner-tuning fits only, ignore the final refit)?

Recall Solution 2.3

For one outer fold: each is evaluated by inner CV = fits, over values fits. Across outer folds: inner fits. What this shows: nested CV cost is where is the number of values — here .

Figure — Cross-validation pitfalls and nested CV

Level 3 — Analysis

Exercise 3.1 (L3)

Five outer folds produce these test errors: Compute the nested CV error estimate.

Recall Solution 3.1

Why average, not minimum: each error came from a different, independent tuning run, so averaging estimates the performance of the whole pipeline. Taking the minimum would re-introduce exactly the selection bias we escaped.

Exercise 3.2 (L3)

In Cawley & Talbot's synthetic study (100 samples, 1000 features), standard CV with feature-selection-outside reported 91% accuracy while nested CV reported 73%. Compute the accuracy gap, and explain in one sentence why 1000 features on 100 samples inflates the gap.

Recall Solution 3.2

Gap percentage points. Why so large: with 1000 features but only 100 samples, some features correlate with by pure chance; selecting them on the full data lets the model fit noise that the standard-CV validation folds shared, so its 91% is measuring memorised noise, not generalisation. This is a 2.6.16-Data-leakage instance.

Exercise 3.3 (L3)

Given standard CV error and nested CV error , compute the optimism (bias magnitude) and state which number you would put in a paper.

Recall Solution 3.3

Optimism . Standard CV understated the error by about (≈ 4.6 loss units too optimistic). Report: the nested number, , because it is the unbiased estimate of the deployed pipeline.


Level 4 — Synthesis

Exercise 4.1 (L4)

Write the correct ordering of steps for a pipeline that (a) normalises features, (b) selects top-10 features by correlation, (c) tunes , (d) estimates performance. State clearly what must sit inside the outer loop and what may sit outside.

Recall Solution 4.1

Everything data-dependent sits inside the outer loop. For each outer fold :

  1. Split → outer-train (80 samples) and outer-test (20 samples).
  2. Fit the normaliser's mean/std on outer-train only; apply that same transform to outer-test.
  3. Select top-10 features by correlation using outer-train only.
  4. Run inner CV on outer-train to tune (the normaliser + selector are re-fit inside each inner fold too, ideally).
  5. Refit the final model on all 80 outer-train samples with the chosen , evaluate on the untouched 20.

Outside the loop: nothing that looks at data. Only fixed choices (which model family, the grid of values, ) live outside. Related: 2.6.16-Data-leakage explains why steps 2 and 3 leak if done on the full dataset.

Exercise 4.2 (L4)

A colleague ran nested CV, found the best was in fold 1 with inner score , and reported . Diagnose the error and give the correct two-part output: (i) the performance number, (ii) the to deploy.

Recall Solution 4.2

Diagnosis: they cherry-picked one fold's inner score. Inner scores are optimistic (they are the minimum over the grid) and come from a single fold — doubly biased. (i) Performance: the outer average across all folds (e.g. ), not . (ii) Deploy : refit on all data using the chosen most often across outer folds (or re-run one plain inner CV on the full data purely to pick ). The deploy- decision and the performance number are answered by different loops.


Level 5 — Mastery

Exercise 5.1 (L5)

Suppose each of hyperparameter configurations has a CV score that is an independent draw from a distribution with mean and standard deviation . Using the known approximation for the expected minimum of i.i.d. draws, , compute the expected optimistic bias when configs are searched. Report the biased score and the bias magnitude.

Recall Solution 5.1

With : . Bias magnitude . Expected biased (minimum) score . Interpretation: searching just 4 configs already drags the reported error ~0.083 below the true mean — that gap is exactly what nested CV recovers.

Exercise 5.2 (L5)

Using the same formula, show the bias grows with : compute the bias for and and give their ratio.

Recall Solution 5.2

: bias . : , , bias . Ratio (which equals ). Lesson: searching more configs multiplies the optimism by — more tuning ⇒ more inflation ⇒ more need for nested CV.

Figure — Cross-validation pitfalls and nested CV

Exercise 5.3 (L5)

Connect to 2.7.3-Bias-variance-tradeoff: on a small dataset the per-fold CV scores have larger variance . Using , if halving the dataset doubles from to (with fixed), what happens to the optimistic bias?

Recall Solution 5.3

Bias scales linearly in . Doubling doubles the bias: new bias , exactly the old . Lesson: small datasets suffer the worst optimism — which is precisely where people most want to skip nested CV to save data, and precisely where they must not.


Recall Master mnemonic

"Inner picks, Outer scores, and the Outer never peeks." ::: Inner loop chooses ; outer loop reports the number; the outer test fold is untouched by any choice.