skills/timeseries/bootstrapped-residual-prediction-intervals/SKILL.md
Generate prediction intervals by repeatedly sampling from model residuals, adding to point forecasts, and taking quantiles across synthetic futures
npx skillsauth add wenmin-wu/ds-skills timeseries-bootstrapped-residual-prediction-intervalsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Given a fitted model's point forecasts and its training residuals, generate prediction intervals without parametric assumptions. Repeatedly sample residuals with replacement, add them to the point forecast to create synthetic future paths, then take quantiles across all paths. This captures the empirical error distribution and naturally adapts interval width to model accuracy.
import numpy as np
def bootstrap_prediction_intervals(point_forecast, residuals,
quantiles=(0.025, 0.975),
n_bootstrap=1000):
"""Generate prediction intervals via residual bootstrap."""
n_steps = len(point_forecast)
paths = np.zeros((n_bootstrap, n_steps))
for i in range(n_bootstrap):
sampled = np.random.choice(residuals, size=n_steps, replace=True)
paths[i] = point_forecast + sampled
intervals = {}
for q in quantiles:
intervals[q] = np.quantile(paths, q, axis=0)
intervals['median'] = np.median(paths, axis=0)
return intervals
residuals = y_train - model.predict(X_train)
intervals = bootstrap_prediction_intervals(
point_forecast=model.predict(X_test),
residuals=residuals.flatten(),
quantiles=[0.005, 0.025, 0.165, 0.25, 0.75, 0.835, 0.975, 0.995],
)
data-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF