mojolearn.ARIMA#

class mojolearn.ARIMA(order=(1, 0, 0), *, seasonal_order=(0, 0, 0, 0), trend=None, method='ml', maxiter=1000, verbose=False, output_type=None)[source]#

Bases: NumericModeMixin

Batched ARIMA and seasonal ARIMA fitted by maximum likelihood.

Fits a separate (S)ARIMA model to every series in a batch at once, with a batched Kalman filter and a batched L-BFGS optimizer. The batch is not a multivariate model: each series gets its own parameters.

Differences from statsmodels and cuML that change how you call it:

  • The data goes to fit, not to the constructor: ARIMA(order=(1, 0, 0)).fit(y). y is (batch_size, n_obs), one series per row; a 1-D y is one series, and outputs are 2-D either way.

  • The default order is (1, 0, 0) (cuML uses (1, 1, 1), statsmodels (0, 0, 0)).

  • trend follows statsmodels: None means an intercept ('c') when the model is not differenced (d + D == 0) and none ('n') otherwise. trend='c' is cuML’s fit_intercept=True.

  • In predict(), end is excluded, as in cuML (statsmodels returns end itself).

  • Computation is float32; a float64 y is converted (and copied).

  • Only maximum likelihood (method='ml') is available. Missing values (NaN) are not supported and are refused; prediction intervals are not available.

Parameters:
  • order (tuple of int (p, d, q), default (1, 0, 0)) – Non-seasonal AR order, differencing and MA order. p and q are at most 8 and d + D at most 2.

  • seasonal_order (tuple of int (P, D, Q, s), default (0, 0, 0, 0)) – Seasonal AR order, differencing, MA order and period. A seasonal term needs s >= 2 with s > p and s > q. The state size is bounded: r = max(p + s P, q + s Q + 1) at most 5 and d + s D + r at most 8; larger orders, and an order with no parameters at all, are refused at fit.

  • trend ({None, “n”, “c”}, default None) – 'c' fits an intercept, 'n' none, None applies statsmodels’ rule above. 't', 'ct' and polynomial trends raise NotImplementedError; pass the time index as an exog column instead.

  • method ({“ml”}, default “ml”) – Likelihood. 'css' and 'css-ml' are recognized but not implemented and raise an error at fit; other names raise ValueError.

  • maxiter (int, default 1000) – L-BFGS iteration cap, at least 1.

  • verbose (bool, default False) – Must be False; True raises NotImplementedError.

  • output_type (None, default None) – Must be None (cuML’s array-type selector is not supported).

  • numeric_mode (str or None, default None) – Numeric mode for this model ('identical', 'deterministic' or 'fast'); None uses the process default (see mojolearn.set_numeric_mode()).

Variables:
  • params_ (Array of shape (batch_size, N), float32) – Fitted parameters per series, packed as mu (when there is an intercept), beta (one per regressor), ar (p), ma (q), sar (P), sma (Q), then sigma2; N = p + q + P + Q + k + n_exog + 1. numpy.asarray on a mojolearn.Array is zero-copy.

  • mu_ (ndarray of shape (batch_size,), float32) – Intercepts. Raises AttributeError when the model has no intercept.

  • beta_ (Array of shape (batch_size, n_exog), float32) – Regression coefficients. Raises AttributeError when the fit had no exog.

  • sma_ (ar_, ma_, sar_,) – Named blocks of params_; zero-width when the order has no such block.

  • sigma2_ (ndarray of shape (batch_size,), float32) – Innovation variance.

  • llf_ (ndarray of shape (batch_size,), float64) – Log-likelihood at the fitted parameters.

  • bic_ (aic_,) – 2 N - 2 llf and log(T) N - 2 llf, with T = n_obs - (d + s D).

  • n_iter_ (ndarray of shape (batch_size,), int32) – L-BFGS iterations per series.

  • retcode_ (ndarray of shape (batch_size,), int32) – 0 when the series converged. A non-zero entry means that series reached maxiter without converging; this is reported, not raised, so check it.

  • x0_ (x_,) – The unconstrained optimum and the starting point, in the same packing.

  • n_exog_ (int) – Number of exogenous regressors (0 without exog).

  • batch_size_ (n_obs_,) – Shape of the fitted y.

  • k_ (int) – 1 with an intercept, 0 without.

  • complexity_ (int) – N, the parameter count per series.

  • input_copied_ (bool) – Whether fit had to copy y to reach float32 C order.

Examples

>>> import numpy as np, mojolearn
>>> y = np.random.default_rng(0).standard_normal((16, 200)).cumsum(axis=1).astype(np.float32)
>>> model = mojolearn.ARIMA(order=(1, 1, 1)).fit(y)
>>> model.forecast(10).shape
(16, 10)
__init__(order=(1, 0, 0), *, seasonal_order=(0, 0, 0, 0), trend=None, method='ml', maxiter=1000, verbose=False, output_type=None)[source]#

Methods

fit(y[, exog])

Fit one model per series.

forecast(steps[, exog])

Forecast steps past the end of every series.

get_params([deep])

load(path)

Load a model written by save().

numeric_mode_used()

The numeric mode this estimator runs in.

predict([start, end, exog])

In-sample and out-of-sample predictions.

save(path)

Save the fitted model to an .npz file.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

Attributes

ar_

beta_

ma_

mu_

sar_

sigma2_

sma_

fit(y, exog=None)[source]#

Fit one model per series.

Parameters:
  • y (array-like of shape (batch_size, n_obs) or (n_obs,)) – The series, one per row; a 1-D y is one series. Must be finite (NaN is refused, not treated as missing).

  • exog (array-like of shape (batch_size, n_obs, n_exog) or None, default None) – Exogenous regressors beside every observation (regression with ARIMA errors, as in cuML; at most 17 regressors, finite). Shorter forms are accepted: (n_obs,) or (n_obs, n_exog) for a 1-D y, and (batch_size, n_obs) for one regressor.

Returns:

self – Check retcode_: a series that ran to maxiter is reported there, not raised on.

predict(start=0, end=None, exog=None)[source]#

In-sample and out-of-sample predictions.

end is excluded, as in cuML: predict(0, n_obs) returns every in-sample prediction (statsmodels’ equivalent is predict(0, n_obs - 1)). end > n_obs continues into a forecast. Predictions before index d + s * D are NaN, because differencing consumes those observations.

Parameters:
  • start (int, default 0) – First index to predict.

  • end (int or None, default None) – Index to stop before; None means n_obs. Must be greater than start.

  • exog (array-like of shape (batch_size, end - n_obs, n_exog) or None, default None) – Future regressor values. Required when the model has regressors and end > n_obs; refused otherwise.

Returns:

Array of shape (batch_size, end - start), float32

Raises:

ValueError – If the model is not fitted, start >= end, or exog is missing, unexpected or of the wrong width.

forecast(steps, exog=None)[source]#

Forecast steps past the end of every series.

Identical to predict(n_obs, n_obs + steps).

Parameters:
  • steps (int) – Forecast horizon, at least 1.

  • exog (array-like of shape (batch_size, steps, n_exog) or None, default None) – Future regressor values; required when the model has regressors.

Returns:

Array of shape (batch_size, steps), float32

save(path)[source]#

Save the fitted model to an .npz file.

The file holds the training series (needed for prediction), the fitted parameters and diagnostics, and the settings. A loaded model predicts, forecasts and exposes every fitted attribute; it does not refit. On a CPU-only install a saved model can be used for inference with ARIMA.load(path) or mojolearn.host_model(path).

Parameters:

path (str or path-like) – Destination file.

classmethod load(path)[source]#

Load a model written by save().

Parameters:

path (str or path-like) – File written by save().

Returns:

ARIMA – A fitted model that predicts and forecasts; it does not refit.

Raises:

ValueError – If the file is not a valid saved ARIMA model.