WRITE · TRACE · UNDERSTAND

Project: walk-forward forecasts with delayed labels and a purge gap

Rows have unique id and finite t, available, x,y; available>=t.

Read lesson: Project: walk-forward forecasts with delayed labels and a purge gap →

Your experiment

Rows have unique id and finite t, available, x,y; available>=t. Cutoffs are finite, unique and strictly increasing; horizon>0 and gap>=0 are finite. At each cutoff, fit only rows with t<=cutoff-gap and label available<=cutoff. Forecast rows cutoff<t<=cutoff+horizon. Fit the centered one-feature ridge formula with penalty .1. Return one window with cutoff,status; evaluated windows also contain train_ids,held_ids,predictions,mse. Return status insufficient if fewer than two fitting rows or no held rows. Evaluation uses offline future truth; it must not enter fitting.

Python runs in your browser. Your draft stays on this device.

What to look for

Review both clocks, the gap convention, overlapping windows and insufficient history. A production backtest also needs historical feature availability and immutable data snapshots. Replay follows the main orchestration function; helper computations execute and are checked separately.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Project: walk-forward forecasts with delayed labels and a purge gap works

The idea

A forecasting model should be tested the way it will be used: standing at a point in time, knowing only what was known then, and predicting what comes next. Shuffled cross-validation breaks this, because it trains on the future. Walk-forward evaluation moves a cutoff through time and repeats the fit and the forecast at each one.

walkforward_forecast(rows, cutoffs, horizon, gap) adds two details that matter in practice. A label often becomes known later than its event, so a row is used for fitting only if its available time is at or before the cutoff. A purge gap also drops the most recent events before the cutoff, whose outcomes may overlap the forecast period. Rows with cutoff < t <= cutoff + horizon are forecast and scored.

How it works

  1. Validate the inputs: finite, strictly increasing cutoffs, horizon > 0, gap >= 0, unique IDs, and no label available before its event.
  2. At each cutoff, select fitting rows with t <= cutoff - gap and available <= cutoff.
  3. Select held rows with cutoff < t <= cutoff + horizon.
  4. If there are fewer than two fitting rows or no held rows, record the window with status insufficient.
  5. Otherwise fit the centred one-feature ridge with penalty 0.1 and predict each held row from its x.
  6. Store the cutoff, train_ids, held_ids, predictions and mean squared error, then move to the next cutoff.

What it costs

Time
O(cutoffs · rows)
Space
O(rows) per window
Fits
one per cutoff

Each cutoff rescans all rows to pick the fitting and held sets and then fits one small model, so the cost grows with rows times cutoffs.

Where it is used

  • scikit-learn's TimeSeriesSplit produces expanding training windows and has a gap parameter that leaves space before each test fold.
  • Quantitative finance backtests use purged cross-validation, described by Marcos Lopez de Prado, to stop overlapping labels from leaking into training.
  • Forecasters evaluate models on a rolling forecasting origin, the method described in Hyndman and Athanasopoulos' textbook on forecasting.

Watch out for

  • Filtering by event time only: a label that arrives after the cutoff was not known yet, so fitting on it is a leak.
  • Shuffling rows into random folds, which lets the model learn from events that happen after the ones it predicts.
  • Tuning settings on the same walk-forward windows that produce the reported error, which makes the backtest look better than live use.