Khansa, home
Work with me

Jump to

Pages

Projects

Links

Lesson 4 of 8, about 15 minutes

Baseline forecasts and exponential smoothing (ETS) in Python

The best first model is a very simple one. If a clever model cannot beat a simple baseline, it is not worth using. We build two baselines and one real model, then compare them on the same test window.

By the end: Make three forecasts and compare them fairly.

Split by time

Keep the last 28 days as a test. Train on everything before. Never shuffle.

Python
import numpy as np
import pandas as pd

rng = np.random.default_rng(42)
days = pd.date_range("2024-01-01", periods=365, freq="D")
trend = np.linspace(100, 140, 365)                 # sales slowly grow
week = np.array([-8, -6, -4, 0, 6, 16, 12])[days.dayofweek]  # busy weekends
s = pd.Series(trend + week + rng.normal(0, 4, 365), index=days, name="sales")

train, test = s[:-28], s[-28:]
len(train), len(test)

Two baselines

Naive says: tomorrow equals today. Seasonal naive says: this Saturday equals last Saturday. For data with a weekly pattern, the second one is hard to beat.

Python
naive = pd.Series(train.iloc[-1], index=test.index)
seasonal_naive = pd.Series(np.tile(train.iloc[-7:].values, 4), index=test.index)

Exponential smoothing

Exponential smoothing gives recent days more weight than old days. Holt-Winters adds a trend and a repeating season on top. It is quick, and often very good.

Python
from statsmodels.tsa.holtwinters import ExponentialSmoothing

ets = ExponentialSmoothing(train, trend="add", seasonal="add",
                           seasonal_periods=7).fit()
ets_fc = ets.forecast(28)

Compare them with one number

MAE is the average size of the miss, in the same unit as your data (here, sales). Smaller is better.

Python
def mae(actual, forecast):
    return float(np.mean(np.abs(actual.values - forecast.values)))

print("naive          ", round(mae(test, naive), 2))
print("seasonal naive ", round(mae(test, seasonal_naive), 2))
print("exp. smoothing ", round(mae(test, ets_fc), 2))

You should see

naive           14.94
seasonal naive  5.35
exp. smoothing  3.08

Seasonal naive beats naive by a lot, and exponential smoothing beats both. Your numbers match because the data uses a fixed random seed.

Where you will use this

  • A cafe predicting next week’s sales to plan staff
  • A warehouse estimating weekly stock needs
  • Setting a simple target that fancier models must beat

Common mistake: Do not skip the baseline. Many "great" models turn out to be no better than seasonal naive once you check.

Try it yourself

Change seasonal="add" to seasonal="mul" and compare the MAE. Which one fits this series better?

Check yourself

What does the seasonal naive forecast say for next Saturday?

The same as last Saturday.

A model has a lower MAE than every other. What does that mean?

On this test window its forecasts missed by the smallest amount on average.