VAR
Vector Autoregressive model is a multivariate extension of the univariate autoregressive (AR) model. It captures the linear interdependencies between multiple variables using a system of equations. Each variable depends not only on its own lagged values but also on the lagged values of other variables.
The equation below shows the VAR(p) model for ‘k’ variables. ‘p’ denotes the number of AR terms to be included in the model.
We use the implementation in statsmodels and re-write the API to adapt Kats development style.
API
# Parameter class
class VARParams(**kwargs)
Parameters:
maxlags: Maximum number of lags to check for order selection,
Defaults to 12 * (nobs/100.)**(1./4)
method: Estimation method to use
Defaults to OLS
ic: Information criterion to use for VAR order selection
Defaults to None
trend: “c” - add constant (Default),
“ct” - constant and trend,
“ctt” - constant, linear and quadratic trend,
“n”/“nc” - no constant, no trend
# Model class
class VARModel(data, params)
Methods
fit(): # fit Theta model with given parameters
predict(steps, freq, alpha): # predict for future steps
Args:
steps: Number of time steps to forecast
freq: optional, frequency of timeseries data.
Defaults to automatically inferring from time index.
alpha: optional, significance level of condifence interval.
Defaults to 0.05
plot(): # plot the timeseries data with confidence interval (if exist)
Example
import pandas as pd
from infrastrategy.kats.consts import TimeSeriesData
from infrastrategy.kats.models.var import VARModel, VARParams
# read data and rename the two columns required by TimeSeriesData structure
DATA_multi = pd.read_csv("../data/multi_ts.csv")
TSData_multi = TimeSeriesData(DATA_multi)
# create VARParam with specifying seasonality param value
params = VARParams()
# create VARModel with given data and parameter class
m = VARModel(data=TSData_multi, params=params)
# call fit method to fit model
m.fit()
# call predict method to predict the next 15 steps
res = m.predict(steps=30)
# visualize the results
m.plot()