2.6.5 · D4Model Evaluation & Selection

Exercises — Stratified and leave-one-out cross-validation

2,596 words12 min readBack to topic

This page is a self-test. Each problem is stated cleanly; the full worked solution hides inside a collapsible Solution callout. Try it first, then reveal. Problems are labelled L1 → L5 (Recognition → Mastery). Build on 2.6.03-K-fold-cross-validation, 2.6.04-Bias-variance-tradeoff-in-cross-validation, and the parent Stratified and leave-one-out cross-validation.

Before we start, one picture to fix the vocabulary we lean on throughout.

Figure — Stratified and leave-one-out cross-validation

Look at the two coloured bars. The left bar is the full dataset's class mix. In stratified CV, every fold's bar (right) has the same shape — same fraction of each colour. That "same shape" is the single fact almost every exercise below tests.


Level 1 — Recognition

Recall Solution — L1.1

Each iteration leaves out one sample. To use every sample once as the test point, you need one iteration per sample, so: This is Leave-One-Out Cross-Validation (LOOCV): the special case . Answer: folds; this is LOOCV.

Recall Solution — L1.2

Stratification demands each fold copy the ratio.

  • Fold X: A, B → matches
  • Fold Y: A, B → does not match ✗

Answer: Fold X is the stratified one.

Recall Solution — L1.3

False. Stratified CV preserves the original imbalance in every fold — it does not rebalance to 50/50. Forcing 50/50 would be a resampling method (see 3.2.05-Handling-imbalanced-classes and 2.3.08-Sampling-methods), which is a different tool with a different purpose.


Level 2 — Application

Recall Solution — L2.1

(a) Split each class evenly into groups: Each fold = samples.

(b) Original ratio: class 0. Fold ratio: class 0. ✓ Identical. Answer: 15 class-0 + 5 class-1 per fold; ratio preserved at 75%/25%.

Recall Solution — L2.2

Each of the 8 minority samples independently lands outside a chosen fold with probability . For the fold to miss the class, all 8 must land outside: Answer: , i.e. about a 17% chance that this one fold contains zero minority samples. Stratification drives this to (essentially) zero.

Recall Solution — L2.3

LOOCV: train on of 50 samples: 5-fold: train on . Answer: LOOCV trains on 98% each iteration vs 80% for 5-fold — this is why LOOCV has the lowest bias.


Level 3 — Analysis

Recall Solution — L3.1

The LOOCV estimate is the mean of the held-out errors: Sum , so Answer: .

Figure — Stratified and leave-one-out cross-validation
Recall Solution — L3.2

(a) Low bias. The model in each iteration trains on samples — nearly the full dataset. So the model being scored is almost identical to the final model trained on all . There is almost no "penalty" from training on less data, hence (b) High variance. Look at the figure: iteration 1's training set and iteration 2's training set share samples (all but two). The bars overlap almost completely. Highly overlapping training sets produce highly correlated error terms . When you average correlated numbers, the randomness does not cancel out the way it would for independent numbers, so the final average fluctuates a lot from dataset to dataset: Answer: overlap almost-identical models (low bias) but strongly correlated errors (high variance). Compare with 2.6.04-Bias-variance-tradeoff-in-cross-validation.

Recall Solution — L3.3

When is not divisible by , folds use or per class. For class A: , so and . The remainder is , so 3 folds get 6 and 1 fold gets 5: (Class B: → two folds get 3, two folds get 2: .) Answer: class A distributes as ; folds differ by at most one sample — as close to equal as integers allow.


Level 4 — Synthesis

Recall Solution — L4.1

(a) Stratified k-fold. With 3% positives, plain random folds risk zero positives; stratification guarantees positives in every fold. Pair with techniques from 3.2.05-Handling-imbalanced-classes if needed. (b) LOOCV. Tiny and cheap training make LOOCV's retrains affordable, and its near-zero bias gives the best point estimate of test error. (c) Neither — use time-series CV. Random or stratified folds leak future into past. See 5.4.02-Time-series-cross-validation. Answer: (a) stratified, (b) LOOCV, (c) time-series CV.

Recall Solution — L4.2

Number of models trained = number of folds. (a) LOOCV: models → s. (b) 10-fold: models → s. Ratio: . Answer: LOOCV takes 1000 s, 10-fold takes 20 s; LOOCV is 50× more expensive — exactly the factor .

Recall Solution — L4.3

Stratification means each fold copies the class ratio. But a LOOCV test fold has exactly one sample, so it holds only one class — it can never mirror a multi-class ratio. There is nothing to stratify. The training set in LOOCV is simply "everything except one point," which already keeps the class ratio nearly intact automatically. Answer: stratified LOOCV is not meaningful — with 1-sample test folds there is no distribution to preserve.


Level 5 — Mastery

Recall Solution — L5.1

Fold holds samples of class out of total. Its class- proportion: The cancels — the fold size doesn't matter, only the ratio carried over. That cancellation is the entire mathematical content of stratification.

Recall Solution — L5.2

Per fold: . By linearity of expectation, expected count of empty folds over folds: Answer: folds — on average more than one of your five folds would have no minority sample. This is the quantitative case for stratification.

Recall Solution — L5.3

Predict, subtract true , square.

  • : ; .
  • : ; .
  • : ; .
  • : ; .

Answer: ; LOOCV error .


Recall Self-test recap (cloze)

Stratified CV makes each fold copy the dataset's class distribution. LOOCV is the special case ::: Each LOOCV iteration trains on how many samples? ::: Bias of LOOCV is approximately ::: zero LOOCV variance is high because training sets ::: overlap heavily, making errors correlated Cost ratio of LOOCV to -fold :::