Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 40 ยท ARIMA and SARIMA

ARIMA Model Fitting and Forecasting

Combine AR, differencing and MA into ARIMA(p, d, q): fit a model with statsmodels, read its summary, check the residuals, and produce forecasts with confidence intervals.

ARIMA stands for AutoRegressive Integrated Moving Average. It joins the two models you have just met, AR and MA, with one extra ingredient: differencing, which turns a trending series into a stationary one. It is the workhorse statistical model for forecasting a single series, and it is a strong baseline against which fancier methods must prove themselves.

An ARIMA model is written ARIMA(p, d, q):

  • p: the number of autoregressive lags.
  • d: how many times the series is differenced to remove trend.
  • q: the number of lagged shocks (the MA part).

What "integrated" means

Differencing replaces each value by its change from the previous one: y'[t] = y[t] - y[t-1]. A series with a steady upward drift becomes a flat series of changes. A random walk, which is not stationary, becomes plain white noise.

ARIMA(p, 1, q) simply fits an ARMA(p, q) model to the differenced series and then integrates (cumulatively sums) the forecasts back to the original scale. That is why the "I" is in the name.

Python Differencing removes a trend

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The level shifts a lot between halves, but the differenced series has nearly the same mean in both, which is what stationarity requires.

Fit an ARIMA model

statsmodels.tsa.arima.model.ARIMA fits the model by maximum likelihood. Give it a Series with a proper date index and a freq; the forecast will then carry real dates. The data below is a simulated monthly series with a trend and AR(1) noise.

Python Fit ARIMA(1,1,0) and read the summary

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the coefficient table, read three things:

  1. coef: the estimate (ar.L1 is the AR coefficient on the differenced series; sigma2 is the shock variance).
  2. P>|z|: a small p-value (below 0.05) means the coefficient is distinguishable from zero.
  3. The confidence interval [0.025, 0.975]: if it straddles zero, the term may not be needed.

With d = 1, ARIMA does not add a constant, because a constant after differencing would mean a linear trend in the original series. If your data has a drift, pass trend="t" (a linear trend) explicitly.

Check the residuals

A good model squeezes all the structure out of the data, leaving residuals that look like white noise: zero mean, constant variance, no autocorrelation. The Ljung-Box test checks the last part; its null hypothesis is "no autocorrelation", so you want a large p-value.

Python Ljung-Box on the residuals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The bare random walk (0, 1, 0) leaves autocorrelation behind, so its p-value is small. Adding the AR term cures it. Note how AIC and the residual test point in the same direction here; when they disagree, prefer the simpler model whose residuals pass.

We skip the first residual with [1:] because differencing makes the first residual an artifact.

Forecast with confidence intervals

get_forecast returns both the point forecast and an interval. The interval widens with the horizon, because uncertainty compounds: each future value depends on earlier values that are themselves uncertain.

Python 12-month forecast with a 95% interval

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the width of the interval at the first and last row: the band at month 12 is much wider than at month 1. That growth is honest information, not a flaw.

flowchart LR
    A["Plot the series"] --> B["Choose d: difference until stationary"]
    B --> C["Pick p and q"]
    C --> D["Fit ARIMA"]
    D --> E{"Residuals white noise?"}
    E -->|"no"| C
    E -->|"yes"| F["Forecast + intervals"]

The Box-Jenkins cycle. The next lesson covers the "pick p and q" step in detail.

Common mistakes

  • Missing frequency. A Series without a date frequency makes statsmodels warn and loses dates on the forecast. Use pd.date_range(..., freq=...) or asfreq.
  • Forgetting the trend term. With d = 1, no drift is modeled unless you ask for it. A trending series forecast as a flat line is the classic symptom.
  • Differencing too much. One or two differences are almost always enough. More makes the forecast noisier and injects negative MA structure.
  • Judging a model by in-sample fit only. AIC compares models fitted to the same data; it does not say how well you will forecast new data. See cross-validation.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does d = 1 in ARIMA(p, d, q) do?

    d is the number of times the series is differenced to make it stationary; forecasts are then integrated back to the original scale.
  2. 2. After fitting, the Ljung-Box p-value on the residuals is 0.002. What does that suggest?

    The test's null hypothesis is no autocorrelation. A tiny p-value rejects it, so structure remains in the residuals.
  3. 3. Why does the forecast interval get wider with the horizon?

    Each future value depends on earlier, uncertain values, so the forecast error variance grows with the horizon.
  4. 4. You fit ARIMA(1,1,0) to a clearly rising series and the forecast is a flat line. What is the likely fix?

    With d = 1 no constant is included by default, so no drift is modeled. Requesting a linear trend restores the upward slope.

Summary

  • ARIMA(p, d, q) = AR(p) + differencing d times + MA(q), integrated back to the original scale.
  • Fit with ARIMA(y, order=(p, d, q)).fit() on a series with a date index; read coefficients and their p-values.
  • Residuals must look like white noise; check them with the Ljung-Box test.
  • Use get_forecast for point forecasts and widening intervals; add trend="t" when d = 1 and the series drifts.

Next: Choosing ARIMA Orders: Box-Jenkins and AIC