Time Series ForecastingFeature Engineering (Lag Features, Rolling Stats, Seasonality)Medium⏱️ ~2 min

Rolling Statistics and Window Aggregations

What Rolling Statistics Capture

Rolling statistics summarize recent history: 7-day rolling mean captures the recent baseline level, 7-day rolling standard deviation captures recent volatility. Unlike point lags, rolling statistics smooth noise and extract persistent signals. A 30-day rolling mean is more stable than lag-30 alone because it averages 30 observations rather than using just one.

Common Rolling Features: Mean (level), std (volatility), min/max (range), median (robust level), sum (volume), count (frequency). Each captures different aspects of recent behavior useful for forecasting.

Window Size Selection

Short windows (7 days) respond quickly to changes but are noisy. Long windows (90 days) are stable but slow to adapt. Use multiple window sizes to capture different scales: 7-day for recent trends, 30-day for monthly patterns, 90-day for quarterly baseline. Compare current value to rolling statistics: ratio of today's value to 30-day mean indicates whether demand is above or below recent normal.

Expanding vs Rolling Windows

Rolling windows have fixed size (last N days). Expanding windows grow over time (all history up to now). Rolling captures recent patterns; expanding captures lifetime patterns. Use expanding for cumulative metrics (lifetime purchases) and rolling for recent behavior (recent purchase frequency).

Implementation Tip: Compute rolling statistics with a gap to prevent leakage. For day-ahead forecasting, the 7-day rolling mean should exclude today (use days t-7 to t-1, not t-6 to t). The window must end before the prediction point.

Exponentially Weighted Statistics

Exponentially weighted moving averages (EWMA) assign more weight to recent observations without hard window cutoffs. Controlled by span or halflife parameter. EWMA responds faster to recent changes while maintaining smoothness. Useful when you want recency weighting without abrupt window boundaries.

💡 Key Takeaways
Rolling statistics smooth noise: 30-day mean averages 30 observations versus one point from lag-30
Use multiple window sizes: 7-day for recent, 30-day for monthly, 90-day for quarterly baseline
Compute with gap to prevent leakage: 7-day mean should use days t-7 to t-1, excluding prediction day
📌 Interview Tips
1Compare current to rolling stats: ratio of today to 30-day mean shows if demand is above/below recent normal
2Use expanding windows for lifetime metrics, rolling for recent behavior patterns
← Back to Feature Engineering (Lag Features, Rolling Stats, Seasonality) Overview
Rolling Statistics and Window Aggregations | Feature Engineering (Lag Features, Rolling Stats, Seasonality) - System Overflow