Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Do not test on yesterday’s homework
To check a model, you hide some data, train on the rest and test on what you hid. With ordinary data, you can hide random rows. With a time series, the value today is very close to the values yesterday and tomorrow. If you hide today but train on yesterday and tomorrow, the test is too easy, like a test whose answers were in the homework. Time-series cross-validation hides whole stretches of time, or only tests on the future.
A model that looks excellent in random cross-validation and fails in production is one of the most common forecasting mistakes.
- 1. Neighbouring points in a time series are usually…
- 2. Random folds on a time series can make the test…
- 3. A safe test for forecasting uses…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Lag embedding: each row has inputs y[t−p], …, y[t−1] and target y[t]; rows close in time share raw values.
- Random K-fold: rows shuffled into K folds; neighbours of each test row stay in training.
- Blocked K-fold: each fold is a contiguous block of time; only the block edges touch the training data.
- Gap (purging, hv-blocking): also drop training rows within a few steps of the test block, so no shared values remain.
- Rolling origin (walk-forward, time-series split): train on data up to an origin, test on the next block, move the origin forward; the window is expanding or sliding.
- When random K-fold is fine: for purely autoregressive models whose residuals are uncorrelated (Bergmeir, Hyndman and Koo 2018).
- Bias of a CV estimate: its average minus the error actually met on new data.
- 1. A rolling-origin split always tests on…
- 2. A gap around the test block removes…
- 3. Random K-fold is fine for autoregressive models when…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Building rolling-origin splits
Exercise 1.
- Rolling-origin splits of 10 points: first origin 4, test length 2, step 2, expanding window.
- The same with a sliding window of 3 and a gap of 1.
- A check that every test block lies strictly after its training data.
Practice.
- Default to rolling origin for forecasting models; it mimics how the model is used.
- Use blocked folds with a gap when data are short and every point is needed for training.
- Check the residual autocorrelation: if it is near zero for an autoregressive model, random K-fold is acceptable and uses the data well.
- Fit everything inside the fold: scaling, feature selection and hyperparameter tuning must see only the training part.
- Match the test length to the forecast horizon you care about.
- Tools: scikit-learn TimeSeriesSplit (with gap), sktime and darts backtesting, statsforecast cross_validation.
from sklearn.model_selection import TimeSeriesSplit
tscv = TimeSeriesSplit(n_splits=5, gap=5)
for train_idx, test_idx in tscv.split(X):
...- 1. Scaling and tuning must be fitted…
- 2. The test length should match…
- 3. scikit-learn's time-series splitter is…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why random folds leak. With p lags, the training row at t shares raw values with the test row at s whenever |t − s| ≤ p. Under random K-fold almost every test row has such neighbours in training. A flexible model such as nearest neighbours can then find, for each test row, a training row that is almost the same input with almost the same target, which a model never has when forecasting the future.
Why it is sometimes harmless. For a linear autoregression with uncorrelated errors, the training and test rows are dependent only through their inputs, and the fitted coefficients use all rows equally. Bergmeir, Hyndman and Koo (2018) showed that K-fold CV is then valid and even more efficient than out-of-sample evaluation. The condition is that the model captures the dependence, so its residuals are white noise.
Bias versus ranking. A CV estimate can be wrong in level but still rank models correctly, if all candidate models are flattered by the same amount. It can also rank them wrongly, when the leakage helps some models (very local ones) more than others.
- 1. With p lags, rows at t and s share raw values when…
- 2. Leakage helps most a model that is…
- 3. A CV estimate biased by the same amount for all models…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.When does random K-fold cross-validation misjudge the forecast error of a time-series model, and do blocked folds with a gap and rolling-origin evaluation fix it?
History.
- M. Stone (1974) and S. Geisser (1975) introduced cross-validation.
- P. Burman, E. Chow and D. Nolan (1994) proposed h-block CV, and J. Racine (2000) hv-block CV for dependent data.
- L. J. Tashman (2000) reviewed rolling-origin evaluation; C. Bergmeir, R. J. Hyndman and B. Koo (2018) showed when K-fold is valid for autoregression.
Exercise 2.
- A stationary AR(2) (φ = 0.5, 0.3) and a persistent AR(1) (φ = 0.97); 400 training points, 5 lags, then 400 new points for the true error.
- Models: least-squares autoregression (OLS) and 5-nearest-neighbour regression (5-NN).
- Estimates: random 5-fold, blocked 5-fold, blocked 5-fold with a gap of 5 rows on each side, and rolling origin (5 blocks over the second half).
- Bias = estimate minus the true RMSE, averaged over 30 runs.
Seed 0, bias of the RMSE estimate:
| series, model | true RMSE | random K-fold | blocked | blocked + gap | rolling origin |
|---|---|---|---|---|---|
| stationary, OLS | 1.013 | +0.001 | −0.002 | −0.002 | +0.002 |
| stationary, 5-NN | 1.125 | −0.001 | +0.014 | +0.015 | +0.018 |
| persistent, OLS | 1.014 | −0.000 | +0.002 | +0.003 | +0.012 |
| persistent, 5-NN | 1.441 | −0.247 | −0.013 | +0.019 | +0.013 |
- For OLS all four estimates were within 0.012 of the truth, even on the persistent series: the linear model captures the dependence, so random folds are fine, as Bergmeir and colleagues showed.
- For 5-NN on the persistent series random K-fold was 17% too optimistic (−0.247 on 1.441). Blocked folds removed most of the bias, and the gap and rolling origin the rest.
- Choosing the number of neighbours (1, 5, 10 or 20) on the persistent series, 12 runs: random K-fold chose 5 three times, 10 seven times and 20 twice, rolling origin 5 three times and 10 nine times; the true RMSE of the chosen models was 1.495 and 1.489 (best possible 1.447). Here the leak shifted the level, not the ranking.
- Seeds 1 and 2: random K-fold bias for 5-NN on the persistent series −0.225 and −0.170; blocked with gap −0.024 and +0.075; rolling origin +0.029 and +0.154, the noisiest estimate because it uses the fewest test points.
Cross-validation for time series in practice
- Many series: with panels of series, folds can also be split by series, but time order within each series still matters.
- Purging and embargo: in finance, labels that span several days need training rows removed around each test block (M. López de Prado, 2018).
- Nested CV: tune hyperparameters in an inner rolling-origin loop and estimate error in an outer one.
- Cost: rolling origin refits the model many times; the next topic, backtesting, discusses how often to refit.
References
- C. Bergmeir, R. J. Hyndman and B. Koo (2018). “A note on the validity of cross-validation for evaluating autoregressive time series prediction.” Computational Statistics and Data Analysis.
- J. Racine (2000). “Consistent cross-validatory model-selection for dependent data: hv-block cross-validation.” Journal of Econometrics.
- P. Burman, E. Chow and D. Nolan (1994). “A cross-validatory method for dependent data.” Biometrika.
- L. J. Tashman (2000). “Out-of-sample tests of forecasting accuracy: an analysis and review.” International Journal of Forecasting.
- M. López de Prado (2018). Advances in Financial Machine Learning. Wiley.