Vector Autoregression (VAR) for multivariate time series forecasting.
Classes
Vector Autoregression for multivariate time series forecasting.
__init__( self, lags: Union[int, str] = 1, maxlags: int | None = None, ic: str = 'aic', trend: str = 'c', )
Overview
-
Stack the
plagged observations of every series into a single
- Estimate the coefficient matrices by ordinary least squares.
-
Optionally choose the lag order over
1..maxlagsby AIC or BIC,
- Forecast recursively by feeding each prediction back in as the most
Theory
A VAR of order p on k series is
with y_t \in \mathbb{R}^k, A_i \in \mathbb{R}^{k \times k} and c \in \mathbb{R}^k. Writing z_t = (1, y_{t-1}^\top, \dots, y_{t-p}^\top)^\top and stacking the rows gives Y = Z B + E, whose least-squares solution is
Because every equation shares exactly the same regressors, the seemingly-unrelated-regressions correction collapses and equation-by-equation OLS is the exact conditional maximum-likelihood estimator for a Gaussian VAR with an identical lag structure across equations -- there is nothing to gain from a joint GLS step.
Lag order is chosen by minimising an information criterion built from the ML residual covariance \hat{\Sigma}_p = E^\top E / T:
Multi-step forecasts iterate the companion form: the h-step forecast uses previous forecasts in place of the unobserved future values, which is the conditional mean E[y_{n+h} \mid y_{1:n}].
Parameters
lags
"auto" selects the order over 1..maxlags by the criterion given in ic.
maxlags
None it is min(10, (n_timepoints - 1) // (n_series + 1)), floored at 1.
ic
lags="auto".
trend
"c" includes an intercept, "n" omits it.
Attributes
coefs_
coefs_[i - 1] is :math:`A_i`.
intercept_
trend="n".
lags_
n_series_
n_obs_
sigma_
ic_values_
lags="auto".
endog_
input_was_1d_
fit received a 1-D series, in which case predict returns a 1-D forecast.
fitted_values_
resid_
Notes
Complexity:
- Training: O(T d^2 + d^3) with d = pk + 1 regressors
- Prediction: O(h p k^2).
- Several series that plausibly drive one another (demand and price,
- When you want forecasts for the whole panel jointly rather than one
- Requires roughly stationary series -- difference or detrend first if
- Parameters grow as p k^2, so keep p small on wide
References
See Also
>>> import numpy as np
>>> from tuiml.algorithms.timeseries.var import VAR
>>> rng = np.random.default_rng(0)
>>> n, y = 300, np.zeros((300, 2))
>>> A = np.array([[0.5, 0.1], [-0.2, 0.3]])
>>> for t in range(1, n):
... y[t] = A @ y[t - 1] + rng.normal(size=2)
>>> model = VAR(lags=1).fit(y)
>>> model.coefs_.shape
(1, 2, 2)
>>> model.predict(steps=4).shape
(4, 2)
Methods
fit
(self, y: np.ndarray, X: Optional[np.ndarray]=None) -> 'VAR'
fit
(self, y: np.ndarray, X: Optional[np.ndarray]=None) -> 'VAR'
Fit the VAR by equation-by-equation OLS on stacked lags.
Parameters
y
predict then returns a 1-D forecast.
X
Returns
self
predict
(self, steps: int=1, X: Optional[np.ndarray]=None) -> np.ndarray
predict
(self, steps: int=1, X: Optional[np.ndarray]=None) -> np.ndarray
Forecast the panel forward by iterating the companion form.
Parameters
steps
X
Returns
forecast
fit received a 1-D series.
fit_predict
(self, y: np.ndarray, steps: int=1) -> np.ndarray
fit_predict
(self, y: np.ndarray, steps: int=1) -> np.ndarray
Fit the model and forecast in one call.
Parameters
y
steps
Returns
forecast