SNARIMAX¶
SNARIMAX model.
SNARIMAX stands for (S)easonal (N)on-linear (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 (Non-linear): 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 (int)
Order of the autoregressive part. This is the number of past target values that will be included as features.
-
d (int)
Differencing order.
-
q (int)
Order of the moving average part. This is the number of past error terms that will be included as features.
-
m (int) – defaults to
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 (int) – defaults to
0
Seasonal order of the autoregressive part. This is the number of past target values that will be included as features.
-
sd (int) – defaults to
0
Seasonal differencing order.
-
sq (int) – defaults to
0
Seasonal order of the moving average part. This is the number of past error terms that will be included as features.
-
regressor (base.Regressor) – defaults to
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 = 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}')
1961-01-01 491.988
1961-02-01 447.593
1961-03-01 481.405
1961-04-01 566.278
1961-05-01 551.561
1961-06-01 605.414
1961-07-01 711.140
1961-08-01 668.204
1961-09-01 570.517
1961-10-01 549.589
1961-11-01 466.344
1961-12-01 506.945
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 = 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}')
1961-01-01 446.874
1961-02-01 423.998
1961-03-01 439.957
1961-04-01 457.958
1961-05-01 457.303
1961-06-01 496.554
1961-07-01 553.798
1961-08-01 551.388
1961-09-01 479.620
1961-10-01 440.613
1961-11-01 409.914
1961-12-01 433.774
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') – defaults to
None