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
seasonal_orderis (P, D, Q, s) with s the period.exogadds outside predictors to the state space.- The forecast needs future exog values too.
Keywords and builtins used here
asprint
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.
Step 5 of 6 in Time series, step 17 of 19 in Statistics with statsmodels.