Backtesting forecasts
Evaluate forecasts with expanding or sliding windows, never by shuffling time, and score them with MASE.
Lesson 55 of 6610 minIntermediate
You will learn to
- Run an expanding-window backtest and explain each fold
- Compare expanding and sliding windows
- Read MASE and explain why it is safer than MAPE
Before this lesson
Intuition
To trust a forecaster, you test it the way it will be used: stand at some past date, pretend you do not know what happened after it, forecast, and then compare with what really happened. Doing this once gives a noisy answer, so you repeat it at several past dates. That is a backtest.
The one rule is that time only moves forward. Training data must come before the data you score. If you shuffle the rows, as ordinary cross-validation does, the model learns from the future it is asked to predict, and the score is too optimistic. This is the time-series version of the rule from the cross-validation lesson.
Two questions remain. How much history should each fold train on? And how do you turn the errors into one number that you can compare across models and series? The first leads to expanding and sliding windows, the second to MASE.
Picture it
Each row is one fold. Grey is the history the model trains on, blue is the block of future periods it is scored on, and dashed is data not used in that fold. In the expanding window, the training part grows. In the sliding window, it keeps the same length and moves forward.
The math
Let the training series have points, the horizon be periods, and be the number of folds. If each fold moves forward by periods and the last fold ends at the end of the training series, the first fold trains on
periods. Fold trains on the first points (expanding) or on the last points before its scored block (sliding), and is scored on the next points.
The mean absolute scaled error compares the forecast errors with the errors of a seasonal naive forecast on the training data:
where is the seasonal period (1 for a non-seasonal series, which makes the denominator the mean error of "repeat the last value"). The numerator is the mean absolute error of the model over the horizon; the denominator is a fixed scale that comes from the training data. MASE below 1 beats the baseline, above 1 loses to it.
By contrast, MAPE averages . It is undefined at , explodes near zero and punishes over-forecasts more than under-forecasts.
Worked example
Fold layout. A training series has 60 months, the horizon is 6 and there are 3 folds. The first fold trains on months.
| Fold | Expanding: train on | Sliding (window 42): train on | Scored on |
|---|---|---|---|
| 1 | months 1 to 42 | months 1 to 42 | months 43 to 48 |
| 2 | months 1 to 48 | months 7 to 48 | months 49 to 54 |
| 3 | months 1 to 54 | months 13 to 54 | months 55 to 60 |
The expanding window uses all history and is best when the behaviour is stable. The sliding window forgets old data, which helps when the series drifts, for example after a change in the business.
MASE. The training values are 10, 12, 11, 13 with no seasonality (m = 1).
- The naive errors on the training data are |12 - 10| = 2, |11 - 12| = 1, |13 - 11| = 2. Their mean is 5 / 3, about 1.667.
- The model forecasts 13 and 16 for two periods where the actual values are 14 and 15. The errors are 1 and 1, so the MAE is 1.
- MASE = 1 / 1.667 = 0.6. The model makes 40% smaller errors than the naive baseline.
MAPE trap. If an actual value is 0.5 and the forecast is 1.5, the error is 1 and the percentage error is 200%, even though the miss is only one unit. One such point can dominate the average.
How mlflow does it
- Time-ordered folds only. In a forecasting pipeline the Forecast settings card has CV folds and a Fold strategy of Expanding or Sliding. Random folds and group columns are not available for time series.
- Fold layout. Each fold trains on the past and is scored on the next horizon of periods. The step between folds equals the horizon and the last fold ends at the end of the training series, as in the table above. If the settings need more rows than the dataset has, mlflow asks you to lower the horizon or the number of folds.
- Hold-out. The last Forecast horizon periods are held out to score the models after the folds.
- Default metric. Models are ranked by MASE (lower is better). Other metrics such as MAE, RMSE, MAPE and SMAPE are also available.
- Where to look. The Leaderboard tab of a run shows the scores. The Forecast tab draws the forecast against the actual values.
Common mistakes
- Shuffling the rows. The model sees the future during training and the score is optimistic. Always use time-ordered folds.
- Too few folds or too short a horizon. A single short block can be an unusual period. Use several folds, and a horizon like the one you will forecast in production.
- Comparing MAPE across series. A series with values near zero will dominate. Prefer MASE for comparisons across series or models.
- Choosing the window by habit. Expanding is a good default, but if the series changed regime, a sliding window may score better. Try both and compare.
- Tuning on the backtest and reporting the same score. Once you have picked a model by its fold scores, those scores are optimistic. Check the final choice on the held-out horizon.
Try it in mlflow
Open the Forecast tab
- Open Runs and choose a run that forecasts a time series.
- Open the Forecast tab and read the forecast against the actual values.
- Open the Leaderboard tab and find the MASE column; compare the best model with the naive baseline.