Moving Averages
Smooth data and forecast with moving averages
What You'll Learn
- Simple moving average
- Weighted moving average
- Exponential smoothing
- Choosing the right method
- Forecasting applications
Simple Moving Average (SMA)
Definition: Average of k most recent observations
Formula: MA_t = (Y_t + Y_{t-1} + ... + Y_{t-k+1}) / k
Example (3-period MA): Data: 100, 110, 105, 115, 120 MA_4 = (110+105+115)/3 = 110 MA_5 = (105+115+120)/3 = 113.3
Purpose:
- Smooth out noise
- Identify trends
- Simple forecasting
Choosing k (Period Length)
Smaller k (e.g., k=3):
- More responsive to changes
- Less smoothing
- More volatile
Larger k (e.g., k=12):
- More smoothing
- Less responsive
- Lags behind actual changes
Guidelines:
k = 3 to 5: Short-term smoothing
k = 12: Monthly data, remove seasonality
k = 7: Weekly data, smooth daily variation
Rule: Match k to seasonal period for deseasonalizing
Centered Moving Average
Problem with SMA: Places average at end of period → Lags behind data
Solution: Center the average
Example (3-period centered): MA_2 = (Y_1 + Y_2 + Y_3) / 3 (Centered on period 2)
For even periods (k=4): Use 2×4 MA to center
Best for:
- Decomposition
- Identifying trend
- Historical analysis
Not for forecasting! (Need future values)
Forecasting with SMA
Simple forecast: Next period = current MA
Example: 3-month MA = 110 Forecast for next month = 110
Limitations:
- Only predicts flat line
- Can't capture trends
- Can't capture seasonality
- Lags behind changes
Works best when:
- Data is relatively flat
- No strong trend
- Short-term forecast only
Weighted Moving Average (WMA)
Idea: Give more weight to recent observations
Formula: WMA_t = w_1×Y_t + w_2×Y_{t-1} + w_3×Y_{t-2}
Where: w_1 + w_2 + w_3 = 1
Example (3-period): Weights: 0.5, 0.3, 0.2 Data: 100, 110, 105
WMA = 0.5(105) + 0.3(110) + 0.2(100) = 52.5 + 33 + 20 = 105.5
Advantage: More responsive than SMA
Choosing weights:
- More recent = higher weight
- Weights sum to 1
- Often linear (0.5, 0.3, 0.2) or exponential
Exponential Smoothing
Most popular method!
Formula: F_{t+1} = α×Y_t + (1-α)×F_t
Where:
- F_t = forecast for period t
- Y_t = actual value in period t
- α = smoothing constant (0 < α < 1)
Interpretation: New forecast = α(latest value) + (1-α)(previous forecast)
Example: α = 0.3 Previous forecast = 100 Actual = 110
New forecast = 0.3(110) + 0.7(100) = 33 + 70 = 103
Choosing Alpha (α)
α close to 0 (e.g., 0.1):
- Heavy smoothing
- Slow to respond
- Stable forecasts
- Good for noisy data
α close to 1 (e.g., 0.9):
- Light smoothing
- Quick to respond
- Volatile forecasts
- Good for rapidly changing data
Typical values: 0.1 to 0.3 for most business data
Optimization: Choose α that minimizes forecast error (MSE)
Exponential Smoothing Example
Data: 100, 105, 110, 115 α = 0.3 Initial forecast = 100
Period 1: F_1 = 100 (initial) Actual = 100 Error = 0
Period 2: F_2 = 0.3(100) + 0.7(100) = 100 Actual = 105 Error = 5
Period 3: F_3 = 0.3(105) + 0.7(100) = 101.5 Actual = 110 Error = 8.5
Period 4: F_4 = 0.3(110) + 0.7(101.5) = 104.05 Actual = 115 Error = 10.95
Period 5 forecast: F_5 = 0.3(115) + 0.7(104.05) = 107.34
Why "Exponential"?
Weights decay exponentially:
Most recent: α One back: α(1-α) Two back: α(1-α)² Three back: α(1-α)³
Example (α=0.3):
- Recent: 0.30
- One back: 0.21
- Two back: 0.15
- Three back: 0.10
All observations have some weight! (Unlike SMA which drops old data)
Double Exponential Smoothing
For data with trend:
Two equations:
Level: L_t = α×Y_t + (1-α)×(L_{t-1} + T_{t-1})
Trend: T_t = β×(L_t - L_{t-1}) + (1-β)×T_{t-1}
Forecast: F_{t+h} = L_t + h×T_t
Parameters:
- α: level smoothing
- β: trend smoothing
Use when: Data has linear trend
Triple Exponential Smoothing (Holt-Winters)
For trend AND seasonality:
Three components:
- Level
- Trend
- Seasonal
Most complete exponential smoothing!
Use when:
- Data has trend
- Data has seasonality
- Need automatic forecasting
Popular in: Business forecasting packages
Comparing Methods
SMA: ✓ Simple ✓ Easy to understand ✗ Equal weights ✗ Lags behind
WMA: ✓ More responsive ✓ Custom weights ✗ Manual weight selection ✗ Still drops old data
Exponential Smoothing: ✓ Most responsive ✓ All data used ✓ Self-correcting ✓ One parameter ✗ Slightly harder to explain
Winner for most cases: Exponential smoothing!
Excel Implementation
SMA: =AVERAGE(B2:B4) for 3-period MA
Moving average tool: Data → Data Analysis → Moving Average
Exponential Smoothing: Data → Data Analysis → Exponential Smoothing Enter α (damping factor = 1-α)
Manual exponential: =alpha*Actual + (1-alpha)*PreviousForecast
Python Implementation
import pandas as pd
import numpy as np
# Data
data = [100, 105, 110, 115, 120, 125]
df = pd.DataFrame({'sales': data})
# Simple Moving Average
df['SMA_3'] = df['sales'].rolling(window=3).mean()
# Weighted Moving Average
weights = np.array([0.5, 0.3, 0.2])
df['WMA_3'] = df['sales'].rolling(window=3).apply(
lambda x: np.sum(weights * x), raw=False
)
# Exponential Smoothing
alpha = 0.3
df['EXP'] = df['sales'].ewm(alpha=alpha, adjust=False).mean()
# Using statsmodels for forecasting
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
model = SimpleExpSmoothing(df['sales'])
fit = model.fit(smoothing_level=0.3)
forecast = fit.forecast(steps=3)
print(f"Next 3 periods: {forecast}")
Forecast Accuracy
Measuring error:
MAE (Mean Absolute Error): Average of |Actual - Forecast|
MSE (Mean Squared Error): Average of (Actual - Forecast)²
MAPE (Mean Absolute Percentage Error): Average of |Actual - Forecast| / Actual × 100%
Use:
- Compare different α values
- Choose best forecasting method
- Optimize parameters
When to Use Each Method
Simple Moving Average:
- Quick smoothing
- No forecasting needed
- Educational purposes
Weighted Moving Average:
- Recent data more important
- Known weight structure
Exponential Smoothing:
- Most common choice!
- Automatic forecasting
- Adapts to data
- Works for various patterns
Double/Triple Exponential:
- Trend present
- Seasonality present
- Automated forecasting system
Limitations
All moving averages:
- Only use past data
- Can't predict turning points
- Assume patterns continue
- No confidence intervals
Not good for:
- Long-term forecasts
- Structural changes
- External factors
- Causal modeling
Practical Example
Monthly sales forecasting:
Data (6 months): Jan: 100, Feb: 110, Mar: 105, Apr: 115, May: 120, Jun: 125
Method 1: 3-month SMA Forecast for Jul = (115+120+125)/3 = 120
Method 2: Exponential (α=0.3) Build up from Jan: ... Forecast for Jul = 118
Method 3: Naive (last value) Forecast for Jul = 125
Compare accuracy: Calculate MAE for past months Choose method with lowest error
Practice Exercise
Sales data: Week 1: 50 Week 2: 55 Week 3: 60 Week 4: 58 Week 5: 62
Tasks:
- Calculate 3-week SMA for Week 5
- Forecast Week 6 using SMA
- Calculate exponential smoothing forecast for Week 6 (α=0.2, initial=50)
- Which gives higher forecast?
Answers:
- SMA_5 = (60+58+62)/3 = 60
- Week 6 forecast = 60
- Build up: 50, 51, 52.8, 54.64, 56.11, 57.29 Week 6 forecast = 57.29
- SMA gives higher forecast (60 vs 57.29)
Next Steps
Learn about Naive Methods (forecasting)!
Tip: Start simple with SMA, then try exponential smoothing for better results!