212. Simple Moving Average

212.1. SMA (Simple Moving Average)

For a time series 𝑦𝑡, the SMA of window size 𝑘 at time 𝑡 is:

𝑠𝑡=1𝑘𝑖=0𝑘1𝑦𝑡𝑖

The average of the last 𝑘 values. Equal weights 1/𝑘 on each. The forecast is 𝑥̂𝑡,𝑡+1=𝑠𝑡.

212.1.1. Choosing the window size 𝑘

The window size trades two effects:

Rules of thumb:

212.1.2. Lag

SMA lags the true level. If the underlying series is rising linearly, the SMA tracks roughly 𝑘/2 time steps behind — because the average of the last 𝑘 values has a center of mass at 𝑡(𝑘1)/2. Larger 𝑘 → larger lag.

This is why SMA is poor for trending series — it’s structurally late. Use ETS or ARIMA when there’s trend.

212.1.3. Visualization

The SMA filters the high-frequency noise and produces a smoother curve, but with a horizontal lag relative to the data.

Example

𝑘=3.

Data

𝑡12345678
𝑥𝑡1020302012243624

Init — no parameters to seed; the first forecast is at 𝑡=4 once we have 𝑘=3 observations.

First forecast — at 𝑡=4, using 𝑥1,𝑥2,𝑥3:

𝑥̂3,4=10+20+303=20,𝑒4=2020=0

Iterate:

𝑡𝑥𝑡𝑥̂𝑡1,𝑡𝑒𝑡
420(10 + 20 + 30) / 3 = 20.000.00
512(20 + 30 + 20) / 3 = 23.33−11.33
624(30 + 20 + 12) / 3 = 20.673.33
736(20 + 12 + 24) / 3 = 18.6717.33
824(12 + 24 + 36) / 3 = 24.000.00
9(24 + 36 + 24) / 3 = 28.00
sma.py
pl.col('X').rolling_mean(window_size)