VARMAX models#
This is a brief introduction notebook to VARMAX models in statsmodels. The VARMAX model is generically specified as:
where \(y_t\) is a \(\mathrm{k_endog} \times 1\) vector.
[1]:
%matplotlib inline
[2]:
import pandas as pd
import statsmodels.api as sm
[3]:
import shutil
import requests
def download_file(url):
local_filename = url.split("/")[-1]
with requests.get(url, stream=True, timeout=30) as r:
with open(local_filename, "wb") as f:
shutil.copyfileobj(r.raw, f)
return local_filename
filename = download_file("https://www.stata-press.com/data/r12/lutkepohl2.dta")
dta = pd.read_stata(filename)
dta.index = dta.qtr
dta.index.freq = "QS"
endog = dta.loc["1960-04-01":"1978-10-01", ["dln_inv", "dln_inc", "dln_consump"]]
Model specification#
The VARMAX class in statsmodels allows estimation of VAR, VMA, and VARMA models (through the order argument), optionally with a constant term (via the trend argument). Exogenous regressors may also be included (as usual in statsmodels, by the exog argument), and in this way a time trend may be added. Finally, the class allows measurement error (via the measurement_error argument) and allows specifying either a diagonal or unstructured innovation covariance matrix (via the
error_cov_type argument).
Example 1: VAR#
Below is a simple VARX(2) model in two endogenous variables and an exogenous series, but no constant term. Notice that we needed to allow for more iterations than the default (which is maxiter=50) in order for the likelihood estimation to converge. This is not unusual in VAR models which have to estimate a large number of parameters, often on a relatively small number of time series: this model, for example, estimates 27 parameters off of 75 observations of 3 variables.
[4]:
exog = endog["dln_consump"]
mod = sm.tsa.VARMAX(endog[["dln_inv", "dln_inc"]], order=(2, 0), trend="n", exog=exog)
res = mod.fit(maxiter=1000, disp=False)
print(res.summary())
/opt/hostedtoolcache/Python/3.14.10/x64/lib/python3.14/site-packages/statsmodels/tsa/statespace/mlemodel.py:737: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
mlefit = super().fit(
Statespace Model Results
==================================================================================
Dep. Variable: ['dln_inv', 'dln_inc'] No. Observations: 75
Model: VARX(2) Log Likelihood 361.039
Date: Wed, 11 Nov 2026 AIC -696.078
Time: 19:07:58 BIC -665.950
Sample: 04-01-1960 HQIC -684.048
- 10-01-1978
Covariance Type: opg
===================================================================================
Ljung-Box (L1) (Q): 0.05, 10.13 Jarque-Bera (JB): 11.22, 2.41
Prob(Q): 0.83, 0.00 Prob(JB): 0.00, 0.30
Heteroskedasticity (H): 0.45, 0.40 Skew: 0.16, -0.38
Prob(H) (two-sided): 0.05, 0.03 Kurtosis: 4.87, 3.44
Results for equation dln_inv
====================================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------------
L1.dln_inv -0.2400 0.093 -2.582 0.010 -0.422 -0.058
L1.dln_inc 0.2843 0.449 0.634 0.526 -0.595 1.163
L2.dln_inv -0.1641 0.155 -1.056 0.291 -0.469 0.140
L2.dln_inc 0.0752 0.421 0.178 0.858 -0.751 0.901
beta.dln_consump 0.9633 0.638 1.510 0.131 -0.287 2.213
Results for equation dln_inc
====================================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------------
L1.dln_inv 0.0630 0.036 1.760 0.078 -0.007 0.133
L1.dln_inc 0.0829 0.107 0.774 0.439 -0.127 0.293
L2.dln_inv 0.0100 0.033 0.304 0.761 -0.055 0.075
L2.dln_inc 0.0341 0.134 0.254 0.799 -0.229 0.297
beta.dln_consump 0.7722 0.112 6.873 0.000 0.552 0.992
Error covariance matrix
============================================================================================
coef std err z P>|z| [0.025 0.975]
--------------------------------------------------------------------------------------------
sqrt.var.dln_inv 0.0433 0.004 12.300 0.000 0.036 0.050
sqrt.cov.dln_inv.dln_inc 4.82e-05 0.002 0.024 0.981 -0.004 0.004
sqrt.var.dln_inc 0.0109 0.001 11.213 0.000 0.009 0.013
============================================================================================
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
From the estimated VAR model, we can plot the impulse response functions of the endogenous variables.
[5]:
ax = res.impulse_responses(10, orthogonalized=True, impulse=[1, 0]).plot(
figsize=(13, 3)
)
ax.set(xlabel="t", title="Responses to a shock to `dln_inv`");
Example 2: VMA#
A vector moving average model can also be formulated. Below we show a VMA(2) on the same data, but where the innovations to the process are uncorrelated. In this example we leave out the exogenous regressor but now include the constant term.
[6]:
mod = sm.tsa.VARMAX(
endog[["dln_inv", "dln_inc"]], order=(0, 2), error_cov_type="diagonal"
)
res = mod.fit(maxiter=1000, disp=False)
print(res.summary())
Statespace Model Results
==================================================================================
Dep. Variable: ['dln_inv', 'dln_inc'] No. Observations: 75
Model: VMA(2) Log Likelihood 353.888
+ intercept AIC -683.776
Date: Wed, 11 Nov 2026 BIC -655.966
Time: 19:16:17 HQIC -672.672
Sample: 04-01-1960
- 10-01-1978
Covariance Type: opg
===================================================================================
Ljung-Box (L1) (Q): 0.00, 0.05 Jarque-Bera (JB): 12.69, 13.65
Prob(Q): 0.95, 0.82 Prob(JB): 0.00, 0.00
Heteroskedasticity (H): 0.44, 0.81 Skew: 0.06, -0.48
Prob(H) (two-sided): 0.04, 0.60 Kurtosis: 5.01, 4.85
Results for equation dln_inv
=================================================================================
coef std err z P>|z| [0.025 0.975]
---------------------------------------------------------------------------------
intercept 0.0182 0.005 3.799 0.000 0.009 0.028
L1.e(dln_inv) -0.2591 0.106 -2.450 0.014 -0.466 -0.052
L1.e(dln_inc) 0.5166 0.631 0.819 0.413 -0.720 1.753
L2.e(dln_inv) 0.0309 0.149 0.207 0.836 -0.261 0.323
L2.e(dln_inc) 0.1903 0.477 0.399 0.690 -0.744 1.125
Results for equation dln_inc
=================================================================================
coef std err z P>|z| [0.025 0.975]
---------------------------------------------------------------------------------
intercept 0.0207 0.002 13.054 0.000 0.018 0.024
L1.e(dln_inv) 0.0479 0.042 1.151 0.250 -0.034 0.129
L1.e(dln_inc) -0.0741 0.140 -0.530 0.596 -0.348 0.200
L2.e(dln_inv) 0.0179 0.042 0.422 0.673 -0.065 0.101
L2.e(dln_inc) 0.1254 0.153 0.819 0.413 -0.175 0.425
Error covariance matrix
==================================================================================
coef std err z P>|z| [0.025 0.975]
----------------------------------------------------------------------------------
sigma2.dln_inv 0.0020 0.000 7.347 0.000 0.001 0.003
sigma2.dln_inc 0.0001 2.33e-05 5.831 0.000 9.01e-05 0.000
==================================================================================
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
Caution: VARMA(p,q) specifications#
Although the model allows estimating VARMA(p,q) specifications, these models are not identified without additional restrictions on the representation matrices, which are not built-in. For this reason, it is recommended that the user proceed with error (and indeed a warning is issued when these models are specified). Nonetheless, they may in some circumstances provide useful information.
[7]:
mod = sm.tsa.VARMAX(endog[["dln_inv", "dln_inc"]], order=(1, 1))
res = mod.fit(maxiter=1000, disp=False)
print(res.summary())
/tmp/ipykernel_4355/242493548.py:1: EstimationWarning: Estimation of VARMA(p,q) models is not generically robust, due especially to identification issues.
mod = sm.tsa.VARMAX(endog[["dln_inv", "dln_inc"]], order=(1, 1))
Statespace Model Results
==================================================================================
Dep. Variable: ['dln_inv', 'dln_inc'] No. Observations: 75
Model: VARMA(1,1) Log Likelihood 354.283
+ intercept AIC -682.566
Date: Wed, 11 Nov 2026 BIC -652.438
Time: 19:17:24 HQIC -670.536
Sample: 04-01-1960
- 10-01-1978
Covariance Type: opg
===================================================================================
Ljung-Box (L1) (Q): 0.02, 0.06 Jarque-Bera (JB): 10.77, 14.08
Prob(Q): 0.89, 0.81 Prob(JB): 0.00, 0.00
Heteroskedasticity (H): 0.43, 0.91 Skew: 0.00, -0.46
Prob(H) (two-sided): 0.04, 0.81 Kurtosis: 4.86, 4.92
Results for equation dln_inv
=================================================================================
coef std err z P>|z| [0.025 0.975]
---------------------------------------------------------------------------------
intercept 0.0110 0.068 0.162 0.871 -0.123 0.145
L1.dln_inv -0.0106 0.721 -0.015 0.988 -1.424 1.403
L1.dln_inc 0.3596 2.854 0.126 0.900 -5.235 5.954
L1.e(dln_inv) -0.2492 0.733 -0.340 0.734 -1.685 1.187
L1.e(dln_inc) 0.1267 3.096 0.041 0.967 -5.942 6.196
Results for equation dln_inc
=================================================================================
coef std err z P>|z| [0.025 0.975]
---------------------------------------------------------------------------------
intercept 0.0166 0.029 0.579 0.562 -0.039 0.073
L1.dln_inv -0.0328 0.287 -0.114 0.909 -0.595 0.529
L1.dln_inc 0.2299 1.163 0.198 0.843 -2.049 2.508
L1.e(dln_inv) 0.0883 0.293 0.301 0.764 -0.487 0.663
L1.e(dln_inc) -0.2340 1.195 -0.196 0.845 -2.576 2.108
Error covariance matrix
============================================================================================
coef std err z P>|z| [0.025 0.975]
--------------------------------------------------------------------------------------------
sqrt.var.dln_inv 0.0450 0.003 14.524 0.000 0.039 0.051
sqrt.cov.dln_inv.dln_inc 0.0017 0.003 0.646 0.518 -0.003 0.007
sqrt.var.dln_inc 0.0116 0.001 11.662 0.000 0.010 0.013
============================================================================================
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).