Foundations to Frontier
Tests

Backtesting

A backtest replays history: at each past moment it trains the model on what was known then, makes the forecast, and scores it against what happened next. It is the most honest test of a forecasting system, but only if the replay is faithful. This page builds a walk-forward backtest, studies how the window length and refit frequency trade precision against adaptation when the series changes regime, and shows how look-ahead bias, such as a centred moving average, produces backtests that are too good to be true.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Replaying the past honestly

To see how a weather forecaster would have done last year, you could give them each morning only the information that existed that morning and compare their forecast with the next day’s weather. That is a backtest. It only works if nothing from the future slips in: not tomorrow’s newspaper, not data that were corrected weeks later, and not a smoothed curve that was drawn using later days.

Time-series lab
An AR(1) series whose level jumps from 0 to 3 at step 400 (dashed) while φ falls from 0.6 to 0.3; one-step forecasts (dark) from step 200 with an expanding window, refitted every 1 step. RMSE (noise sd 1): before the change 0.98, first 50 steps after 1.74, later 1.12. Short windows and frequent refits adapt faster after the change; long windows are more precise while nothing changes.
Why engineers care

Many trading strategies that looked brilliant in backtests failed with real money because the backtest had quietly used information that was not available at the time.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A backtest gives the model at each moment…
  2. 2. Look-ahead bias means…
  3. 3. A backtest that is too good to be true usually…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Walk-forward backtest: for each time t, fit on data before t, forecast t (or t + h), then move on.
  • Expanding window: train on all past data; most precise when nothing changes.
  • Sliding (rolling) window: train on the last w points; forgets old regimes, at the cost of noisier estimates.
  • Refit frequency: refitting every step is most up to date but costly; between refits the model is reused.
  • Look-ahead bias: any feature, scaling, model choice or data value that uses information after the forecast time, for example centred moving averages, scalers fitted on all data, or final revised data instead of what was first published.
  • Point-in-time (vintage) data: values as they were known at each date, including later revisions.
  • Regime change: the process changes (level, dynamics), and older data become misleading.
📝 Quick check (Level 1)
0/3 answered
  1. 1. A sliding window…
  2. 2. Point-in-time data contain values…
  3. 3. A centred moving average at time t uses…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

A walk-forward backtest

Exercise 1.

  • Forecast the series 0, 1, …, 9 by the mean of the training data, starting at step 4.
  • Compare an expanding window, a sliding window of 2, and refitting every 3 steps.

Practice.

  • Freeze everything at forecast time: features, scalers, hyperparameters and data must be what was available then.
  • Use point-in-time data where values get revised (economics, accounting, sensor corrections).
  • Mimic the real schedule: the refit frequency, data delays and forecast horizon of the production system.
  • Report errors by period, especially around known changes; one overall number hides when the model fails.
  • Distrust results better than the noise level or far better than simple benchmarks; look for leaks first.
  • Tools: sktime and darts backtesting, statsforecast cross_validation, backtrader and vectorbt in finance.
def backtest(y, fit, predict, start, window=None, refit_every=1):
    f, model = {}, None
    for t in range(start, len(y)):
        if model is None or (t - start) % refit_every == 0:
            model = fit(y[(0 if window is None else t - window):t])
        f[t] = predict(model, y[:t])
    return f
🐍 Exercise 1: walk-forward backtest
Write a walk-forward backtest with an expanding or sliding window and a refit interval, and check it with a mean forecaster on a straight-line series.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A result better than the noise level suggests…
  2. 2. Data that get revised should be used…
  3. 3. Errors should be reported…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Precision versus adaptation. With a stable process, the variance of fitted parameters falls like 1/w with the window length w, so longer windows forecast better. After a change, a window that still contains old data estimates a mixture of the two regimes; its bias shrinks only as old data leave the window. A sliding window of w points needs about w steps to forget, and an expanding window never fully forgets. Refitting every r steps adds, on average, (r − 1)/2 steps of staleness.

Why look-ahead is so easy to miss. A centred 5-point average at t − 1 uses y[t − 3], …, y[t + 1], so it contains the value y[t] being forecast. A regression on it learns to read the answer off the input. Other leaks are subtler: a scaler fitted on all data knows the future mean, and a model chosen because it had the best backtest is partly fitted to the backtest itself.

A test for leaks. Change every value after time t and recompute every feature up to t. If anything changes, the feature uses the future.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Refitting every r steps adds an average staleness of…
  2. 2. An expanding window after a regime change…
  3. 3. To test a feature for look-ahead…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

When a series changes regime, how do window length and refit frequency affect backtest errors before and after the change, and what does look-ahead bias do to the result?

History.

  • L. J. Tashman (2000) set out rolling-origin evaluation for forecasting.
  • D. Croushore and T. Stark (2001) built a real-time (vintage) data set for US macroeconomics, showing how revisions change backtests.
  • D. H. Bailey, J. M. Borwein, M. López de Prado and Q. J. Zhu (2014) quantified backtest overfitting in finance.

Exercise 2.

  • AR(1) noise (sd 1) around level 0 with φ = 0.6; at step 600 the level jumps to 3 and φ falls to 0.3; 1000 steps, 20 runs.
  • One-step forecasts from step 300 by AR(1) with intercept, scored in three periods.
  • Also a regression of y[t] on a 5-point moving average at t − 1: trailing (honest) or centred (look-ahead).

Seed 0, one-step RMSE:

backtest before (300–599) just after (600–699) later (700–999)
expanding, refit every step 1.005 1.390 1.203
expanding, refit every 100 1.006 1.597 1.221
sliding 50, refit every step 1.027 1.129 1.008
sliding 100, refit every step 1.014 1.195 0.999
sliding 300, refit every step 1.007 1.310 1.075
sliding 100, refit every 25 1.015 1.300 1.001
sliding 100, refit every 100 1.014 1.628 0.999
trailing 5-point average as input 1.171 1.443 1.130
look-ahead: centred 5-point average 0.809 0.861 0.825
  • Before the change, longer windows were slightly better (1.005 expanding against 1.027 for 50 points).
  • After the change the order flipped: just after it the 50-point window had 1.129 and the expanding window 1.390, and 400 steps later the expanding window was still 20% worse than a 100-point window (1.203 against 0.999), because it never forgets the old regime.
  • Refitting rarely cost most right after the change: refitting every 100 steps raised the error just after it from 1.195 to 1.628 for the 100-point window, but made no difference later.
  • The look-ahead backtest beat the noise level (0.81 with noise sd 1), which no real forecaster can do, while the honest trailing version had 1.17.
  • Seeds 1 and 2 gave the same pattern: expanding window just after the change 1.423 and 1.404, later 1.219 and 1.206; 100-point window later 1.007 and 0.995; look-ahead before the change 0.796 and 0.810.
🐍 Exercise 2 (research): when a series changes regime, how do window length and refit frequency affect backtest errors before and after the change, and what does look-ahead bias do to the result?
Backtest an AR(1) forecaster across a regime change with expanding and sliding windows and several refit intervals, and compare an honest trailing-average input with a centred one that leaks the future.

Backtesting in practice

  • Data delays: in production a value may arrive days late; the backtest must use the same delay.
  • Multiple testing: trying many strategies on the same history and reporting the best inflates its performance; hold out a final period or correct for the number of trials.
  • Change detection: the change-point methods of an earlier topic can trigger refits or window resets instead of a fixed schedule.
  • Costs: in finance, include transaction costs and slippage; in operations, include the cost of acting on each forecast.

References

  1. L. J. Tashman (2000). “Out-of-sample tests of forecasting accuracy: an analysis and review.” International Journal of Forecasting.
  2. D. Croushore and T. Stark (2001). “A real-time data set for macroeconomists.” Journal of Econometrics.
  3. D. H. Bailey, J. M. Borwein, M. López de Prado and Q. J. Zhu (2014). “Pseudo-mathematics and financial charlatanism: the effects of backtest overfitting on out-of-sample performance.” Notices of the AMS.
  4. M. H. Pesaran and A. Timmermann (2007). “Selection of estimation window in the presence of breaks.” Journal of Econometrics.
  5. R. J. Hyndman and G. Athanasopoulos (2021). Forecasting: Principles and Practice, 3rd edition. OTexts.