Type at least 2 letters. Enter opens the first result, Esc closes.

    Time Series and Forecasting · Lesson 6 · ARIMA and state-space models

    ARIMA and SARIMA: differencing, forecasting and prediction intervals

    Intermediate 90 min 5 interactive figures 4 videos Colab notebook

    Before this lesson: lesson 02 (stationarity, differencing, unit roots), lesson 04 (simple exponential smoothing) and lesson 05 (AR, MA, ARMA, ψ\psi-weights, AIC). Lesson 03 gives the benchmarks and backtests we compare against. Time: about 90 minutes, plus the notebook.


    Why this matters #

    • The standard statistical forecaster. ARIMA, with automatic order selection (auto.arima in R, AutoARIMA in statsforecast, pmdarima), is a benchmark in almost every forecasting paper and in competitions such as M4 and M5. A new ML model that cannot beat it, or a seasonal naive forecast, is not useful.
    • Honest uncertainty. The ARIMA fan shows how uncertainty grows with the horizon, and it is exact under the model's assumptions. It is the reference for the probabilistic forecasts of lesson 10.
    • Links to the rest of the track. Simple exponential smoothing (lesson 04) is an ARIMA(0,1,1); every ARIMA is a state-space model whose likelihood the Kalman filter computes (lesson 07); residuals of a seasonal ARIMA are a common anomaly score (lesson 11).
    • Science. The CO₂ record of Mauna Loa, river flows, electricity load and epidemic counts are all modelled with seasonal ARIMA or regression with ARIMA errors.

    1. Predict first #

    2. The picture #

    Differencing removes a unit root. A random walk yt=yt−1+εty_t = y_{t-1} + \varepsilon_t is not stationary, but its difference ∇yt=yt−yt−1=εt\nabla y_t = y_t - y_{t-1} = \varepsilon_t is white noise (lesson 02). A linear trend disappears after one difference; a stable seasonal pattern with period mm disappears after a seasonal difference ∇myt=yt−yt−m\nabla_m y_t = y_t - y_{t-m}. ARIMA models the differences as ARMA and then "integrates" (sums) them back.

    The forecast fan. Forecasts start at the forecast origin nn (yellow line) and go into the future. Around the point forecast (orange line) we draw the 80 % and 95 % prediction intervals (orange bands). The figure lets you compare model families.

    Interactive figure: loading…

    Pick a model and move the sliders. Blue: the history (simulated from the chosen model, except for the CO₂ case). Orange line: the point forecast from the yellow origin; dark and light orange bands: 80 % and 95 % intervals. Notice: for AR(1) the forecast returns to the mean and the bands stop growing; for the random walk the bands grow like h\sqrt h; drift tilts the whole fan; ARIMA(0,1,1) gives a flat forecast with growing bands. The readout lists ψ0,…,ψ5\psi_0, \dots, \psi_5 and the 95 % half-width at h=1,12,24h = 1, 12, 24.

    Why do the bands grow? Write the future value as a sum of shocks: yn+h=(known part)+ψ0εn+h+ψ1εn+h−1+⋯+ψh−1εn+1y_{n+h} = \text{(known part)} + \psi_0\varepsilon_{n+h} + \psi_1\varepsilon_{n+h-1} + \dots + \psi_{h-1}\varepsilon_{n+1}. The hh future shocks are unknown, so they make up the forecast error. Each one adds ψj2σ2\psi_j^2\sigma^2 to the variance. If the ψj\psi_j shrink quickly (a stationary model), the sum converges and the bands reach a fixed width. If the ψj\psi_j do not shrink (a differenced model, where every shock acts forever), the sum grows without limit.

    Interactive figure: loading…

    Choose a model and move ϕ\phi or θ\theta. Top: the ψ\psi-weights (orange bars), the effect of a shock jj steps later. Bottom: the forecast variance Var⁡(en+h)/σ2=∑j<hψj2\operatorname{Var}(e_{n+h})/\sigma^2 = \sum_{j<h}\psi_j^2 (aqua line). Notice: for AR(1) the bars shrink like ϕj\phi^j and the line flattens at 1/(1−ϕ2)1/(1 - \phi^2); for ARIMA(0,1,1) all bars after the first equal 1+θ1 + \theta and the line keeps rising.

    Quick check

    A random walk yt=yt−1+εty_t = y_{t-1} + \varepsilon_t with σ=1\sigma = 1 is forecast from yn=50y_n = 50. What is the 95 % prediction interval at h=4h = 4?

    3. Definitions and main results #

    ARIMA(p,d,qp, d, q) #

    Let ∇=1−B\nabla = 1 - B be the difference operator, so ∇yt=yt−yt−1\nabla y_t = y_t - y_{t-1} and ∇2yt=(yt−yt−1)−(yt−1−yt−2)\nabla^2 y_t = (y_t - y_{t-1}) - (y_{t-1} - y_{t-2}). An ARIMA(p,d,qp, d, q) model says that the dd-times differenced series wt=∇dytw_t = \nabla^d y_t is a stationary, invertible ARMA(p,qp, q):

    ϕ(B) (1−B)d yt=c+θ(B) εt,\phi(B)\,(1 - B)^d\,y_t = c + \theta(B)\,\varepsilon_t ,

    with ϕ(z)=1−ϕ1z−⋯−ϕpzp\phi(z) = 1 - \phi_1 z - \dots - \phi_p z^p and θ(z)=1+θ1z+⋯+θqzq\theta(z) = 1 + \theta_1 z + \dots + \theta_q z^q (plus-sign convention, as in lesson 05). The AR part of the whole model, ϕ(z)(1−z)d\phi(z)(1 - z)^d, has dd roots exactly on the unit circle at z=1z = 1: the unit roots. In practice d∈{0,1,2}d \in \{0, 1, 2\}.

    Special cases.

    Model Equation Name
    ARIMA(0,0,0) yt=c+εty_t = c + \varepsilon_t white noise with mean cc
    ARIMA(0,1,0) yt=yt−1+εty_t = y_{t-1} + \varepsilon_t random walk (the naive forecast of lesson 03)
    ARIMA(0,1,0) with cc yt=c+yt−1+εty_t = c + y_{t-1} + \varepsilon_t random walk with drift (the drift method)
    ARIMA(pp,0,0) ϕ(B)yt=c+εt\phi(B)y_t = c + \varepsilon_t AR(pp)
    ARIMA(0,1,1) yt=yt−1+εt+θεt−1y_t = y_{t-1} + \varepsilon_t + \theta\varepsilon_{t-1} simple exponential smoothing

    Theorem (SES is ARIMA(0,1,1)). Simple exponential smoothing with parameter α\alpha gives the same one-step forecasts as ARIMA(0,1,1) with θ=α−1\theta = \alpha - 1, when both start from the same first forecast.

    Proof.

    1. SES in error-correction form (lesson 04): y^t+1∣t=ℓt=ℓt−1+αet\hat y_{t+1\mid t} = \ell_t = \ell_{t-1} + \alpha e_t, where et=yt−y^t∣t−1e_t = y_t - \hat y_{t\mid t-1} and y^t∣t−1=ℓt−1\hat y_{t\mid t-1} = \ell_{t-1}.
    2. Replace ℓt−1\ell_{t-1} by yt−ety_t - e_t: y^t+1∣t=yt−et+αet=yt+(α−1)et\hat y_{t+1\mid t} = y_t - e_t + \alpha e_t = y_t + (\alpha - 1)e_t.
    3. For ARIMA(0,1,1), write the model at time t+1t + 1: yt+1=yt+εt+1+θεty_{t+1} = y_t + \varepsilon_{t+1} + \theta\varepsilon_t. The future shock εt+1\varepsilon_{t+1} is replaced by 0 and the past shock εt\varepsilon_t by the residual ete_t: y^t+1∣t=yt+θet\hat y_{t+1\mid t} = y_t + \theta e_t.
    4. Steps 2 and 3 are the same recursion when θ=α−1\theta = \alpha - 1. With the same start, all forecasts and residuals agree. ■\blacksquare

    So α∈(0,1)\alpha \in (0, 1) corresponds to θ∈(−1,0)\theta \in (-1, 0), an invertible MA part. SES assumes that the data are a random walk plus noise; this explains its flat forecast with growing intervals.

    Quick check

    A fitted ARIMA(0,1,1) has θ=−0.7\theta = -0.7 (plus-sign convention). Which simple exponential smoothing gives the same one-step forecasts?

    Forecasting step by step #

    Let y^n+h∣n\hat y_{n+h\mid n} be the forecast made at origin nn for horizon hh. The rules (FPP3 §9.8):

    1. Write the model with yty_t alone on the left, everything else on the right.
    2. Replace tt by n+hn + h.
    3. On the right, replace future observations (yn+jy_{n+j} with j≥1j \ge 1) by their forecasts, future shocks (εn+j\varepsilon_{n+j}, j≥1j \ge 1) by 0, and past shocks (εt\varepsilon_t, t≤nt \le n) by the residuals ete_t.

    Why 0 for future shocks? The best forecast in mean squared error is the conditional mean E[yn+h∣y1,…,yn]\mathrm E[y_{n+h} \mid y_1, \dots, y_n], and a future shock has mean 0 and is independent of the data. Past shocks are not random any more once we have seen the data: for an invertible model they are functions of the observations, and the residuals estimate them (lesson 05).

    Example: AR(1) with mean μ\mu. yt−μ=ϕ(yt−1−μ)+εty_t - \mu = \phi(y_{t-1} - \mu) + \varepsilon_t. Then y^n+1∣n−μ=ϕ(yn−μ)\hat y_{n+1\mid n} - \mu = \phi(y_n - \mu), and each further step multiplies by ϕ\phi again:

    y^n+h∣n=μ+ϕh (yn−μ).\hat y_{n+h\mid n} = \mu + \phi^h\,(y_n - \mu) .

    The forecast decays geometrically from the last value to the mean. For any stationary ARMA the forecast tends to the mean μ\mu as h→∞h \to \infty.

    Interactive figure: loading…

    Press Step to compute the next forecast of the ARMA(1,1) yt=0.6yt−1+εt+0.3εt−1y_t = 0.6y_{t-1} + \varepsilon_t + 0.3\varepsilon_{t-1} from the origin nn with yn=2.0y_n = 2.0 and residual en=0.5e_n = 0.5. In each formula, blue terms are known data, magenta terms are residuals (past shocks), orange terms are earlier forecasts and grey terms are future shocks set to 0. Notice: the residual ene_n is used only at h=1h = 1; after that the forecast is 0.60.6 times the previous forecast.

    Forecast variance and prediction intervals #

    Theorem (forecast error of an ARIMA model). Let the model have the ψ\psi-weights ψ0=1,ψ1,ψ2,…\psi_0 = 1, \psi_1, \psi_2, \dots, defined by ψ(z)=θ(z)/(ϕ(z)(1−z)d)\psi(z) = \theta(z)/\bigl(\phi(z)(1 - z)^d\bigr). Assume the parameters are known and the shocks are white noise with variance σ2\sigma^2. Then the hh-step forecast error is

    en+h=yn+h−y^n+h∣n=∑j=0h−1ψj εn+h−j,Var⁡(en+h)=σ2∑j=0h−1ψj2.e_{n+h} = y_{n+h} - \hat y_{n+h\mid n} = \sum_{j=0}^{h-1}\psi_j\,\varepsilon_{n+h-j}, \qquad \operatorname{Var}(e_{n+h}) = \sigma^2\sum_{j=0}^{h-1}\psi_j^2 .

    Proof idea.

    1. For a stationary ARMA, the MA(∞\infty) form gives yn+h=μ+∑j≥0ψjεn+h−jy_{n+h} = \mu + \sum_{j\ge0}\psi_j\varepsilon_{n+h-j}.
    2. The terms with j≥hj \ge h contain shocks up to time nn: they are known from the data, and they form the forecast. The terms with j<hj < h contain the hh future shocks: they have mean 0 and are independent of the data, so they form the error.
    3. The shocks are uncorrelated, so the variances add: σ2∑j<hψj2\sigma^2\sum_{j<h}\psi_j^2.
    4. For d≥1d \ge 1 there is no stationary MA(∞\infty) form, but the argument still works for a finite horizon. Unroll the model recursion from the origin nn: yn+hy_{n+h} equals a function of the data up to nn plus ∑j<hψjεn+h−j\sum_{j<h}\psi_j\varepsilon_{n+h-j}, where the ψj\psi_j are the first hh coefficients of the power series θ(z)/(ϕ(z)(1−z)d)\theta(z)/\bigl(\phi(z)(1 - z)^d\bigr). ■\blacksquare

    The ψ\psi-weights come from the recursion of lesson 05, applied to the full AR polynomial ϕ∗(z)=ϕ(z)(1−z)d=1−ϕ1∗z−…\phi^*(z) = \phi(z)(1 - z)^d = 1 - \phi^*_1 z - \dots: ψ0=1\psi_0 = 1, ψj=θj+∑iϕi∗ψj−i\psi_j = \theta_j + \sum_i\phi^*_i\psi_{j-i}.

    Model ψj\psi_j (j≥1j \ge 1) Var⁡(en+h)/σ2\operatorname{Var}(e_{n+h})/\sigma^2 as h→∞h \to \infty
    AR(1) ϕj\phi^j (1−ϕ2h)/(1−ϕ2)(1 - \phi^{2h})/(1 - \phi^2) →1/(1−ϕ2)\to 1/(1 - \phi^2), the variance of yy
    MA(1) θ\theta for j=1j = 1, then 0 11 for h=1h = 1, then 1+θ21 + \theta^2 constant from h=2h = 2
    random walk 11 hh grows like hh
    ARIMA(0,1,1) 1+θ1 + \theta (=α= \alpha) 1+(h−1)(1+θ)21 + (h - 1)(1 + \theta)^2 grows like hh

    A 100(1−a) %100(1 - a)\,\% prediction interval under Gaussian shocks is

    y^n+h∣n±z1−a/2 σ^∑j<hψj2,z0.9=1.28 (80 %),z0.975=1.96 (95 %).\hat y_{n+h\mid n} \pm z_{1 - a/2}\,\hat\sigma\sqrt{\textstyle\sum_{j<h}\psi_j^2}, \qquad z_{0.9} = 1.28 \ (80\,\%), \quad z_{0.975} = 1.96 \ (95\,\%).

    At h=1h = 1 every model gives y^±1.96 σ^\hat y \pm 1.96\,\hat\sigma. For a stationary model the half-width tends to 1.961.96 times the standard deviation of yy; for d=1d = 1 it grows like h\sqrt h; for d=2d = 2 like h3/2h^{3/2}.

    Quick check

    An AR(1) has mean μ=10\mu = 10 and ϕ=0.5\phi = 0.5. The last observation is yn=14y_n = 14. What is the forecast y^n+2∣n\hat y_{n+2\mid n}?

    What dd and the constant do to the long run #

    The constant cc and the number of differences dd fix the shape of the long-run forecast (FPP3 §9.5):

    d=0d = 0 d=1d = 1 d=2d = 2
    c=0c = 0 goes to 0 goes to a constant follows a straight line
    c≠0c \ne 0 goes to the mean c/ϕ(1)c/\phi(1) follows a straight line follows a quadratic

    With d=1d = 1, cc is the average change per step: the drift. The random walk with drift forecasts y^n+h∣n=yn+hc\hat y_{n+h\mid n} = y_n + hc, a straight line from the last value. With d=2d = 2 a nonzero constant gives a quadratic trend, which is almost never sensible; software such as fable does not allow it. Higher dd also makes the intervals grow faster. So dd is a modelling decision about the long run, not only a way to pass a test.

    Seasonal ARIMA #

    For data with a seasonal period mm (12 for monthly data), the seasonal difference is ∇myt=(1−Bm)yt=yt−yt−m\nabla_m y_t = (1 - B^m)y_t = y_t - y_{t-m}. A seasonal ARIMA (p,d,q)(P,D,Q)m(p, d, q)(P, D, Q)_m multiplies non-seasonal and seasonal polynomials:

    Φ(Bm) ϕ(B) (1−Bm)D (1−B)d yt=c+Θ(Bm) θ(B) εt,\Phi(B^m)\,\phi(B)\,(1 - B^m)^D\,(1 - B)^d\,y_t = c + \Theta(B^m)\,\theta(B)\,\varepsilon_t ,

    with Φ(z)=1−Φ1z−⋯−ΦPzP\Phi(z) = 1 - \Phi_1 z - \dots - \Phi_P z^P and Θ(z)=1+Θ1z+⋯+ΘQzQ\Theta(z) = 1 + \Theta_1 z + \dots + \Theta_Q z^Q. The capital letters act at multiples of mm.

    Seasonal patterns in the ACF/PACF. The seasonal part shows up at lags m,2m,3m,…m, 2m, 3m, \dots:

    • a seasonal MA(1), (0,0,0)(0,0,1)12(0,0,0)(0,0,1)_{12}: one ACF spike at lag 12; the PACF decays over lags 12, 24, …;
    • a seasonal AR(1), (0,0,0)(1,0,0)12(0,0,0)(1,0,0)_{12}: the ACF decays over lags 12, 24, …; one PACF spike at lag 12.

    The airline model (0,1,1)(0,1,1)12(0,1,1)(0,1,1)_{12} (Box and Jenkins, first used for monthly airline passenger numbers):

    (1−B)(1−B12) yt=(1+θB)(1+ΘB12) εt.(1 - B)(1 - B^{12})\,y_t = (1 + \theta B)(1 + \Theta B^{12})\,\varepsilon_t .

    Expand both sides. The left side is yt−yt−1−yt−12+yt−13y_t - y_{t-1} - y_{t-12} + y_{t-13}. The right side is εt+θεt−1+Θεt−12+θΘεt−13\varepsilon_t + \theta\varepsilon_{t-1} + \Theta\varepsilon_{t-12} + \theta\Theta\varepsilon_{t-13}. So

    yt=yt−1+yt−12−yt−13+εt+θεt−1+Θεt−12+θΘεt−13.y_t = y_{t-1} + y_{t-12} - y_{t-13} + \varepsilon_t + \theta\varepsilon_{t-1} + \Theta\varepsilon_{t-12} + \theta\Theta\varepsilon_{t-13}.

    It has MA terms at lags 1, 12 and 13, but only two free coefficients θ,Θ\theta, \Theta (plus σ2\sigma^2). The lag-13 coefficient is the product θΘ\theta\Theta: the multiplicative form ties it to the other two. The model says: "this month's change from last year is last month's change from last year, corrected by recent errors at both time scales".

    Interactive figure: loading…

    Choose a transformation of the monthly Mauna Loa CO₂ series (1958–2001, Scripps; data from statsmodels). Top: the transformed series. Below: sample ACF (blue) and PACF (violet) up to lag 36 with the grey band ±1.96/n\pm 1.96/\sqrt n; dashed yellow lines mark the seasonal lags 12, 24, 36. Notice: the raw series and ∇y\nabla y have a huge, slowly decaying ACF; ∇12y\nabla_{12}y still decays slowly; only ∇∇12y\nabla\nabla_{12}y looks stationary, with negative spikes at lags 1 and 12. Those two spikes ask for an MA(1) and a seasonal MA(1): the airline model.

    Quick check

    Expand the airline model (1−B)(1−B12)yt=(1+θB)(1+ΘB12)εt(1 - B)(1 - B^{12})y_t = (1 + \theta B)(1 + \Theta B^{12})\varepsilon_t. Which lags of ε\varepsilon appear on the right side?

    The Box–Jenkins workflow #

    Box and Jenkins proposed an iterative cycle: identify, estimate, check, forecast. In the modern form of FPP3 §9.7:

    1. Plot the series. Look for outliers, level shifts and changes of variance.
    2. Transform if the variance grows with the level: a log or a Box–Cox transform (lesson 01).
    3. Difference until the series is stationary. Choose DD first (seasonal strength, or a clear seasonal pattern), then dd (a unit-root test such as KPSS, lesson 02). Rarely use d+D>2d + D > 2.
    4. Identify candidate orders from the ACF/PACF of the differenced series (lesson 05 table, plus the seasonal patterns above).
    5. Estimate candidates by maximum likelihood and compare them by AICc. Remember: only models with the same dd and DD are comparable by AICc.
    6. Check the residuals: ACF inside the band, Ljung–Box test with h−p−q−P−Qh - p - q - P - Q degrees of freedom. If they fail, go back to step 4.
    7. Forecast, and back-transform if you transformed in step 2.

    Back-transformation. If you model wt=log⁡ytw_t = \log y_t and forecast w^\hat w with forecast variance σh2\sigma_h^2, then exp⁡(w^)\exp(\hat w) is the forecast median of yy, not its mean. For a Gaussian ww the mean is exp⁡(w^+σh2/2)≈exp⁡(w^)(1+σh2/2)\exp(\hat w + \sigma_h^2/2) \approx \exp(\hat w)(1 + \sigma_h^2/2) (FPP3 §5.6 calls this the bias adjustment). Intervals back-transform directly: [exp⁡(lower),exp⁡(upper)][\exp(\text{lower}), \exp(\text{upper})].

    Automatic ARIMA. The Hyndman–Khandakar algorithm (auto.arima) automates steps 3–5: it picks dd by repeated KPSS tests and DD by a seasonal-strength measure, then searches p,qp, q (and P,QP, Q) stepwise. It starts from four models, for example ARIMA(2,dd,2) and ARIMA(0,dd,0), and moves pp and qq by ±1\pm 1, with and without the constant, as long as AICc improves. It is a strong default, but it still needs the residual check and a backtest.

    Interactive figure: loading…

    Step through the workflow on the CO₂ series. Each box shows the check made at that step: the plot, the ACF after differencing, an AICc table of candidate models (fitted with statsmodels), the residual ACF and Ljung–Box test of the chosen model, and the 3-year forecast with 80 % and 95 % intervals. Notice: AICc slightly prefers a model with one more parameter, while BIC prefers the airline model; both pass the residual check.

    Regression with ARIMA errors #

    Often other variables help: temperature for electricity load, prices for sales, holidays. A regression with ARIMA errors (also called dynamic regression or ARIMAX) is

    yt=β0+β1x1,t+⋯+βkxk,t+ηt,ηt∼ARIMA(p,d,q).y_t = \beta_0 + \beta_1 x_{1,t} + \dots + \beta_k x_{k,t} + \eta_t, \qquad \eta_t \sim \text{ARIMA}(p, d, q).

    The regression explains the effect of the covariates, and the ARIMA error ηt\eta_t models the autocorrelation that is left. Three points matter:

    • Fit both parts together by maximum likelihood. Ordinary least squares with autocorrelated errors gives wrong standard errors and can find spurious relations between unrelated trending series (lesson 02). If d=1d = 1, the software differences yy and all xx together.
    • To forecast, you need the future covariates. Calendar effects are known in advance; weather must itself be forecast, and its forecast error adds to yours.
    • Fourier terms for long or non-integer seasons. For daily data with a yearly season (m=365.25m = 365.25) or several seasons, a seasonal ARIMA with m=365m = 365 is impractical. Use covariates sin⁡(2πkt/m)\sin(2\pi kt/m) and cos⁡(2πkt/m)\cos(2\pi kt/m) for k=1,…,Kk = 1, \dots, K (a dynamic harmonic regression), choose KK by AICc, and let a short ARIMA model the rest.
    Changing variance: ARCH and GARCH

    ARIMA assumes a constant shock variance σ2\sigma^2. Financial returns violate this: calm periods and turbulent periods alternate (volatility clustering). The returns themselves are nearly uncorrelated, but their squares are strongly autocorrelated. Engle's ARCH model (1982) and Bollerslev's GARCH(1,1) (1986) model the conditional variance:

    εt=σtzt,zt∼iidN(0,1),σt2=ω+a1εt−12+b1σt−12,\varepsilon_t = \sigma_t z_t, \quad z_t \overset{iid}{\sim} \mathcal N(0, 1), \qquad \sigma_t^2 = \omega + a_1\varepsilon_{t-1}^2 + b_1\sigma_{t-1}^2 ,

    with ω>0\omega > 0, a1,b1≥0a_1, b_1 \ge 0 and a1+b1<1a_1 + b_1 < 1, so the unconditional variance ω/(1−a1−b1)\omega/(1 - a_1 - b_1) is finite. A large shock raises the next variance; b1b_1 makes the effect persist. ARIMA can model the mean and GARCH the variance of the same series. The point forecast does not change, but the intervals widen after turbulent days and narrow after calm ones.

    Answers to section 1. (1) The random walk. The AR(1) interval tends to ±1.96/1−ϕ2\pm 1.96/\sqrt{1 - \phi^2} and stops growing; the random-walk interval is ±1.96h\pm 1.96\sqrt h and grows without limit (±19.6\pm 19.6 at h=100h = 100). (2) A straight line: y^n+h∣n=10+0.5h\hat y_{n+h\mid n} = 10 + 0.5h. (3) At lags 1, 12 and 13 (the last one with coefficient θΘ\theta\Theta).

    4. Worked example #

    (a) ARMA(1,1) forecasts and intervals by hand. Model: yt=0.6 yt−1+εt+0.3 εt−1y_t = 0.6\,y_{t-1} + \varepsilon_t + 0.3\,\varepsilon_{t-1} with σ=1\sigma = 1 and mean 0. At the origin, yn=2.0y_n = 2.0 and the last residual is en=0.5e_n = 0.5.

    1. h=1h = 1: yn+1=0.6yn+εn+1+0.3εny_{n+1} = 0.6y_n + \varepsilon_{n+1} + 0.3\varepsilon_n. Replace εn+1\varepsilon_{n+1} by 0 and εn\varepsilon_n by ene_n: y^n+1∣n=0.6⋅2.0+0+0.3⋅0.5=1.2+0.15=1.35\hat y_{n+1\mid n} = 0.6 \cdot 2.0 + 0 + 0.3 \cdot 0.5 = 1.2 + 0.15 = 1.35.
    2. h=2h = 2: yn+2=0.6yn+1+εn+2+0.3εn+1y_{n+2} = 0.6y_{n+1} + \varepsilon_{n+2} + 0.3\varepsilon_{n+1}. Both shocks are in the future: y^n+2∣n=0.6⋅1.35=0.81\hat y_{n+2\mid n} = 0.6 \cdot 1.35 = 0.81.
    3. h=3h = 3: y^n+3∣n=0.6⋅0.81=0.486\hat y_{n+3\mid n} = 0.6 \cdot 0.81 = 0.486. From now on each forecast is 0.60.6 times the previous one.
    4. ψ\psi-weights: ψ0=1\psi_0 = 1, ψ1=θ+ϕ=0.9\psi_1 = \theta + \phi = 0.9, ψ2=ϕψ1=0.54\psi_2 = \phi\psi_1 = 0.54, ψ3=0.324\psi_3 = 0.324.
    5. Variances: h=1h = 1: 11; h=2h = 2: 1+0.81=1.811 + 0.81 = 1.81; h=3h = 3: 1.81+0.2916=2.10161.81 + 0.2916 = 2.1016.
    6. 95 % intervals: 1.35±1.961.35 \pm 1.96; 0.81±1.961.81=0.81±2.640.81 \pm 1.96\sqrt{1.81} = 0.81 \pm 2.64; 0.486±1.962.1016=0.486±2.840.486 \pm 1.96\sqrt{2.1016} = 0.486 \pm 2.84. As h→∞h \to \infty the half-width tends to 1.96∑jψj2=1.961+0.81/(1−0.36)=1.96⋅1.505=2.951.96\sqrt{\sum_j\psi_j^2} = 1.96\sqrt{1 + 0.81/(1 - 0.36)} = 1.96 \cdot 1.505 = 2.95.
    Quick check

    For the ARMA(1,1) of example (a), with ϕ=0.6\phi = 0.6, θ=0.3\theta = 0.3 and σ=1\sigma = 1, a new value of θ=−0.6\theta = -0.6 is tried. What happens to the 95 % interval at h=2h = 2?

    (b) SES as ARIMA(0,1,1). SES with α=0.3\alpha = 0.3 is ARIMA(0,1,1) with θ=−0.7\theta = -0.7. All ψj=1+θ=0.3\psi_j = 1 + \theta = 0.3 for j≥1j \ge 1, so Var⁡(en+h)=σ2(1+0.09(h−1))\operatorname{Var}(e_{n+h}) = \sigma^2(1 + 0.09(h - 1)). With σ=2\sigma = 2: the 95 % half-width is 1.96⋅2=3.921.96 \cdot 2 = 3.92 at h=1h = 1 and 1.96⋅21+0.09⋅11=3.92⋅1.411=5.531.96 \cdot 2\sqrt{1 + 0.09 \cdot 11} = 3.92 \cdot 1.411 = 5.53 at h=12h = 12. The point forecast is flat.

    (c) The airline model on CO₂. Monthly mean CO₂ at Mauna Loa, March 1958 to December 2001 (n=526n = 526; the 5 months without data are filled by linear interpolation). The variance does not grow with the level, so no log is needed. The ACF of ∇∇12y\nabla\nabla_{12}y has spikes at lag 1 (−0.30-0.30) and lag 12 (−0.41-0.41). Maximum likelihood (statsmodels SARIMAX) gives

    (1−B)(1−B12) yt=(1−0.353 B)(1−0.855 B12) εt,σ^2=0.093.(1 - B)(1 - B^{12})\,y_t = (1 - 0.353\,B)(1 - 0.855\,B^{12})\,\varepsilon_t, \qquad \hat\sigma^2 = 0.093 .
    Model AICc BIC
    (0,1,1)(0,1,1)12(0,1,1)(0,1,1)_{12} airline 259.2 271.9
    (1,1,1)(0,1,1)12(1,1,1)(0,1,1)_{12} 256.9 273.8
    (0,1,2)(0,1,1)12(0,1,2)(0,1,1)_{12} 258.3 275.2
    (0,1,1)(1,1,1)12(0,1,1)(1,1,1)_{12} 260.9 277.8
    (2,1,0)(0,1,1)12(2,1,0)(0,1,1)_{12} 265.6 282.5
    (1,1,0)(1,1,0)12(1,1,0)(1,1,0)_{12} 404.0 416.7

    AICc prefers (1,1,1)(0,1,1)12(1,1,1)(0,1,1)_{12} by 2.3 units; BIC prefers the airline model. A difference of about 2 is small, so we keep the simpler airline model. Its residuals pass the Ljung–Box test with h=24h = 24 (Q=20.0Q = 20.0, 22 degrees of freedom, p-value 0.58). Forecasts from December 2001, with 95 % intervals:

    Horizon Month Forecast (ppm) σ^h\hat\sigma_h 95 % interval
    1 Jan 2002 371.98 0.30 371.38 – 372.57
    12 Dec 2002 372.54 0.72 371.12 – 373.95
    24 Dec 2003 374.13 1.07 372.03 – 376.22
    36 Dec 2004 375.72 1.39 372.99 – 378.44

    The forecast continues the trend and the season. The interval widens like that of a model with two differences.

    5. In code #

    NumPy checks the worked example (a) and the ψ\psi-weight variance; the airline fit uses statsmodels.

    import numpy as np
    
    def psi_weights(phi_star, theta, m):
        """psi-weights for the full AR polynomial phi*(z) = 1 - sum phi*_i z^i (plus-sign MA)."""
        psi = np.zeros(m); psi[0] = 1.0
        for j in range(1, m):
            psi[j] = (theta[j - 1] if j <= len(theta) else 0.0) + sum(
                phi_star[i - 1] * psi[j - i] for i in range(1, min(j, len(phi_star)) + 1))
        return psi
    
    # (a) ARMA(1,1): phi = 0.6, theta = 0.3, y_n = 2.0, e_n = 0.5
    f = [0.6 * 2.0 + 0.3 * 0.5]
    for h in range(2, 4):
        f.append(0.6 * f[-1])
    psi = psi_weights([0.6], [0.3], 3)
    half = 1.96 * np.sqrt(np.cumsum(psi**2))
    print(np.round(f, 3), psi, np.round(half, 2))   # [1.35 0.81 0.486] [1. 0.9 0.54] [1.96 2.64 2.84]
    
    # (b) ARIMA(0,1,1) with theta = -0.7: phi*(z) = 1 - z, so phi* = [1]
    psi = psi_weights([1.0], [-0.7], 12)
    print(psi[:4], round(1.96 * 2 * np.sqrt(np.sum(psi**2)), 2))   # [1. 0.3 0.3 0.3] 5.53
    
    # (c) airline model on monthly CO2 (the data ship with statsmodels)
    import statsmodels.api as sm
    from statsmodels.tsa.statespace.sarimax import SARIMAX
    co2 = sm.datasets.co2.load_pandas().data["co2"].resample("MS").mean().interpolate().to_numpy()
    res = SARIMAX(co2, order=(0, 1, 1), seasonal_order=(0, 1, 1, 12)).fit(disp=False)
    print(np.round(res.params, 3))                  # [-0.353 -0.855  0.093]
    fc = res.get_forecast(36)
    print(np.round(fc.predicted_mean[[0, 11, 23, 35]], 2))   # [371.98 372.54 374.13 375.72]
    

    6. Where it shows up #

    • Forecasting practice. Retail demand, call-centre volumes, energy load, tourism and macroeconomic series are forecast with (seasonal) ARIMA or with its automatic versions; statistical agencies use X-13ARIMA-SEATS, a seasonal ARIMA, to remove the season from official statistics.
    • Benchmarks for ML. The M4 and M5 competitions and most deep-learning forecasting papers report AutoARIMA and ETS as baselines. Lesson 08 and 09 compare against them with the backtests of lesson 03.
    • Hybrid models. Gradient boosting on the residuals of an ARIMA, or ARIMA errors in a regression on weather covariates, combine the strengths of both worlds.
    • Science. Seasonal ARIMA describes CO₂ and temperature records, river discharge and epidemic surveillance counts; residuals above the upper interval flag outbreaks or sensor faults (lesson 11).

    7. Common mistakes #

    • Over-differencing. Differencing a stationary series introduces an MA unit root: the differenced series has ρ(1)\rho(1) near −0.5-0.5 and a non-invertible MA part, and the intervals become too wide. If the ACF of the differenced series starts with a large negative spike, you may have differenced once too often.
    • Comparing AICc across different dd or DD. Differencing changes the data that the likelihood is computed on. Choose dd and DD first, then compare orders.
    • Reading intervals as guaranteed. They ignore parameter uncertainty and model error. Check their coverage in a backtest.
    • Forgetting the back-transform bias. exp⁡(forecast of log⁡y)\exp(\text{forecast of }\log y) is a median. Sums of such forecasts (for example monthly forecasts added up to a yearly total) are then biased low.
    • Adding a constant with d=2d = 2, or by habit with d=1d = 1. With d=1d = 1 the constant is a drift that continues forever; include it only if the trend is real.
    • Using future covariates you will not have. A regression with ARIMA errors evaluated with the observed future temperature looks better than it will be in production (leakage, lesson 03).

    8. Check yourself #

    1. Write ARIMA(1,1,0) with ϕ=0.4\phi = 0.4 as an equation for yty_t in terms of past yy.
      Answer(1−0.4B)(1−B)yt=εt(1 - 0.4B)(1 - B)y_t = \varepsilon_t. The left side is (1−1.4B+0.4B2)yt(1 - 1.4B + 0.4B^2)y_t, so yt=1.4yt−1−0.4yt−2+εty_t = 1.4y_{t-1} - 0.4y_{t-2} + \varepsilon_t. It is an AR(2) with a unit root (1−1.4z+0.4z2=(1−z)(1−0.4z)1 - 1.4z + 0.4z^2 = (1 - z)(1 - 0.4z)).
    2. For that model, compute the forecasts at h=1,2h = 1, 2 from yn=20y_n = 20, yn−1=18y_{n-1} = 18.
      Answery^n+1=1.4⋅20−0.4⋅18=28−7.2=20.8\hat y_{n+1} = 1.4 \cdot 20 - 0.4 \cdot 18 = 28 - 7.2 = 20.8. y^n+2=1.4⋅20.8−0.4⋅20=29.12−8=21.12\hat y_{n+2} = 1.4 \cdot 20.8 - 0.4 \cdot 20 = 29.12 - 8 = 21.12. The forecast changes are 0.8=0.4⋅20.8 = 0.4 \cdot 2 and 0.32=0.42⋅20.32 = 0.4^2 \cdot 2, where 22 is the last observed change yn−yn−1y_n - y_{n-1}. They decay geometrically, so the forecast levels off at 20+2⋅0.4/(1−0.4)=21.3320 + 2 \cdot 0.4/(1 - 0.4) = 21.33.
    3. What are the ψ\psi-weights of the random walk with drift, and the 80 % half-width at h=9h = 9 for σ=1\sigma = 1?
      AnswerAll ψj=1\psi_j = 1 (the drift does not change them). Var⁡(en+9)=9\operatorname{Var}(e_{n+9}) = 9, so the half-width is 1.28⋅3=3.841.28 \cdot 3 = 3.84.
    4. Why does ARIMA(0,1,1) with θ\theta close to −1-1 behave almost like a constant mean model?
      Answer(1−B)yt=(1+θB)εt(1 - B)y_t = (1 + \theta B)\varepsilon_t with θ→−1\theta \to -1 makes both sides share the factor (1−B)(1 - B), which cancels: yt≈c+εty_t \approx c + \varepsilon_t. In SES terms, α=1+θ→0\alpha = 1 + \theta \to 0: the level hardly moves, so the forecast is close to the long-run average.
    5. A monthly series shows, after ∇∇12\nabla\nabla_{12}, a single significant ACF spike at lag 12 and PACF spikes at lags 12, 24 and 36 that shrink. Which seasonal term do you add?
      AnswerA seasonal MA(1): Q=1Q = 1, the factor (1+ΘB12)(1 + \Theta B^{12}). A cut-off in the ACF at the seasonal lag with a decaying seasonal PACF is the MA signature, one season level up.
    6. You fit a model to log⁡y\log y and get w^=5\hat w = 5 with σh2=0.04\sigma_h^2 = 0.04. Give the median and the mean forecast of yy.
      AnswerMedian e5=148.4e^5 = 148.4. Mean e5+0.02=151.4e^{5 + 0.02} = 151.4 (the approximation e5(1+0.02)e^5(1 + 0.02) also gives 151.4).
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. Write the ARIMA(p,d,qp, d, q) model with the backshift operator.

    2. Which ARIMA model is simple exponential smoothing with parameter α\alpha?

    3. When you forecast with an ARIMA model, what do you put in for future shocks and for past shocks?

    4. Give the hh-step forecast error variance of an ARIMA model.

    5. How does the 95 % interval of a random walk grow with the horizon hh?

    6. Compute the forecast y^n+2∣n\hat y_{n+2\mid n} of an AR(1) with μ=10\mu = 10, ϕ=0.5\phi = 0.5, yn=14y_n = 14.

    7. What is the airline model, and at which lags does it have MA terms?

    8. What does a seasonal difference with D=1D = 1 do?

    9. You forecast log⁡y\log y. Is exp⁡(w^)\exp(\hat w) the mean or the median forecast of yy?

    10. What do you need to forecast with a regression with ARIMA errors?

    Open the review →

    9. Interview angle #

    • "Explain ARIMA in one minute." — Difference the series dd times to make it stationary, model the differences as ARMA(p,qp, q), and sum back to forecast. Choose dd by unit-root tests, pp and qq by AICc, and check the residuals.
    • "Why do ARIMA intervals widen?" — The forecast error is a sum of the future shocks weighted by the ψ\psi-weights. With differencing, every shock has a permanent effect, so the variance grows linearly with the horizon.
    • "ARIMA or exponential smoothing?" — They overlap (SES = ARIMA(0,1,1), and some Holt models are ARIMA too), but neither contains the other. Choose by a backtest, not by habit.
    • "How do you handle multiple seasonalities or daily data with a yearly cycle?" — Fourier terms in a regression with ARIMA errors, or a state-space / ML model; not a SARIMA with m=365m = 365.
    • "Your 95 % intervals cover only 85 % in the backtest. Why?" — Parameter uncertainty, model misspecification, non-Gaussian or heteroscedastic errors. Fixes: bootstrap residuals, GARCH, or conformal calibration.

    10. Practice #

    The notebook for this lesson runs in Colab. It has five exercises, each with a check cell and a folded solution:

    1. arma_forecast(y, resid, phi, theta, c, h): the forecast recursion, checked on AR(1) against μ+ϕh(yn−μ)\mu + \phi^h(y_n - \mu) and on the worked example (a).
    2. psi_weights(phi_star, theta, m): the recursion, checked on AR(1) (ϕj\phi^j) and MA(1).
    3. forecast_intervals for ARIMA(0,1,1) and their coverage by simulation.
    4. Fit a seasonal AR model by least squares on ∇∇12\nabla\nabla_{12} CO₂, undo the differences to forecast, and compare it with the seasonal naive forecast on the last two years.
    5. SES = ARIMA(0,1,1): the same one-step forecasts with θ=α−1\theta = \alpha - 1.

    An optional cell fits the airline model with statsmodels SARIMAX if it is installed.

    11. Watch and read #

    Differencing plus ARMA, as in section 3.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The seasonal polynomials and the multiplicative form.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    A short overview of the whole ARIMA idea.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    For the GARCH block: a model for changing variance.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play