skills/timeseries/expanding-window-stacking/SKILL.md
Walk-forward stacking ensemble that trains base models on expanding windows and a meta-learner on their out-of-fold predictions across time
npx skillsauth add wenmin-wu/ds-skills timeseries-expanding-window-stackingInstall 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.
Standard K-fold stacking leaks future information in time series. Expanding-window stacking respects temporal order: base models are trained on data up to time t, predict t+1, and the meta-learner trains on these walk-forward out-of-fold predictions. This produces a properly validated stacking ensemble for forecasting tasks.
import numpy as np
from sklearn.linear_model import Ridge
def expanding_stack(df, base_models, time_col="month", target="y"):
periods = sorted(df[time_col].unique())
oof_preds = np.zeros((len(df), len(base_models)))
for i, cutoff in enumerate(periods[12:], start=12): # min 12 months training
train_mask = df[time_col] < cutoff
val_mask = df[time_col] == cutoff
for j, model in enumerate(base_models):
model.fit(df.loc[train_mask, features], df.loc[train_mask, target])
oof_preds[val_mask, j] = model.predict(df.loc[val_mask, features])
# Train meta-learner on OOF predictions
valid_mask = df[time_col] >= periods[12]
meta = Ridge(alpha=1.0)
meta.fit(oof_preds[valid_mask], df.loc[valid_mask, target])
return meta, oof_preds
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