2.6.5 · D3Model Evaluation & Selection

Worked examples — Stratified and leave-one-out cross-validation

4,059 words18 min readBack to topic

This page is a workbench. In Stratified and leave-one-out cross-validation you met the two ideas; here we drive them through every situation they can meet — clean splits, ugly non-divisible splits, the degenerate one-sample-per-class case, a multi-class remainder mess, the extreme LOOCV limit, a real-world imbalance disaster, and an exam twist. Before each example, you forecast the answer. Guessing first is how the intuition sticks.

Prerequisites we lean on: 2.6.03-K-fold-cross-validation (the base split), 2.6.04-Bias-variance-tradeoff-in-cross-validation (why fold count matters), 2.6.01-Training-validation-and-test-sets (train/test roles), 3.2.05-Handling-imbalanced-classes (why minority classes break naive splits), and 2.3.08-Sampling-methods (how we draw without bias).


The scenario matrix

Every problem this topic can throw is one cell below. The worked examples that follow each declare which cell they cover, so together they fill the whole grid.

Cell Scenario class What is special / dangerous
A Stratified, class count divisible by Clean case, exact per-fold counts
B Stratified, class count NOT divisible by Remainder samples — floor/ceil distribution
B2 Stratified with 3+ classes, several remainders Each class distributes its own remainder
C Stratified with severe imbalance (rare class) Random fold can hit zero minority samples
D Degenerate: a class has fewer samples than Cannot put one in every fold — limit case
E LOOCV numeric error on a small dataset , average of per-sample losses
F LOOCV classification (0/1 loss) Loss is right/wrong, gives an accuracy
G Limiting behaviour as Cost and bias trend, why we stop somewhere
H Real-world word problem Translate a story into counts and folds
I Exam twist: probability a fold misses a class Uses the formula

First, the symbols (earned before use)

The three symbols , and appear constantly below, so define them on a picture.

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

Look at the figure: the strip of length is cut into chunks. When the cut is not even, the plum chunks are one taller (the ceilings) and the teal chunks are the floors. Count the extra tiles: exactly chunks (the remainder we just defined) get the extra one.


Cell A — clean divisible split


Cell B — remainder, not divisible

Refer back to the plum/teal strip in s01: here exactly 3 chunks are plum (15) and 2 are teal (14).


Cell B2 — three classes, several remainders at once


Cell C — severe imbalance (rare class saved)


Cell D — degenerate: class smaller than


Cell E — LOOCV numeric error (regression)


Cell F — LOOCV classification (0/1 loss)


Cell G — limiting behaviour as

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

The figure plots both curves: the teal training-fraction climbing and flattening at 1, the burnt-orange cost rising as a straight line. LOOCV sits at the far right of both.


Cell H — real-world word problem


Cell I — exam twist: probability a random fold misses a class

Cells C and D worried about a random fold containing zero of a class. Here we finally derive how likely that is — the formula both earlier cells pointed to.


Recall Quick self-test

Divisible split, 60 of a class, k=6, per fold? ::: per fold. Non-divisible: 62 of a class, k=6 — how many folds get the extra? ::: folds get , the rest get 10. Three classes 22/15/7 in k=4 — do their "extra" fold counts have to match? ::: No; they are ( each), independent per class. A class has 4 samples, k=10 — is stratification possible? ::: No; so 6 folds get zero of it (Cell D degenerate). LOOCV error if per-sample squared errors sum to 3.18 over 5 samples? ::: . Probability a random k=5 fold misses a class with 10 samples? ::: (independence approximation).

Related routes onward: for ordered data where random folding is wrong entirely, see 5.4.02-Time-series-cross-validation.