Khansa, home
Work with me

Jump to

Pages

Projects

Links

Lesson 5 of 8, about 18 minutes

ARIMA and SARIMAX explained simply, with Python code

ARIMA sounds scary, but the idea is small. It predicts the next value from recent values (AR), recent mistakes (MA), after differencing the series to make it stationary (I).

By the end: Fit a seasonal ARIMA model and read its forecast range.

The three numbers p, d, q

p is how many past values the model looks at. d is how many times you difference the series. q is how many past mistakes it looks at. A model written ARIMA(1, 1, 1) uses one of each.

Seasonal ARIMA adds a second set of numbers (P, D, Q) and the period m. For our weekly cafe, m is 7.

Pick the numbers by looking at plots

ACF shows how strongly today relates to each earlier day. PACF shows the same, with the in-between days removed. Plot both on the differenced series.

A rough guide: if PACF stops after p lags and ACF fades slowly, try AR(p). If ACF stops after q lags and PACF fades, try MA(q). Spikes at lag 7 and 14 point to weekly seasonal terms.

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")

from statsmodels.graphics.tsaplots import plot_acf, plot_pacf

d = s.diff().dropna()
plot_acf(d, lags=28)
plot_pacf(d, lags=28)

Fit a seasonal model

Start small. Do not try to be perfect. A simple model such as (1, 1, 1) with seasonal (1, 0, 1, 7) is a strong first guess for this data.

Python
from statsmodels.tsa.statespace.sarimax import SARIMAX

train, test = s[:-28], s[-28:]
model = SARIMAX(train, order=(1, 1, 1),
                seasonal_order=(1, 0, 1, 7)).fit(disp=False)

fc = model.get_forecast(28).summary_frame(alpha=0.05)
fc[["mean", "mean_ci_lower", "mean_ci_upper"]].head()

Always check the leftover mistakes

If the model learned the pattern, what is left (the residuals) should look like random noise. The diagnostics plot shows this. If you still see a pattern, add a term or a feature.

Python
model.plot_diagnostics(figsize=(9, 6))

from statsmodels.stats.diagnostic import acorr_ljungbox
acorr_ljungbox(model.resid[8:], lags=[10])   # p-value above 0.05 is good

Where you will use this

  • Demand forecasting for one product with a clear weekly pattern
  • Monthly sales when you also know about promotions (use the exog option)
  • Forecasts with a 95% range, so a manager can plan for the best and worst case

Common mistake: Do not search for the "perfect" (p, d, q) for hours. Compare a few simple models on a backtest (lesson 6) and keep the best.

Try it yourself

Plot the forecast mean and the two range lines together with the test data. How often does the real value stay inside the range?

Check yourself

What does the d in ARIMA stand for?

How many times you difference the series to make it stationary.

You see ACF spikes at lag 7 and 14. What does it suggest?

A weekly seasonal pattern, so add seasonal terms with m = 7.