SNARIMAX¶
SNARIMAX model.
SNARIMAX stands for (S)easonal (N)onlinear (A)uto(R)egressive (I)ntegrated (M)oving(A)verage with e(X)ogenous inputs model.
This model generalizes many established time series models in a single interface that can be trained online. It assumes that the provided training data is ordered in time and is uniformly spaced. It is made up of the following components:

S (Seasonal)

N (Nonlinear): Any online regression model can be used, not necessarily a linear regression
as is done in textbooks.  AR (Autoregressive): Lags of the target variable are used as features.

I (Integrated): The model can be fitted on a differenced version of a time series. In this
context, integration is the reverse of differencing.  MA (Moving average): Lags of the errors are used as features.

X (Exogenous): Users can provide additional features. Care has to be taken to include
features that will be available both at training and prediction time.
Each of these components can be switched on and off by specifying the appropriate parameters. Classical time series models such as AR, MA, ARMA, and ARIMA can thus be seen as special parametrizations of the SNARIMAX model.
This model is tailored for time series that are homoskedastic. In other words, it might not work well if the variance of the time series varies widely along time.
Parameters¶

p
Type → int
Order of the autoregressive part. This is the number of past target values that will be included as features.

d
Type → int
Differencing order.

q
Type → int
Order of the moving average part. This is the number of past error terms that will be included as features.

m
Type → int
Default →
1
Season length used for extracting seasonal features. If you believe your data has a seasonal pattern, then set this accordingly. For instance, if the data seems to exhibit a yearly seasonality, and that your data is spaced by month, then you should set this to 12. Note that for this parameter to have any impact you should also set at least one of the
p
,d
, andq
parameters. 
sp
Type → int
Default →
0
Seasonal order of the autoregressive part. This is the number of past target values that will be included as features.

sd
Type → int
Default →
0
Seasonal differencing order.

sq
Type → int
Default →
0
Seasonal order of the moving average part. This is the number of past error terms that will be included as features.

regressor
Type → base.Regressor  None
Default →
None
The online regression model to use. By default, a
preprocessing.StandardScaler
piped with alinear_model.LinearRegression
will be used.
Attributes¶

differencer (Differencer)

y_trues (collections.deque)
The
p
past target values. 
errors (collections.deque)
The
q
past error values.
Examples¶
import datetime as dt
from river import datasets
from river import time_series
from river import utils
period = 12
model = time_series.SNARIMAX(
p=period,
d=1,
q=period,
m=period,
sd=1
)
for t, (x, y) in enumerate(datasets.AirlinePassengers()):
model.learn_one(y)
horizon = 12
future = [
{'month': dt.date(year=1961, month=m, day=1)}
for m in range(1, horizon + 1)
]
forecast = model.forecast(horizon=horizon)
for x, y_pred in zip(future, forecast):
print(x['month'], f'{y_pred:.3f}')
19610101 494.542
19610201 450.825
19610301 484.972
19610401 576.401
19610501 559.489
19610601 612.251
19610701 722.410
19610801 674.604
19610901 575.716
19611001 562.808
19611101 477.049
19611201 515.191
Classic ARIMA models learn solely on the time series values. You can also include features built at each step.
import calendar
import math
from river import compose
from river import linear_model
from river import optim
from river import preprocessing
def get_month_distances(x):
return {
calendar.month_name[month]: math.exp((x['month'].month  month) ** 2)
for month in range(1, 13)
}
def get_ordinal_date(x):
return {'ordinal_date': x['month'].toordinal()}
extract_features = compose.TransformerUnion(
get_ordinal_date,
get_month_distances
)
model = (
extract_features 
time_series.SNARIMAX(
p=1,
d=0,
q=0,
m=12,
sp=3,
sq=6,
regressor=(
preprocessing.StandardScaler() 
linear_model.LinearRegression(
intercept_init=110,
optimizer=optim.SGD(0.01),
intercept_lr=0.3
)
)
)
)
for x, y in datasets.AirlinePassengers():
model.learn_one(x, y)
forecast = model.forecast(horizon=horizon)
for x, y_pred in zip(future, forecast):
print(x['month'], f'{y_pred:.3f}')
19610101 444.821
19610201 432.612
19610301 457.739
19610401 465.544
19610501 476.575
19610601 516.255
19610701 565.405
19610801 572.470
19610901 512.645
19611001 475.919
19611101 438.033
19611201 456.892
Methods¶
forecast
Makes forecast at each step of the given horizon.
Parameters
 horizon — 'int'
 xs — 'list[dict]  None' — defaults to
None
learn_one
Updates the model.
Parameters
 y — 'float'
 x — 'dict  None' — defaults to
None