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.