SyntaxPilot
Courses/Machine Learning/Lesson 51
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

Machine Learning · Lesson 51 of 90

Time series 2: classical forecasting

forecastingnaive and seasonal naive baselinesmoving averagesimple exponential smoothingHolt-Wintersautoregressive (AR) modelmoving-average (MA) modelARIMASARIMAAugmented Dickey-Fuller (ADF) testforecast horizon

Machine Learning Full Course · Episode 51. Every code sample below was run with Python 3.13, NumPy 2.5.3, pandas 3.0.5, statsmodels 0.15.0 and scikit-learn 1.9.1; the output shown is the real output. Ramesh's chai stall is the same made-up series as in Episode 50: a slow growth, busier weekends (× 1.3), busier winters (× 1.25 in mid-January, × 0.75 in mid-July), and luck where each day keeps 60% of yesterday's.

The big idea: Ramesh must sign a milk contract for all of 2025, so he needs a forecast with a horizon of 12 months. We learn from 2023-24, hide 2025, and score each method by its MAE (average miss, in cups a day). The obvious attempts ignore the seasons: the average of history misses by 61.2, a straight line by 50.7. Baselines set the bar: naive 60.9, moving average 51.6, seasonal naive 33.0. Simple exponential smoothing picks alpha = 1.0 and becomes naive (60.9). Holt-Winters smooths level, trend and season together: 10.6. The Box-Jenkins family needs a stationary series: the ADF test says raw cups are not (p = 0.945) but log + diff(7) is (p = 0.0000). An AR(1) model finds a coefficient of 0.619 (we built 0.6) and beats MA(1) and AR(2) on AIC. ARIMA without seasons misses by 92.7; SARIMA with a seasonal difference at 12 months: 8.6. Uncertainty grows with the horizon: SARIMA's 95% range is 87 cups wide 1 month ahead and 246 at 12 months.

Sources: C. C. Holt's 1957 report ONR 52 to the Office of Naval Research on exponentially weighted moving averages (never published in a journal; introduced by K. Ord in the International Journal of Forecasting 20(1):1-3, 2004; iaorifors.com); P. R. Winters, "Forecasting Sales by Exponentially Weighted Moving Averages", Management Science 6(3):324-342, 1960 (ideas.repec.org); G. E. P. Box and G. M. Jenkins, Time Series Analysis: Forecasting and Control, Holden-Day, San Francisco, 1970 (library record); D. A. Dickey and W. A. Fuller, "Distribution of the Estimators for Autoregressive Time Series with a Unit Root", Journal of the American Statistical Association, 1979, pp. 427-431 (reference).

Words you need first

  • Time series, trend, seasonality, stationarity, differencing, lag, ACF, PACF (Episode 50): ordered measurements; long-term direction; a pattern with a fixed period; constant mean and spread over time; value minus an earlier value; steps back; total link to a lag; direct link to a lag.
  • Mean absolute error, MAE (Episode 19): the average size of the misses. Example: misses of +10, −20, +30 → MAE = (10 + 20 + 30) / 3 = 20.
  • Train/test split (Episode 15): learn on some rows, judge on rows the model never saw. For time series the test must come after the training.
  • Linear regression (Episode 18): fits a straight line, value = a + b × input.
  • Hypothesis test, null hypothesis, p-value (Episode 11): the null hypothesis is the "nothing special" claim; a small p-value (below 0.05) means the data would be unlikely if it were true, so we reject it.
  • AIC (Episode 45): a score that rewards fitting well and penalises extra parameters; lower is better.
  • Logarithm and np.exp: np.log turns growing swings into equal ones; np.exp undoes it.
  • Confidence interval (Episode 11): a range that should contain the true value with a stated probability (95%).

The real-life hook: a milk contract

At the end of 2024 the dairy offers Ramesh a cheaper price if he signs for all of 2025, with a fixed amount of milk for each month. Too little milk: on busy winter days the chai runs out and customers leave. Too much: in quiet summer months it spoils. He needs a good guess for each of the next twelve months.

These lines create the diary (as in Episode 50; we call them HDR):

import numpy as np, pandas as pd
r = np.random.default_rng(50)                                    # Ramesh's chai stall, made-up numbers
days = pd.date_range("2023-01-01", "2025-12-31", freq="D"); t = np.arange(len(days))   # 3 years of days
level = 200 + 0.1 * t                                            # slow growth: +0.1 cup a day
week = np.where(days.dayofweek >= 5, 1.3, 1.0)                   # weekends 30% busier
year = 1 + 0.25 * np.cos(2 * np.pi * (days.dayofyear - 15) / 365.25)   # winter busier, summer quieter
mood = np.zeros(len(t)); shock = r.normal(0, 0.05, len(t))       # random luck that carries over:
for i in range(1, len(t)): mood[i] = 0.6 * mood[i - 1] + shock[i]   # 60% of yesterday's luck stays
cups = (level * week * year * np.exp(mood)).round().astype(int)
diary = pd.DataFrame({"date": days.strftime("%d-%m-%Y"), "cups": cups}).sample(frac=1, random_state=1)

The monthly programs add these three lines (MONTHLY):

diary["date"] = pd.to_datetime(diary["date"], format="%d-%m-%Y")
m = diary.set_index("date").sort_index()["cups"].asfreq("D").resample("MS").mean()   # 36 monthly means
train, test = m[:-12], m[-12:]; mae = lambda f: np.abs(np.asarray(f) - test.values).mean().round(1)   # learn 2023-24
  • m holds the average cups a day for each month (monthly means, not totals, so February's 28 days don't look like a dip).
  • m[:-12] is everything except the last 12 months (24 months: 2023-24); m[-12:] is 2025.
  • mae(f) is the average miss of a forecast f against 2025, in cups a day.

The daily programs add these two lines (DAILY):

diary["date"] = pd.to_datetime(diary["date"], format="%d-%m-%Y")
ts = diary.set_index("date").sort_index()["cups"].asfreq("D")   # the daily series from episode 50

Forecasting: the words

  • A forecast is a prediction of the future values of a time series.
  • The forecast horizon is how far ahead: 12 months here. (1 step ahead is much easier than 12.)
  • Testing fairly: pretend it is the end of 2024. Models learn only from 2023-24; compare their guesses with the real 2025. Never shuffle: the test must come after the training in time, or the future leaks into the model.
  • Score: MAE in cups a day.

The obvious attempts and the baselines

Method Forecast for every future month Idea
Mean of history The average of all 24 months "Things are usually about this much"
Straight line Linear regression on the month number "It keeps growing"
Naive The last value seen (December 2024) "Tomorrow = today"
Moving average (3) The mean of the last 3 months Smooths noise
Seasonal naive The same month last year "Next March = last March"

These simple methods are baselines: forecasts so cheap that any real model must beat them.

# HDR + MONTHLY (above), then:
from sklearn.linear_model import LinearRegression; flat = lambda v: np.full(12, v)   # one guess for all 12 months
tt = np.arange(36).reshape(-1, 1); line = LinearRegression().fit(tt[:24], train.values)
for name, f in [("mean of history", flat(train.mean())), ("straight line", line.predict(tt[24:])),
                ("naive (last month)", flat(train.iloc[-1])), ("moving average (3)", flat(train.iloc[-3:].mean())),
                ("seasonal naive", train.iloc[-12:].values)]:          # seasonal naive: same month, last year
    print(f"{name + ':':19}", mae(f), "cups a day")

Output:

mean of history:    61.2 cups a day
straight line:      50.7 cups a day
naive (last month): 60.9 cups a day
moving average (3): 51.6 cups a day
seasonal naive:     33.0 cups a day
  • np.arange(36).reshape(-1, 1) numbers the months 0..35 as one column (scikit-learn wants a 2-D input). The line learns on months 0-23 and predicts 24-35.
  • flat(v) = np.full(12, v) repeats one number 12 times: a flat forecast. The loop scores each forecast with mae; f"{name + ':':19}" pads the name to 19 characters so the numbers line up.
  • Everything without seasons misses by 50-61 cups a day. Seasonal naive: 33.0, our bar. It knows about winter but forgets the growth, so it guesses too low every month.

Exponential smoothing

A better average: recent months count more and older months fade.

Simple exponential smoothing (SES) keeps one number, the level:

level(new) = alpha × newest value + (1 − alpha) × level(old)

  • alpha (between 0 and 1) is the smoothing parameter. Near 1: trust the newest value. Near 0: long memory, very smooth.
  • Unrolled, the value k steps back gets weight alpha × (1 − alpha)^k. With alpha = 0.6: 0.6, 0.24, 0.096, ... ; with alpha = 0.2: 0.2, 0.16, 0.128, ... This shrinking by a constant factor is exponential decay, hence the name. Charles Holt described these weighted averages in a 1957 report.
  • The forecast is a flat line at the last level.

Holt-Winters

One level cannot climb or swing. Holt-Winters (triple exponential smoothing) smooths three pieces:

Piece Meaning Smoothing parameter
Level Where we are now alpha
Trend How fast we're climbing per month beta
Season A factor for each month (× 1.23 for January) gamma

Forecast h months ahead (additive trend, multiplicative season): (level + h × trend) × season of that month. Holt added the trend; Peter Winters added the season (Management Science, 1960).

# HDR + MONTHLY (above), then:
from statsmodels.tsa.holtwinters import SimpleExpSmoothing, ExponentialSmoothing
ses = SimpleExpSmoothing(train, initialization_method="estimated").fit()
print("simple exponential smoothing: alpha =", round(ses.params["smoothing_level"], 2), "  MAE", mae(ses.forecast(12)))
hw = ExponentialSmoothing(train, trend="add", seasonal="mul", seasonal_periods=12,
                          initialization_method="estimated").fit()   # level + trend + season
f = hw.forecast(12); print("Holt-Winters MAE:", mae(f))
print("Dec 2025: forecast", f.iloc[-1].round(1), "  actual", test.iloc[-1].round(1))

Output:

simple exponential smoothing: alpha = 1.0   MAE 60.9
Holt-Winters MAE: 10.6
Dec 2025: forecast 413.4   actual 410.7
  • initialization_method="estimated" lets statsmodels estimate the starting level (and trend and seasons) too. In statsmodels 0.15 you must pass it: leaving it out raised TypeError: initialization_method must be a string when we tried.
  • SES chose alpha = 1.0: it trusts only the newest month, so it is the naive forecast (60.9). Without a season it can do no better.
  • Holt-Winters: 10.6. For December 2025 it guessed 413.4; the truth was 410.7.

The ADF test

The second family (Box and Jenkins, 1970) models the series' memory directly, but it needs a stationary series. The augmented Dickey-Fuller (ADF) test (after Dickey and Fuller, 1979) is a hypothesis test:

  • Null hypothesis: the series is not stationary: it wanders like a ball that never comes back to a fixed average (technically, it has a "unit root").
  • p < 0.05: reject the null and treat the series as stationary. A large p-value: we can't rule out wandering.
  • The ADF statistic is the test's number; the more negative, the stronger the evidence for stationarity.
# HDR + DAILY (above), then:
from statsmodels.tsa.stattools import adfuller
for name, x in [("raw cups", ts), ("log, diff(7)", np.log(ts).diff(7).dropna())]:
    stat, p = adfuller(x, result_object=False)[:2]               # null hypothesis: NOT stationary
    print(f"{name:12} ADF stat {stat:6.2f}   p {p:.4f} ->", "stationary" if p < 0.05 else "not stationary")

Output:

raw cups     ADF stat  -0.14   p 0.9450 -> not stationary
log, diff(7) ADF stat  -6.50   p 0.0000 -> stationary
  • adfuller returns a tuple; [:2] takes the statistic and the p-value. result_object=False keeps the tuple and silences a FutureWarning about a coming change.
  • Raw cups: p = 0.945, not stationary. Log + diff(7): p rounds to 0, stationary: the eyeball check from Episode 50 was right.

AR and MA models

Model Memory of Formula (on the stationary series x) Chai-stall picture
AR(p), autoregressive Past values xₜ = c + φ₁ xₜ₋₁ + ... + φₚ xₜ₋ₚ + εₜ A good day brings regulars who bring friends the next day
MA(q), moving average Past surprises xₜ = c + εₜ + θ₁ εₜ₋₁ + ... + θ_q εₜ₋_q A cricket-match crowd: half of the surprise echoes back the next day, then it's gone
  • εₜ (epsilon) is the surprise (shock, error) at time t: the part nobody could predict.
  • "Auto"-regressive: the series is regressed on itself.
  • This MA is not the rolling mean: it is a weighted sum of past surprises.
  • Reading the charts from Episode 50: a PACF that cuts off after lag p and an ACF that fades suggest AR(p); an ACF that cuts off after lag q suggests MA(q).
# HDR + DAILY (above), then:
from statsmodels.tsa.arima.model import ARIMA
x = np.log(ts).diff(7).dropna()                                  # the stationary series
for order in [(1, 0, 0), (0, 0, 1), (2, 0, 0)]:                  # AR(1), MA(1), AR(2)
    fit = ARIMA(x, order=order).fit()
    coefs = {k: round(v, 3) for k, v in fit.params.items() if k[:2] in ("ar", "ma")}
    print(f"ARIMA{order}: {coefs}   AIC {fit.aic:.1f}")

Output:

ARIMA(1, 0, 0): {'ar.L1': 0.619}   AIC -2679.3
ARIMA(0, 0, 1): {'ma.L1': 0.475}   AIC -2523.3
ARIMA(2, 0, 0): {'ar.L1': 0.638, 'ar.L2': -0.03}   AIC -2678.3
  • order=(p, d, q); (1, 0, 0) is AR(1). fit.params also holds a constant and the noise variance (sigma2); we keep only the ar/ma coefficients.
  • AR(1) finds 0.619: we built 60% of yesterday's luck carrying over. The model found it from the numbers alone.
  • MA(1) has a clearly worse (higher) AIC. AR(2)'s second coefficient is −0.03 (≈ 0) and its AIC is no better: one direct step of memory, as the PACF said.

ARIMA and SARIMA

ARIMA(p, d, q):

  • p: number of AR terms (past values).
  • d: how many times to difference the series first. The I is for integrated: after forecasting the differences, the model adds them back up.
  • q: number of MA terms (past surprises).

SARIMA(p, d, q) × (P, D, Q, s) adds a seasonal copy working at the season's lag s: for monthly data s = 12, and D = 1 means "this March minus last March".

We fit on np.log(train) (so growing swings become equal) and turn forecasts back with np.exp.

# HDR + MONTHLY (above), then:
from statsmodels.tsa.arima.model import ARIMA
for o, so in [((1, 1, 1), (0, 0, 0, 0)), ((1, 0, 0), (0, 1, 0, 12))]:   # ARIMA, then SARIMA
    fit = ARIMA(np.log(train), order=o, seasonal_order=so).fit()
    print(f"order {o}, seasonal {so}: MAE", mae(np.exp(fit.forecast(12))))
ci = np.exp(fit.get_forecast(12).conf_int()); w = (ci.iloc[:, 1] - ci.iloc[:, 0]).round(1)   # 95% range per month
print("width of the 95% range: 1 month ahead", w.iloc[0], "  6 months", w.iloc[5], "  12 months", w.iloc[11])

Output:

order (1, 1, 1), seasonal (0, 0, 0, 0): MAE 92.7
order (1, 0, 0), seasonal (0, 1, 0, 12): MAE 8.6
width of the 95% range: 1 month ahead 87.2   6 months 131.2   12 months 245.9
  • Plain ARIMA(1, 1, 1) knows trend and memory but nothing about winter: 92.7, worse than the average of history.
  • SARIMA (1, 0, 0) × (0, 1, 0, 12): one AR term on top of "this month minus the same month last year": 8.6, the best.
  • get_forecast(12).conf_int() gives the lower and upper ends of the 95% range on the log scale; np.exp brings them back to cups. The range widens with the horizon: 87 → 131 → 246 cups. The further ahead, the less sure any model can be.

The scoreboard

Method MAE (cups a day, 2025)
ARIMA(1, 1, 1), no season 92.7
Mean of history 61.2
Naive · simple exponential smoothing 60.9 · 60.9
Moving average (3) 51.6
Straight line 50.7
Seasonal naive (the bar) 33.0
Holt-Winters 10.6
SARIMA (1,0,0) × (0,1,0,12) 8.6

On the chart, the mean is a flat useless line, seasonal naive has the right shape but sits too low, and Holt-Winters and SARIMA hug the real 2025 values, with SARIMA's 95% band widening towards December. On one test year, 10.6 vs 8.6 is close (Practice 3 shows the order flips at a 6-month horizon); the next episode tests over many periods.

Common mistakes

Mistake Exact message / symptom Why Fix
No baseline a model scores 50 and you can't tell if that's good No bar to beat naive and seasonal naive first
Random split or shuffling future rows leak into training Time order matters Train on the past, test on the later period
SES on seasonal data alpha = 1.0, MAE 60.9 No season in the model Holt-Winters
Leaving out initialization_method TypeError: initialization_method must be a string statsmodels 0.15 needs it initialization_method="estimated"
ARIMA without seasons on seasonal data MAE 92.7 Winter ignored SARIMA with s = 12
Reading the ADF p-value backwards p = 0.945 read as "stationary" The null is NOT stationary p < 0.05 → stationary
Forgetting to undo the log forecasts around 5.8 instead of 340 The model works on log values np.exp(fit.forecast(h))
One number with no range range 87 → 246 cups wide Uncertainty grows with the horizon Report the 95% range

Mini project: Ramesh's 2026 milk plan

Refit the winning model on all 36 months, forecast 2026, and convert cups a day into litres a month (100 ml a cup), with a "safe" amount from the top of the 95% range.

# HDR + MONTHLY (above), then:
from statsmodels.tsa.arima.model import ARIMA
fit = ARIMA(np.log(m), order=(1, 0, 0), seasonal_order=(0, 1, 0, 12)).fit()   # now learn from all 36 months
g = fit.get_forecast(12); mid = np.exp(g.predicted_mean); hi = np.exp(g.conf_int().iloc[:, 1])
days_in = mid.index.days_in_month                                # cups a day x days = cups a month
plan = pd.DataFrame({"cups/day": mid.round(0), "litres": (mid * days_in * 0.1).round(0),
                     "litres, safe (95% top)": (hi * days_in * 0.1).round(0)}, index=mid.index.strftime("%b %Y"))
print(plan.iloc[[0, 1, 5, 6, 10, 11]].to_string())
print("2026 total litres:", int(plan["litres"].sum()))

Output:

          cups/day  litres  litres, safe (95% top)
Jan 2026     405.0  1256.0                  1393.0
Feb 2026     417.0  1167.0                  1346.0
Jun 2026     274.0   823.0                  1029.0
Jul 2026     254.0   788.0                   997.0
Nov 2026     397.0  1192.0                  1559.0
Dec 2026     438.0  1357.0                  1785.0
2026 total litres: 12460
  • Once a method has been chosen on the test year, refit it on all the data before forecasting the real future.
  • days_in_month turns cups a day into cups a month (February has 28 days, so fewer litres despite more cups a day).
  • The "safe" column grows faster than the middle one towards December: the 95% range widens with the horizon. A practical contract might take the middle for the first months and add slack later, or renegotiate mid-year.

Practice

1. Holt-Winters with an additive season instead of multiplicative: better or worse?

Solution
# HDR + MONTHLY (above), then:
from statsmodels.tsa.holtwinters import ExponentialSmoothing
for seas in ("mul", "add"):
    hw = ExponentialSmoothing(train, trend="add", seasonal=seas, seasonal_periods=12,
                              initialization_method="estimated").fit()
    print(f"Holt-Winters, seasonal={seas}: MAE", mae(hw.forecast(12)))

Output:

Holt-Winters, seasonal=mul: MAE 10.6
Holt-Winters, seasonal=add: MAE 14.5

Worse (14.5): the seasonal swings grow with the level, so the multiplicative season fits (Episode 50).

2. What is SARIMA (0,0,0) × (0,1,0,12) on the log?

Solution
# HDR + MONTHLY (above), then:
from statsmodels.tsa.arima.model import ARIMA
fit = ARIMA(np.log(train), order=(0, 0, 0), seasonal_order=(0, 1, 0, 12)).fit()
print("SARIMA (0,0,0)x(0,1,0,12): MAE", mae(np.exp(fit.forecast(12))))
print("seasonal naive:            MAE", mae(train.iloc[-12:].values))
print("same numbers?", np.allclose(np.exp(fit.forecast(12)).values, train.iloc[-12:].values))

Output:

SARIMA (0,0,0)x(0,1,0,12): MAE 33.0
seasonal naive:            MAE 33.0
same numbers? True

It is the seasonal naive forecast: "this month minus the same month last year is zero" means "this month = last year's". Our winning SARIMA is seasonal naive plus one AR term on the year-over-year change.

3. Forecast 6 months instead of 12. Which model wins?

Solution
# HDR + MONTHLY (above), then:
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from statsmodels.tsa.arima.model import ARIMA
hw = ExponentialSmoothing(train, trend="add", seasonal="mul", seasonal_periods=12, initialization_method="estimated").fit()
sar = ARIMA(np.log(train), order=(1, 0, 0), seasonal_order=(0, 1, 0, 12)).fit()
for h in (6, 12):
    e_hw = np.abs(hw.forecast(h).values - test.values[:h]).mean()
    e_sar = np.abs(np.exp(sar.forecast(h)).values - test.values[:h]).mean()
    print(f"horizon {h:2} months: Holt-Winters MAE {e_hw:5.1f}   SARIMA MAE {e_sar:5.1f}")

Output:

horizon  6 months: Holt-Winters MAE  10.2   SARIMA MAE  11.0
horizon 12 months: Holt-Winters MAE  10.6   SARIMA MAE   8.6

The winner depends on the horizon: Holt-Winters for 6 months, SARIMA for 12. One test period is not enough to crown a model; that's why the next episode uses walk-forward validation over many periods.

4. Run the ADF test on diff(1) of the daily cups. Is it "stationary"? Does that mean the weekly season is gone?

Solution
# HDR + DAILY (above), then:
from statsmodels.tsa.stattools import adfuller
for name, x in [("raw cups", ts), ("diff(1)", ts.diff().dropna()), ("log, diff(1)", np.log(ts).diff().dropna())]:
    stat, p = adfuller(x, result_object=False)[:2]
    print(f"{name:13} ADF statistic {stat:7.2f}   p-value {p:.4f}")

Output:

raw cups      ADF statistic   -0.14   p-value 0.9450
diff(1)       ADF statistic   -7.85   p-value 0.0000
log, diff(1)  ADF statistic   -8.18   p-value 0.0000

ADF says yes (p ≈ 0), but Episode 50's Practice 4 showed diff(1) still has ACF 0.86 at lag 7. The ADF test checks for wandering (a unit root), not for seasonality; always look at the ACF too.

Quick quiz

1. January 2024 = 300, February 2024 = 280, January 2025 = 330. Seasonal naive forecast for February 2025? Options: 330, 280, 290, 260.

Answer

280: the same month last year. Naive would say 330.

2. SES fits alpha = 0.95 on a series. Does it have a long or a short memory?

Answer

Short: 95% of the weight is on the newest value.

3. The ADF test gives p = 0.62. What do you do?

Answer

Treat the series as not stationary: difference it (or log it), then test again.

4. In ARIMA(2, 1, 0), what do 2, 1 and 0 mean?

Answer

2 AR terms (the last two values of the differenced series), 1 difference, 0 MA terms.

5. Why does the 95% range widen with the horizon?

Answer

Each future step adds new unknown surprises, and errors build on each other, so uncertainty grows (87 cups wide at 1 month, 246 at 12).

Interview questions

  1. How do you evaluate a forecasting model? On a later period it never saw (time-ordered split), with an error such as MAE, compared against naive and seasonal-naive baselines; better, over many periods (walk-forward, next episode).
  2. Why are baselines important? They're free and often surprisingly hard to beat; a model that can't beat seasonal naive isn't adding value.
  3. Explain simple exponential smoothing. A weighted average with weights alpha(1 − alpha)^k; one level; flat forecasts. alpha near 1 = naive.
  4. What does Holt-Winters add? A smoothed trend (Holt) and seasonal factors (Winters), additive or multiplicative.
  5. What does the ADF test test? Null: a unit root (non-stationary, wandering); small p → stationary. It does not check for seasonality.
  6. AR vs MA? AR: depends on past values; MA: on past surprises. Identify with the PACF (cuts off for AR) and the ACF (cuts off for MA).
  7. What does each number in ARIMA(p, d, q) mean? AR order, number of differences, MA order.
  8. When do you need SARIMA? When there's seasonality: the seasonal part (P, D, Q, s) models patterns at the seasonal lag.
  9. Why fit on the log? When seasonal swings grow with the level (multiplicative), the log makes them additive and stabilises the variance; transform forecasts back with exp.
  10. How would you choose between Holt-Winters and SARIMA? Compare on several held-out periods and horizons (they swapped places between 6 and 12 months here), check residuals, prefer the simpler model when scores are close, and report prediction intervals.

Glossary

Term Meaning (plain words + example)
Forecasting Predicting future values of a time series: 2025's monthly cups
Forecast horizon How far ahead you predict: 12 months
MAE Mean absolute error: the average miss, in cups a day
Baseline A very simple forecast every model must beat
Naive forecast Every future value = the last value seen
Seasonal naive forecast Every future value = the same season last time: next March = last March
Moving average (forecast) Forecast = the mean of the last k values
Exponential smoothing Weighted average where weights fade by a constant factor going back
Simple exponential smoothing (SES) One smoothed level, flat forecast
alpha (smoothing level) 0..1: how much the newest value counts; 1.0 = naive
Level The smoothed "where we are now"
Holt-Winters Exponential smoothing of level, trend and season (triple exponential smoothing)
beta, gamma Smoothing parameters for the trend and the season
seasonal_periods The period of the season: 12 for monthly data with a yearly pattern
ADF test Augmented Dickey-Fuller test; null hypothesis: not stationary
Unit root The technical name for a series that wanders and never returns to a fixed mean
ADF statistic The test number; more negative = stronger evidence of stationarity
Surprise (shock, error, ε) The unpredictable part of each new value
AR model (autoregressive) Today from past values: AR(1) coefficient 0.619
MA model (moving average) Today from past surprises (not the rolling mean)
ARIMA(p, d, q) AR order p, d differences, MA order q
Integrated (the I) Forecast the differences, then add them back up
SARIMA ARIMA plus a seasonal part (P, D, Q, s) at the seasonal lag s
order, seasonal_order statsmodels arguments: (p, d, q) and (P, D, Q, s)
AIC Fit score with a penalty for extra parameters; lower is better
Prediction interval (95% range) A range that should contain the future value 95% of the time
conf_int() statsmodels method returning the lower and upper ends of that range
np.exp Undoes np.log
Box-Jenkins The ARIMA approach, from Box and Jenkins's 1970 book

Summary

  • A forecast predicts the future; the horizon is how far ahead. Test on a later period, never shuffled; score with MAE.
  • Baselines first: mean 61.2, straight line 50.7, naive 60.9, moving average 51.6, seasonal naive 33.0.
  • Exponential smoothing fades the past; SES (alpha 1.0) became naive. Holt-Winters smooths level, trend and season: 10.6.
  • The ADF test checks stationarity (null: not stationary): raw p = 0.945, log + diff(7) p ≈ 0.
  • AR uses past values, MA past surprises; AR(1) recovered 0.619 (built 0.6) with the best AIC.
  • ARIMA(p, d, q) without seasons: 92.7; SARIMA × (0, 1, 0, 12): 8.6. The 95% range widens with the horizon (87 → 246).

Next episode: Time series 3: ML for forecasting: lag and calendar features, walk-forward validation, backtesting, look-ahead leakage, MAPE and MASE.