Objective
Walk-forward validation simulates what you would actually have done: fit on the past, trade the next period, refit, repeat. You should be able to choose between rolling and expanding windows, compute which training samples to purge when labels overlap, judge parameter stability, and report the stitched out-of-sample result without flattering it.
The procedure
Choose a training length, a test length and a refit frequency. For each step:
- Fit everything (parameters, feature scaling, model selection) on the training window only.
- Trade the following test block with that fit, frozen.
- Move forward by one test block and repeat.
The out-of-sample result is the concatenation of the test blocks. Every number in it was produced by a model that had not seen it.
Rolling or expanding?
| Rolling window | Expanding window | |
|---|---|---|
| Training data | last periods | everything up to now |
| Strength | adapts to regime change | lower estimation noise |
| Weakness | noisier parameters | slow to forget old regimes |
Expanding windows suit slow-moving, stable relationships (risk premia estimated over decades); rolling windows suit relationships believed to drift (microstructure, short-horizon signals). Choosing the window length by looking at results is another trial and belongs in the log.
Purging and embargo
Labels in finance often span several periods, for example a 20-day forward return. If sample has features known at day and a label from the returns of days to , then training samples just before the test block have labels that use returns inside the test block. Training on them leaks test-period information. Purging removes every training sample whose label is not fully realised before the test block starts: with test starting at day , keep only with , which drops the last samples.
An embargo additionally drops training samples just after a test block. It matters in k-fold or combinatorial cross-validation, where training data can follow the test fold and serially correlated features carry information backwards. In pure walk-forward, training always precedes the test, so the purge is what matters.
Parameter stability
Record the parameters chosen in each window. A real effect tends to give a plateau: neighbouring parameter values perform similarly, and the chosen value wanders within a stable range. Overfitting gives a spike: one value works, its neighbours do not, and the chosen value jumps between windows. Report the distribution of chosen parameters and the performance surface around them, not only the winner.
Reporting honestly
- Stitch, do not average. Compute the Sharpe ratio of the concatenated test returns. The average of fold Sharpe ratios gives a calm fold with a high Sharpe as much weight as a volatile losing fold.
- Report the in-sample to out-of-sample ratio, the worst fold, maximum drawdown of the stitched series, turnover and net-of-cost figures.
- State everything you chose after seeing walk-forward results (window length, refit frequency, model class): the walk-forward result is out of sample only for choices made before it was run.
Worked example
a purged walk-forward splitter
Write a splitter for 1500 daily samples with 1000-day training windows, 250-day test blocks and 20-day forward-return labels. Which training samples does the first split use under a rolling and under an expanding window?
Show solutionHide solution
def walk_forward(n, train, test, h, expanding=False):
"""Yield (train_idx, test_idx) for samples 0..n-1.
Sample t has features known at the close of day t and a label built from
the returns of days t+1..t+h. A training label must be fully realised
before the test block starts, so the last h samples are purged.
"""
start = train
while start + test <= n:
lo = 0 if expanding else start - train
train_idx = [t for t in range(lo, start) if t + h < start]
test_idx = list(range(start, start + test))
yield train_idx, test_idx
start += test
for expanding in (False, True):
print("expanding" if expanding else "rolling")
for tr, te in walk_forward(n=1500, train=1000, test=250, h=20, expanding=expanding):
print(f" train {tr[0]}-{tr[-1]} ({len(tr)}), test {te[0]}-{te[-1]}")
Output:
rolling
train 0-979 (980), test 1000-1249
train 250-1229 (980), test 1250-1499
expanding
train 0-979 (980), test 1000-1249
train 0-1229 (1230), test 1250-1499
The first test block starts at day 1000. Sample 979 has a label using days 980 to 999 and is kept; sample 980's label reaches day 1000, inside the test block, so samples 980 to 999 (twenty of them, ) are purged. The same 20-day gap appears before every test block. Without it, the last training labels overlap the first test labels and the fit has seen part of the test period.
Worked example
stitching folds
A walk-forward run has three one-year test folds with annualised mean returns and volatilities of , and . Compare the average of the fold Sharpe ratios with the Sharpe ratio of the stitched series.
Show solutionHide solution
Average of fold Sharpes. , and average to .
Stitched series. The mean is a year. With daily data the variance of the stitched series is essentially the average of the fold variances (differences between fold means contribute a negligible amount at daily frequency):
The stitched Sharpe ratio is . Averaging fold Sharpes reports for a strategy that barely made money, because it counts the calm profitable years at the same weight as the volatile losing one. The stitched figure is the one an investor would have experienced, and the maximum drawdown of the stitched series, dominated by the third fold, belongs next to it.
Knowledge check
Labels are 10-day forward returns and a walk-forward test block starts on day 500. Which training samples must be purged from a window ending on day 499?
Knowledge check
Across walk-forward windows the optimal lookback jumps between 5, 60, 12 and 200 days, and neighbouring lookbacks perform poorly in each window. What does this most suggest?
Knowledge check
When is an embargo after the test block most needed?
Key takeaways
- Fit on the past only, trade the next block frozen, roll forward, and report the concatenated test blocks.
- Rolling windows adapt but are noisy; expanding windows are stable but slow. The choice is a trial.
- Purge training samples whose labels are not realised before the test block ( samples for -period labels); embargo matters when training data follow the test.
- Look for parameter plateaus, not spikes, and report the chosen parameters per window.
- Stitch test returns before computing Sharpe ratios and drawdowns; never average fold Sharpe ratios.
Practice problems
Finished reading?
Answer the 3 knowledge checks to complete this lesson (0 done).