Split by time
Keep the last 28 days as a test. Train on everything before. Never shuffle.
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.
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.
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.
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.