typestar

SARIMAX in Python

Seasonal orders and exogenous regressors, in one model.

import numpy as np
import pandas as pd
from statsmodels.tsa.statespace.sarimax import SARIMAX

rng = np.random.default_rng(15)
n = 160
season = 6 * np.sin(2 * np.pi * np.arange(n) / 12)
practice = rng.uniform(0, 2, size=n)
series = pd.Series(90 + season + 3 * practice + rng.normal(0, 1, n))

model = SARIMAX(series, exog=practice, order=(1, 0, 0),
                seasonal_order=(1, 0, 0, 12)).fit(disp=False)
print(model.params.round(3).to_dict())

future_exog = np.full((4, 1), 1.0)
forecast = model.get_forecast(steps=4, exog=future_exog)
print(forecast.predicted_mean.round(2).tolist())

How it works

  1. seasonal_order is (P, D, Q, s) with s the period.
  2. exog adds outside predictors to the state space.
  3. The forecast needs future exog values too.

Keywords and builtins used here

The run, in numbers

Lines
17
Characters to type
575
Tokens
190
Three-star pace
115 tpm

At the three-star pace of 115 tokens a minute, this run takes about 99 seconds.

Type this snippet

Step 5 of 6 in Time series, step 17 of 19 in Statistics with statsmodels.

← Previous Next →