Quant Memo
Advanced

Nested Cross-Validation

Two loops instead of one — an inner loop tunes the model, a separate outer loop grades it — so the performance you report was never used to pick the model, killing the optimistic bias that ordinary cross-validation hides.

Prerequisites: Cross-Validation for Time Series, Hyperparameter Tuning

Ordinary cross-validation does two jobs at once, and that is its downfall. You use the same folds to choose the best hyperparameters and to report how good the winner is. But the winner was selected precisely because it scored well on those folds, so quoting that score as your performance estimate is like grading a student on the exact practice questions you let them study. The number comes back too high. Nested cross-validation separates the two jobs into two loops so the grade is honest.

The idea is simple once you see it. An inner loop searches hyperparameters on one slice of data and picks a winner. An outer loop then trains that winning configuration and scores it on a completely different slice the inner loop never touched. Because the outer test data played no part in choosing the model, its score is an unbiased estimate of true performance.

The two loops

OUTER LOOP — honest estimate training data (tune here) test INNER LOOP — pick hyperparameters inner-train validation
The outer loop reserves a test block (clay) it will use only once, to grade the finished model. The inner loop works entirely inside the outer training data, splitting it again into inner-train and validation to choose hyperparameters. The test block never influences tuning.

Step by step, for each outer fold:

  1. Hold out the outer test block. Set it aside, untouched.
  2. Inner loop: on the remaining data, run cross-validation across every hyperparameter candidate and pick the best.
  3. Refit that winning configuration on all of the outer training data.
  4. Score it once on the held-out test block and record the number.

Rotate which block is the outer test, repeat, and average the recorded scores. That average is your honest performance estimate.

Never grade a model on the data you used to tune it. Nested CV enforces this with two loops: the inner loop selects, the outer loop reports. The outer score is unbiased because the outer test folds took no part in choosing the model.

Worked example: how big is the bias

You tune a classifier over 40 hyperparameter combinations. Its true out-of-sample accuracy is 53%.

  • Single (flat) CV: you cross-validate all 40 candidates, keep the best, and report its cross-validated accuracy: it comes back at 57%. That 4-point lift is selection bias, the luckiest of 40 noisy estimates, not skill.
  • Nested CV: the inner loop still picks a winner from the 40, but you then grade that winner on outer test blocks it never saw. The reported accuracy comes back at 53%, matching reality.

The single-CV number lied by 4 points, enough to turn a break-even strategy into a "profitable" one on paper and a losing one live. The more candidates you search, the wider that gap grows, which is exactly why a large hyperparameter search demands a nested outer loop.

In finance you must also purge

On labeled market data, both loops need the time-series machinery, not plain k-fold. Overlapping labels leak across any naive split, so every inner and outer split must be purged and embargoed (Purged & Embargoed Cross-Validation), or leakage inflates the scores before selection bias even enters. Nested CV controls the selection bias; purging controls the information leakage, and you need both. López de Prado's combinatorial purged CV generalizes the outer loop to many paths, turning one performance number into a distribution.

Pitfalls

  • Reporting the tuned score. The most common mistake is doing a single CV, tuning on it, and quoting the winner's score, that is the biased number nested CV exists to fix.
  • Leaking between loops. If any preprocessing (scaling, feature selection, PCA) is fit on data that includes the outer test block, the separation is broken, see Data Leakage in Machine Learning. Fit every transform inside the inner training data only.
  • Cost. Nested CV multiplies compute: an outer loop of 5 and an inner loop of 5 over 40 candidates is 5×5×40=1,0005 \times 5 \times 40 = 1{,}000 fits. Random search inside the inner loop keeps this tractable.
  • Too little data. With short financial history, deeply nested loops leave tiny folds with noisy scores. Sometimes a single Walk-Forward Analysis hold-out is the more honest choice.

Nested CV gives an honest estimate of your tuning procedure, not a guarantee of live performance. It removes selection bias, but it cannot fix non-stationarity, regime change, or a flawed simulator. A leak-free, unbiased backtest can still be a strategy whose edge has since decayed, so treat even a nested-CV result as a ceiling, not a promise.

Report the spread of the outer-fold scores, not just their average. If the folds range from 48% to 58%, a mean of 53% hides real instability — the model's edge depends heavily on which period it is tested on, which is itself a warning sign on non-stationary market data.

Related concepts

Practice in interviews

Further reading

  • Cawley & Talbot, On Over-fitting in Model Selection and Subsequent Selection Bias
  • Varma & Simon, Bias in Error Estimation when Using Cross-Validation for Model Selection
  • López de Prado, Advances in Financial Machine Learning (Ch. 7, 12)
ShareTwitterLinkedIn