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.

Expanding windowFold 1Fold 2Fold 3Sliding windowFold 1Fold 2Fold 3TrainScoreNot usedTime→
Three folds of an expanding backtest and of a sliding backtest over the same series.

The math

Let the training series have nn points, the horizon be HH periods, and KK be the number of folds. If each fold moves forward by HH periods and the last fold ends at the end of the training series, the first fold trains on

w=n−((K−1) H+H)=n−KHw = n - \big((K-1)\,H + H\big) = n - K H

periods. Fold kk trains on the first w+(k−1)Hw + (k-1)H points (expanding) or on the last ww points before its scored block (sliding), and is scored on the next HH points.

The mean absolute scaled error compares the forecast errors with the errors of a seasonal naive forecast on the training data:

MASE=1H∑j=1H∣yT+j−y^T+j∣1n−m∑t=m+1n∣yt−yt−m∣\text{MASE} = \frac{\frac{1}{H}\sum_{j=1}^{H}\lvert y_{T+j}-\hat{y}_{T+j}\rvert}{\frac{1}{n-m}\sum_{t=m+1}^{n}\lvert y_t-y_{t-m}\rvert}

where mm 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 ∣y−y^∣/∣y∣\lvert y - \hat{y}\rvert / \lvert y \rvert. It is undefined at y=0y = 0, 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 w=60−3×6=42w = 60 - 3 \times 6 = 42 months.

FoldExpanding: train onSliding (window 42): train onScored on
1months 1 to 42months 1 to 42months 43 to 48
2months 1 to 48months 7 to 48months 49 to 54
3months 1 to 54months 13 to 54months 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).

  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.
  2. 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.
  3. 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

  1. Open Runs and choose a run that forecasts a time series.
  2. Open the Forecast tab and read the forecast against the actual values.
  3. Open the Leaderboard tab and find the MASE column; compare the best model with the naive baseline.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. You have 60 months of training data, a horizon of 6 and 3 folds with an expanding window. How many months does the first fold train on?
  2. 2. A forecast has MASE = 1.4. What does that say?
  3. 3. Why is MAPE a poor choice for a series that sometimes gets close to zero?