Sub topic 3
Measuring forecast error
This chapter assumes you know basic NumPy and the idea of a train/test split. You should also know what a forecast is: a prediction for time steps the model has not seen. Here you will learn four error metrics and when each one is the right choice.
Why the metric matters
Every forecast is wrong by some amount. The metric turns those misses into one number you can compare across models and use to tune them. Different metrics punish different mistakes, so pick the one that matches the cost of errors in your problem.
Let e_t = y_t - ŷ_t be the error at step t, where y_t is the actual value and ŷ_t is the forecast.
The four common metrics
| Metric | Formula | Unit | Behavior |
|---|---|---|---|
| MAE | mean of `abs(e_t)` | Same as the target | Easy to explain. Treats all errors linearly. |
| RMSE | square root of the mean of `e_t²` | Same as the target | Penalizes large errors more. Sensitive to outliers. |
| MAPE | mean of `abs(e_t / y_t)` | Percent | Scale-free, but breaks when `y_t` is zero or near zero. |
| MASE | MAE divided by the MAE of a naive forecast on the training data | Unitless | Scale-free. Below 1 means better than the in-sample naive forecast. |
MAE and RMSE depend on the units of the series. You cannot average them across series that have different scales. MAPE fixes the scale problem, but it explodes for values near zero. It also penalizes over-forecasts and under-forecasts unequally.
MASE divides your error by the average one-step change in the training data. For seasonal data, use the seasonal period m instead (for example m=7 for daily data with a weekly pattern). This ties the metric to the baselines you must beat, which the next chapter covers.
Computing them
import numpy as np
def mae(y, yhat):
return float(np.mean(np.abs(y - yhat)))
def rmse(y, yhat):
return float(np.sqrt(np.mean((y - yhat) ** 2)))
def mape(y, yhat):
return float(np.mean(np.abs((y - yhat) / y)) * 100)
def mase(y, yhat, y_train, m=1):
# scale = average error of the (seasonal) naive forecast on training data
scale = np.mean(np.abs(y_train[m:] - y_train[:-m]))
return mae(y, yhat) / float(scale)
y_train = np.array([10, 12, 11, 13, 12, 14, 13, 15], dtype=float)
y_true = np.array([14, 16, 15], dtype=float)
y_pred = np.array([13, 17, 14], dtype=float)
print('MAE ', mae(y_true, y_pred))
print('RMSE', rmse(y_true, y_pred))
print('MAPE', round(mape(y_true, y_pred), 2), '%')
print('MASE', round(mase(y_true, y_pred, y_train), 2))This prints MAE 1.0, RMSE 1.0, MAPE 6.69 % and MASE 0.64. All three errors have size 1, so MAE and RMSE agree. If one error were 3 and the others 0, RMSE would be larger than MAE.
Common errors
- Scaling MASE with test data. Compute the scale from the training series only. Using test data leaks information about the period you are evaluating.
- Reporting MAPE on intermittent data. Series with zeros, such as daily sales of a slow-moving item, give infinite or meaningless values. Use MAE or MASE instead.
- Averaging per-series MAE across series with different scales. Large series dominate the average. Use MASE, or normalize first.
- Judging with one number only. Report at least MAE plus one other metric, and always compare against a baseline.
Exercise
A model's MASE on the test set is 1.3, computed with m=7 on daily data. What does that tell you, and what would you check next?
Show answer
The model's average error is 30% larger than the seasonal-naive forecast (the value from 7 days earlier) makes on the training data. It is not yet better than a trivial baseline. Check that the scale came from the training series only. Then compare against the naive and seasonal-naive forecasts on the same test rows, and revisit the features.