Quant Memo
Advanced

Cross-Validation for Time Series

Why ordinary k-fold cross-validation is invalid on financial time series, the iid assumption fails, autocorrelation and overlapping labels leak across folds, and shuffling trains on the future.

Prerequisites: Overfitting, Ordinary Least Squares (OLS)

Cross-validation estimates out-of-sample error by repeatedly holding out part of the data. Standard k-fold CV shuffles the observations, splits them into kk folds, trains on k1k-1 and tests on the held-out one, and averages. This is the workhorse of machine learning, and it is invalid on financial time series, because every assumption that makes k-fold CV an unbiased estimate of generalization error is violated by markets. Using it produces cross-validated scores that are systematically too optimistic, which is worse than useless: it launders overfit models as validated ones.

The iid assumption and why it fails

k-fold CV is unbiased when observations are independent and identically distributed. Then a random test fold is a fair sample of the same distribution the model will face, and training on the rest does not "know" anything about the test points. Financial data breaks both halves:

  • Not independent. Returns and features are serially correlated; volatility clusters; regimes persist. Adjacent observations share information.
  • Not identically distributed. The data-generating process drifts, regime shifts, structural breaks, changing microstructure. The future is not drawn from the past's distribution.

When observations are dependent, a random split places highly correlated points in both train and test. The model can essentially memorize a training point and be graded on its near-twin in the test fold, so the CV score measures interpolation among correlated neighbors, not genuine forecasting.

Two concrete leakage channels

Shuffling trains on the future. Random k-fold assignment puts observations from after the test point into the training set. The model is then fit on the future to predict the past, a gross form of Look-Ahead Bias. Any evaluation that shuffles a time series has already lost.

Overlapping labels. In finance, labels are usually built from forward windows: the label at time tt might be the sign of the return over [t,t+h][t, t+h] (as in Triple-Barrier Labeling). Two observations at times tt and tt' with tt<h|t - t'| < h have overlapping label horizons and therefore share outcome information. If tt lands in the training fold and tt' in the test fold, the label leaks across the split even without shuffling. Concretely, the labels

yt=f(rtt+h),yt=f(rtt+h)y_t = f\big(r_{t \to t+h}\big), \qquad y_{t'} = f\big(r_{t' \to t'+h}\big)

are correlated whenever [t,t+h][t,t+h][t, t+h] \cap [t', t'+h] \neq \varnothing, so train and test are not independent. This is the single most important reason naive CV fails on labeled financial data, and it is exactly what purging is designed to remove, see Purged & Embargoed Cross-Validation.

Why the bias is upward

Serial correlation inflates the apparent OOS score. Near a fold boundary, the nearest training and test points are only one step apart and thus nearly identical; the model looks accurate because it is being tested on data that is a hair's breadth from what it trained on. Bergmeir & Benítez and others show that for autocorrelated series the k-fold estimate of generalization error is biased low (too optimistic), and the bias grows with the autocorrelation length and the label horizon hh. An overfit model that would fail live can post an excellent cross-validated score.

Worked example

Suppose you label daily observations by the sign of the next 10-day return, and you run 5-fold shuffled CV. Any training observation within 10 days of a test observation shares part of its 10-day forward window, so their labels are correlated. With daily data and h=10h=10, each test point has up to 20 neighboring observations (10 before, 10 after) whose labels overlap it, and after shuffling most of those neighbors sit in the training set. The model can effectively see the test labels through their overlapping twins. A random forest that has zero real predictive power can post a cross-validated accuracy of 55–60% purely from this leakage, a number that evaporates the moment labels are purged or the split is made temporal.

What to do instead

  • Respect time order. Never shuffle. Train on the past, test on the future, the logic of Walk-Forward Analysis analysis (expanding or rolling windows).
  • Purge and embargo. When labels overlap, remove training observations whose label windows intersect the test window (purging), and drop a buffer of training observations immediately after the test window (embargo) to kill residual serial-correlation leakage. See Purged & Embargoed Cross-Validation.
  • Fit transforms inside the fold. Scalers, PCA, and imputers must be fit on training data only, full-sample preprocessing is itself look-ahead.
  • Match the evaluation horizon to the label horizon. The gap between train and test must be at least hh.

Failure modes

  • Shuffled k-fold on any time series, the cardinal sin, silently training on the future.
  • Ignoring label overlap, leaving correlated labels straddling the split.
  • Full-sample normalization leaking distributional information backward.
  • Too-short test folds, so the OOS estimate is dominated by boundary correlation.
  • TimeSeriesSplit without purging, respecting order helps, but overlapping labels at the boundary still leak; ordered CV is necessary, not sufficient.

In interviews

Expect "why can't you use k-fold cross-validation on a trading strategy?" A complete answer names the iid violation (serial correlation and non-stationarity), the shuffling-trains-on-the-future problem, and the overlapping-label leakage, then prescribes purged, embargoed, time-ordered validation. A sharp follow-up: "your labels are 10-day forward returns and you use TimeSeriesSplit, is that enough?" No: ordering fixes shuffling but the observation right before the test boundary still shares its forward window with the first test observation, so you must purge and embargo. The principle to state: on dependent, non-stationary data, the only honest test is one where train and test are genuinely separated in information, not just in index position.

Related concepts

Practice in interviews

Further reading

  • López de Prado, Advances in Financial Machine Learning (Ch. 7)
  • Hastie, Tibshirani & Friedman, The Elements of Statistical Learning (Ch. 7)
  • Bergmeir & Benítez, On the Use of Cross-Validation for Time Series Predictor Evaluation
ShareTwitterLinkedIn