Time Series & Forecasting

Smooth the Wiggles

Ice-cream sales bounce up and down every day. Average a few days together and the real pattern shows through, but average too many and you arrive late.

Each dot is one day of sales. Slide Days to average up and watch the line calm down. Which size follows the pattern best?

Ice-cream cones sold per day

daily salesmoving averagetrend linereal pattern (hidden)

Around the middle a heat wave makes sales jump up. See how quickly your line notices it.

Challenges

Sweet spot: with past days only, get within 15% of the smallest possible error.
Too slow: use 30 or more days and see the line arrive 8 or more days late.
Peek ahead: switch to past and future, and find a size within 15% of its best.
Straight on: draw the straight trend line to see how fast sales grow.

What just happened?

For grown-ups: what's really going on

A moving average is a low-pass filter: st = (1/w) Σ yt−j. It trades variance (noise left in the line, which shrinks about like 1/w) against bias (real changes smeared out, which grows with w). The best window sits where the two balance.

  • Trailing windows use only the past, so they are causal and lag by about (w−1)/2 steps. Centered windows use past and future, so they have no lag but cannot be used for live forecasting.
  • The Off by number is the root-mean-square distance between the smoothed line and the hidden true pattern, which a real analyst would not know. The simulator knows it, so it can show the bias-variance trade-off honestly. Lag is the shift of the true pattern that best matches the smoothed line.
  • The trend line is an ordinary least-squares fit y = a + b·t, which minimises squared error. It cannot follow the jump, which shows why one straight line is a model with strong assumptions.
  • Next steps: exponential smoothing, LOESS and Savitzky-Golay filters use weighted windows that react better.

Further reading: Hyndman and Athanasopoulos, Forecasting: Principles and Practice, ch. 3 (time series decomposition and moving averages).