Forecast calibration in a rolling backtest
A backtest that reports only point error says whose forecast was closest, not whether the uncertainty each forecaster claimed was honest. This example backtests a Bayesian seasonal local-level model against the seasonal naive baseline on synthetic monthly data and compares them on point accuracy, interval coverage, interval width and PIT values.
The data are generated from a seasonal local-level process, so the model is close to correctly specified here. On real data neither forecaster has that advantage.
Run it with python examples/forecast_calibration.py from the repository root. The blocks below are
cells of that one file and share its state, so they assume the blocks above
them have run.
Simulate monthly data and backtest at rolling origins
rng = np.random.default_rng(2026)
period, months = 12, 84
pattern = np.array([-8.0, -5.0, -2.0, 1.0, 4.0, 7.0, 10.0, 8.0, 4.0, 0.0, -4.0, -5.0])
pattern -= pattern.mean()
level = 100.0 + np.cumsum(rng.normal(0.0, 1.0, months))
y = level + pattern[np.arange(months) % period] + rng.normal(0.0, 2.0, months)
model = r.BayesianSeasonalLocalLevel(
period=period,
level_variance_prior=r.InverseGammaPrior(3.0, 2.0),
seasonal_variance_prior=r.InverseGammaPrior(3.0, 1.0),
observation_variance_prior=r.InverseGammaPrior(3.0, 8.0),
initial_level=100.0,
initial_seasonal_effects=np.zeros(period),
initial_level_variance=25.0,
initial_seasonal_variance=25.0,
)
# One fit per origin, each seeing only the months before it. baseline_period=12
# makes the baseline a seasonal naive forecast with bootstrapped seasonal
# differences, so it has intervals to check as well.
result = r.backtest(model, y, horizon=6, initial=48, step=2, baseline_period=period,
fit_kwargs={"chains": 2, "draws": 500, "warmup": 500})
print(f"{len(result.folds)} origins, {len(result.errors)} failed fits")
Accuracy and calibration side by side
# compare_backtests scores only the origins both forecasters completed.
# period=12 scales MASE by the seasonal naive error within each fold's training
# data.
comparison = r.compare_backtests(
{"seasonal model": result, "seasonal naive": result.baseline()}, period=period)
print(comparison)
16 common origins
metric seasonal model seasonal naive
mae 4.303 6.562
rmse 5.388 7.68
wape 0.03867 0.05897
mase 1.118 1.674
crps 3.11 4.891
wis 2.455 3.875
coverage_0.5 0.375 0.3125
width_0.5 5.218 6.496
coverage_0.8 0.6979 0.4896
width_0.8 10.05 13.03
coverage_0.95 0.8438 0.6667
width_0.95 15.46 18.29
coverage_error 0.114 0.2061
count 96 96
Reading the comparison
The first six rows score accuracy. MAE, RMSE, WAPE and MASE score the predictive mean; CRPS and WIS score the whole predictive distribution. MASE divides each error by the mean absolute 12-month difference in that fold's training data and averages the results. That is not the baseline's error on these outcomes, so a multi-step forecast can beat the baseline and still have a MASE above 1.
coverage_<level> is the share of outcomes inside the central interval of that
nominal level, and width_<level> is that interval's mean width. A forecaster
can reach good coverage with wide, uninformative intervals, so read the two
together. coverage_error averages the gap between empirical and nominal
coverage over 19 levels from 5% to 95%.
Here the model is more accurate than the baseline and its intervals are narrower, and both cover less often than nominal. One series with 16 overlapping origins is a small sample for a coverage rate, as the next table shows.
Coverage by horizon step
table = result.calibration(levels=(0.5, 0.8, 0.95))
for step, (coverage, lower, upper, width) in enumerate(zip(
table["coverage"], table["coverage_lower"], table["coverage_upper"], table["width"]), start=1):
cells = " ".join(f"{c:.2f} [{lo:.2f}, {hi:.2f}] w={w:.1f}"
for c, lo, hi, w in zip(coverage, lower, upper, width))
print(f"step {step}: {cells}")
step 1: 0.38 [0.18, 0.61] w=4.5 0.69 [0.44, 0.86] w=8.6 0.94 [0.72, 0.99] w=13.2
step 2: 0.38 [0.18, 0.61] w=4.8 0.62 [0.39, 0.82] w=9.1 0.81 [0.57, 0.93] w=13.9
step 3: 0.38 [0.18, 0.61] w=5.0 0.69 [0.44, 0.86] w=9.7 0.88 [0.64, 0.97] w=14.9
step 4: 0.38 [0.18, 0.61] w=5.5 0.69 [0.44, 0.86] w=10.5 0.75 [0.51, 0.90] w=16.0
step 5: 0.38 [0.18, 0.61] w=5.6 0.75 [0.51, 0.90] w=10.9 0.75 [0.51, 0.90] w=16.9
step 6: 0.38 [0.18, 0.61] w=5.9 0.75 [0.51, 0.90] w=11.4 0.94 [0.72, 0.99] w=17.8
How much the intervals in brackets can be trusted
The bracketed ranges are 95% Wilson intervals for each coverage rate. They treat every outcome as an independent trial, and these outcomes are not independent: origins two months apart share most of their training data and four of their six target months. Overlapping errors like these are usually positively correlated, and then the true uncertainty is wider than shown, by an amount the table cannot estimate. Use them to spot large gaps, not as a test.
PIT histogram
# A calibrated forecaster's PIT values are roughly uniform: about equal counts per
# bin. Too many in the outer bins means intervals that are too narrow, too many in
# the middle means intervals that are too wide. The same dependence applies, so
# this is a description, not a test, and rustmc does not compute a p-value for it.
for name, backtest in (("seasonal model", result), ("seasonal naive", result.baseline())):
print(f"{name:>15}:", backtest.pit_histogram(bins=5))