Sub topic 17
Prediction intervals with quantile loss
This chapter assumes you have the train, test and feature_cols objects from the earlier chapters, and that you know how the gradient boosting regressor is fitted. A point forecast gives one number. A prediction interval gives a range that the actual value should fall inside with a stated probability. Planning stock or staffing often needs that range more than the single number.
Quantiles and the pinball loss
A quantile is the value below which a given share of outcomes falls. The 0.9 quantile is exceeded only 10% of the time. To forecast a quantile, you train with the pinball loss instead of squared error. For quantile q, an under-prediction costs q per unit of error and an over-prediction costs 1 - q.
At q = 0.9, under-predicting is nine times as costly as over-predicting. The model therefore learns to predict high. At q = 0.5 both sides cost the same, and the result is the median, which is the same target that MAE optimises.
Fitting one model per quantile
Each quantile needs its own model. To get an 80% interval, fit the 0.1 and 0.9 quantiles. Also fit the 0.5 quantile as the central forecast.
import numpy as np
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.metrics import mean_pinball_loss
quantiles = [0.1, 0.5, 0.9]
preds = []
for q in quantiles:
m = HistGradientBoostingRegressor(
loss='quantile', quantile=q,
max_iter=300, learning_rate=0.05, random_state=0,
)
m.fit(train[feature_cols], train['y'])
preds.append(m.predict(test[feature_cols]))
# Independent models can cross (low > high). Sorting per row fixes that.
low, mid, high = np.sort(np.vstack(preds), axis=0)
y = test['y'].to_numpy()
coverage = np.mean((y >= low) & (y <= high))
print('coverage of 80% interval:', coverage)
for q, p in zip(quantiles, (low, mid, high)):
print(q, 'pinball loss:', mean_pinball_loss(y, p, alpha=q))Checking the interval
Two checks matter.
- Coverage. Count how often the actual value lands inside the interval. For an 80% interval you want about 0.80 on held-out data. A much lower value means the interval is too narrow and overconfident.
- Pinball loss. Compute it for each quantile on the test rows. Lower is better, and you can use it to compare feature sets or hyperparameters.
Measure both on data the models never saw. Use the same chronological split or walk-forward validation from the validation chapter. Coverage on training rows is nearly always too optimistic.
Practical notes
- Quantile models are fitted on the same lag, rolling and calendar features as the point model. The shift before rolling statistics still applies.
- For multi-step forecasts, error grows with the horizon. Direct models, one per horizon, can give each horizon its own interval width. A recursive forecast feeds predictions back in as lags, so its intervals from a single-step quantile model understate the uncertainty at long horizons.
- Pinball loss does not require normally distributed errors. This makes it suitable for skewed demand data.
Exercise
You fit quantile models at 0.05 and 0.95 and want a 90% interval. On walk-forward validation, only 76% of actual values fall inside. What does this tell you, and what can you do about it?
Show answer
The interval is too narrow, so it is overconfident. The model underestimates future error, often because the training period was calmer or the model overfit. You can use more extreme quantiles, widen the interval with an adjustment based on the validation residuals, add features that explain the variability, or regularise the model more strongly. Then check coverage again on walk-forward folds.