Lag Features and Rolling Windows
Turning ordered history into tabular features
Feature-based forecasting treats time series as supervised learning: lag features, rolling statistics and calendar variables feed any regression model. Windowing discipline — as-of only — is what stops it from cheating.
What you will learn
- Build lag and rolling-window features correctly as-of prediction time
- Add calendar and external features safely
- Handle the boundary between feature engineering and leakage
What is this?
Any regression model can forecast if you encode the past as features: lag values (y_{t-1}, y_{t-7}), rolling stats (mean, std, min/max over windows), calendar effects (day of week, holiday flags, month), and external regressors (price, weather). Constructed with an immutable 'as-of' rule — each row in the training matrix holds only information available at its timestep — this turns forecasting into tabular ML where gradient boosting often dominates classical tools on rich features.
Why do you need it?
- It inherits the power of mature tabular models and libraries.
- Calendar and external features model effects no ARIMA recursion handles organically.
- Multiple horizons can be trained simultaneously by shifting the target.
- It scales to thousands of series with the same feature-builder.
Mental model
Reading a horse race from the last lap, the track and the weather
Multisignal feature windowing
You wouldn't predict the next finish from a single number — you'd pull last lap times (lags), the split averages (rolling windows), the track condition (calendar), and the odds (externals), each only as known as of the start of the stretch.
How it works
- Define the row timestamp t and the horizon h; the target is y_{t+h}.
- Featurize the history up to t: lags y_{t-k}, rolling mean/std over the last w, EWMA, min/max, counts.
- Add calendar features that are known in advance: dow, month, holiday, day-of-year.
- Add external regressors only if their t-values are available at prediction time.
- Train the model on (X_t, y_{t+h}) over history; forecast by walking forward the features.
From history to a supervised row at each t.
In production
- An e-commerce forecaster feeds 14 lags, 7-day rolling means and holiday flags to LightGBM — beating ARIMA at a 14-day horizon.
- A ride-hailing model predicts next-hour demand from last-4-hour counts + weather + city events.
- A platform forecasts churn odds per account per day by rolling features from the account's event stream.
Best practices
- Make the as-of rule code-reviewed: features must be computable at t or 'now'.
- Create per-horizon models when forecast dynamics differ sharply by horizon.
- Use neutral lags/rolling windows — never forward-looking stats.
- Backtest by walking the horizon forward, scoring each step as the features were known.
Common mistakes
- Including the target's future (or same-t row) in the feature matrix — silent leakage.
- Rolling windows computed on the full history before splitting, leaking future stats.
- Fitting a single model for h=1 and reporting it for h=30.
- Ignoring the calendar in favor of raw seasonal lags, then missing holidays each year.
Common issues
Champion lags dominate, model short-memory onlyLags (y_{t-1}) carry most signal; horizon dependence unclear
Train per-horizon models and shift targets, then inspect feature importance per horizon.
Rolling features computed differently in train vs live serveTrain rebuilt on history; live accumulates sparse windows
Extract features through one shared function with identical min-period semantics.
Debugging tips
- Corrupt roll/EWMA? Rebuild features inside each fold and compare to the pre-split fit.
- If the model's best feature is 'last hour' suspiciously powerful, check the feature was truly t-only.
- Backtest per horizon and per month; a holiday-month error here is common and telling.
- Drop one lag feature at a time to learn which memory scales carry signal.
Interview checkpoint
Quick quiz
The 'as-of' rule for feature construction requires:
A single model trained on y_{t+1} can be honestly reported at a 30-day horizon.