VAR模型

简介

向量自回归(VARs)于20世纪80年代被引入计量经济学文献,用于描述多个变量之间的(线性)依赖关系。对于一个\(K\) x \(1\)的向量\(y_{t}\),我们可以将VAR(p)模型定义为:

\[y_{t} = c + A_{1}y_{t-1} + ... + A_{p}y_{t-p} + e_{t}\]

这些模型可以通过OLS快速估计。但当因变量数量较多时,待估参数的数量会急剧增加。关于处理这类模型的替代方法,请参阅贝叶斯向量自回归的笔记本。

示例

我们将为美国银行业股票运行一个VAR模型。

import numpy as np
import pyflux as pf
from pandas_datareader import DataReader
from datetime import datetime
import matplotlib.pyplot as plt
%matplotlib inline

ibm = DataReader(['JPM','GS','BAC','C','WFC','MS'],  'yahoo', datetime(2012,1,1), datetime(2016,6,28))
opening_prices = np.log(ibm['Open'])
plt.figure(figsize=(15,5));
plt.plot(opening_prices.index,opening_prices);
plt.legend(opening_prices.columns.values,loc=3);
plt.title("Logged opening price");
http://www.pyflux.com/notebooks/VAR/output_7_1.png

这里我们指定一个任意的VAR(2)模型,通过OLS方法进行拟合:

model = pf.VAR(data=opening_prices, lags=2, integ=1)

接下来我们估计潜在变量。对于这个例子,我们将使用OLS估计\(z^{OLS}\)

x = model.fit()
x.summary()

VAR(2)
======================================== =================================================
Dependent Variable: Differenced BAC      Method: OLS
Start Date: 2012-01-05 00:00:00          Log Likelihood: 21547.2578
End Date: 2016-06-28 00:00:00            AIC: -42896.5156
Number of observations: 1126             BIC: -42398.8994
==========================================================================================
Latent Variable           Estimate   Std Error  z        P>|z|    95% C.I.
========================= ========== ========== ======== ======== ========================
Diff BAC Constant         0.0007     0.0006     1.2007   0.2299   (-0.0004 | 0.0018)
Diff BAC AR(1)            -0.0525    0.0005     -97.5672 0.0      (-0.0535 | -0.0514)
Diff C to Diff BAC AR(1)  -0.0616    0.0004     -143.365 0.0      (-0.0625 | -0.0608)
Diff GS to Diff BAC AR(1) 0.0595     0.0004     132.6638 0.0      (0.0587 | 0.0604)
Diff JPM to Diff BAC AR(1)0.0296     0.0006     49.3563  0.0      (0.0284 | 0.0308)
Diff MS to Diff BAC AR(1) -0.0231    0.0004     -62.6218 0.0      (-0.0239 | -0.0224)
Diff WFC to Diff BAC AR(1)-0.0417    0.0598     -0.6968  0.4859   (-0.159 | 0.0756)
Diff BAC AR(2)            0.1171     0.0555     2.1087   0.035    (0.0083 | 0.226)
Diff C to Diff BAC AR(2)  -0.1266    0.0444     -2.8528  0.0043   (-0.2136 | -0.0396)
Diff GS to Diff BAC AR(2) 0.1698     0.0464     3.6618   0.0003   (0.0789 | 0.2606)
Diff JPM to Diff BAC AR(2)-0.0959    0.062      -1.5472  0.1218   (-0.2174 | 0.0256)
Diff MS to Diff BAC AR(2) -0.0213    0.0381     -0.557   0.5775   (-0.096 | 0.0535)
Diff WFC to Diff BAC AR(2)-0.001     0.0701     -0.0149  0.9881   (-0.1384 | 0.1363)
Diff C Constant           0.0003     0.065      0.0047   0.9962   (-0.1272 | 0.1278)
Diff C AR(1)              0.0193     0.052      0.3706   0.7109   (-0.0826 | 0.1211)
Diff BAC to Diff C AR(1)  -0.0576    0.0543     -1.0613  0.2886   (-0.164 | 0.0488)
Diff GS to Diff C AR(1)   0.0579     0.0726     0.7979   0.4249   (-0.0844 | 0.2002)
Diff JPM to Diff C AR(1)  0.0831     0.0447     1.8595   0.063    (-0.0045 | 0.1706)
Diff MS to Diff C AR(1)   -0.037     0.0794     -0.4657  0.6414   (-0.1925 | 0.1186)
Diff WFC to Diff C AR(1)  -0.1785    0.0737     -2.4235  0.0154   (-0.3229 | -0.0341)
Diff C AR(2)              0.1612     0.0589     2.7379   0.0062   (0.0458 | 0.2765)
Diff BAC to Diff C AR(2)  -0.1021    0.0615     -1.6598  0.0969   (-0.2226 | 0.0185)
Diff GS to Diff C AR(2)   0.1109     0.0822     1.3483   0.1776   (-0.0503 | 0.272)
Diff JPM to Diff C AR(2)  -0.0453    0.0506     -0.8946  0.371    (-0.1444 | 0.0539)
Diff MS to Diff C AR(2)   0.0127     0.0775     0.1643   0.8695   (-0.1391 | 0.1646)
Diff WFC to Diff C AR(2)  -0.1313    0.0719     -1.8261  0.0678   (-0.2723 | 0.0096)
Diff GS Constant          0.0003     0.0575     0.006    0.9952   (-0.1123 | 0.113)
Diff GS AR(1)             -0.016     0.06       -0.266   0.7903   (-0.1336 | 0.1017)
Diff BAC to Diff GS AR(1) 0.0051     0.0803     0.0633   0.9495   (-0.1523 | 0.1624)
Diff C to Diff GS AR(1)   -0.0785    0.0494     -1.5891  0.112    (-0.1753 | 0.0183)
Diff JPM to Diff GS AR(1) 0.0507     0.0575     0.8814   0.3781   (-0.062 | 0.1633)
Diff MS to Diff GS AR(1)  0.0425     0.0534     0.7961   0.4259   (-0.0621 | 0.1471)
Diff WFC to Diff GS AR(1) -0.0613    0.0426     -1.4376  0.1505   (-0.1449 | 0.0223)
Diff GS AR(2)             0.0865     0.0445     1.9422   0.0521   (-0.0008 | 0.1738)
Diff BAC to Diff GS AR(2) -0.1896    0.0596     -3.1832  0.0015   (-0.3064 | -0.0729)
Diff C to Diff GS AR(2)   0.0423     0.0367     1.1553   0.248    (-0.0295 | 0.1142)
Diff JPM to Diff GS AR(2) 0.0667     0.0769     0.8664   0.3863   (-0.0841 | 0.2174)
Diff MS to Diff GS AR(2)  0.0433     0.0714     0.6067   0.5441   (-0.0966 | 0.1833)
Diff WFC to Diff GS AR(2) -0.0362    0.0571     -0.6347  0.5256   (-0.1481 | 0.0756)
Diff JPM Constant         0.0005     0.0596     0.0082   0.9934   (-0.1163 | 0.1173)
Diff JPM AR(1)            -0.0304    0.0797     -0.3813  0.703    (-0.1866 | 0.1258)
Diff BAC to Diff JPM AR(1)-0.0281    0.049      -0.5738  0.5661   (-0.1243 | 0.068)
Diff C to Diff JPM AR(1)  0.0695     0.0594     1.1698   0.2421   (-0.047 | 0.186)
Diff GS to Diff JPM AR(1) -0.0106    0.0552     -0.1924  0.8474   (-0.1187 | 0.0975)
Diff MS to Diff JPM AR(1) -0.0338    0.0441     -0.7675  0.4428   (-0.1202 | 0.0526)
Diff WFC to Diff JPM AR(1)-0.0725    0.046      -1.5744  0.1154   (-0.1627 | 0.0178)
Diff JPM AR(2)            0.096      0.0616     1.559    0.119    (-0.0247 | 0.2167)
Diff BAC to Diff JPM AR(2)-0.1246    0.0379     -3.2883  0.001    (-0.1989 | -0.0503)
Diff C to Diff JPM AR(2)  0.0229     0.0696     0.3284   0.7426   (-0.1136 | 0.1593)
Diff GS to Diff JPM AR(2) -0.0084    0.0646     -0.1301  0.8965   (-0.1351 | 0.1182)
Diff MS to Diff JPM AR(2) 0.0319     0.0516     0.6182   0.5364   (-0.0693 | 0.1332)
Diff WFC to Diff JPM AR(2)-0.0117    0.0539     -0.2161  0.8289   (-0.1174 | 0.0941)
Diff MS Constant          0.0004     0.0721     0.005    0.996    (-0.141 | 0.1417)
Diff MS AR(1)             0.0249     0.0444     0.5605   0.5752   (-0.0621 | 0.1119)
Diff BAC to Diff MS AR(1) 0.0456     0.0783     0.5833   0.5597   (-0.1077 | 0.199)
Diff C to Diff MS AR(1)   0.0083     0.0726     0.1148   0.9086   (-0.134 | 0.1507)
Diff GS to Diff MS AR(1)  0.1319     0.0581     2.2717   0.0231   (0.0181 | 0.2457)
Diff JPM to Diff MS AR(1) -0.1771    0.0606     -2.9213  0.0035   (-0.296 | -0.0583)
Diff WFC to Diff MS AR(1) -0.151     0.0811     -1.8629  0.0625   (-0.31 | 0.0079)
Diff MS AR(2)             0.1512     0.0499     3.0308   0.0024   (0.0534 | 0.249)
Diff BAC to Diff MS AR(2) -0.2173    0.0772     -2.8157  0.0049   (-0.3686 | -0.066)
Diff C to Diff MS AR(2)   0.1827     0.0716     2.5499   0.0108   (0.0423 | 0.3231)
Diff GS to Diff MS AR(2)  -0.0107    0.0573     -0.1873  0.8514   (-0.1229 | 0.1015)
Diff JPM to Diff MS AR(2) 0.0004     0.0598     0.0066   0.9947   (-0.1168 | 0.1176)
Diff WFC to Diff MS AR(2) -0.0697    0.08       -0.8711  0.3837   (-0.2264 | 0.0871)
Diff WFC Constant         0.0005     0.0492     0.0095   0.9924   (-0.096 | 0.0969)
Diff WFC AR(1)            0.0092     0.0574     0.1611   0.872    (-0.1032 | 0.1217)
Diff BAC to Diff WFC AR(1)-0.0059    0.0532     -0.1113  0.9114   (-0.1103 | 0.0984)
Diff C to Diff WFC AR(1)  0.0062     0.0425     0.1448   0.8848   (-0.0772 | 0.0896)
Diff GS to Diff WFC AR(1) 0.0525     0.0444     1.1811   0.2376   (-0.0346 | 0.1396)
Diff JPM to Diff WFC AR(1)-0.0047    0.0594     -0.0792  0.9368   (-0.1212 | 0.1118)
Diff MS to Diff WFC AR(1) -0.1996    0.0366     -5.4578  0.0      (-0.2713 | -0.1279)
Diff WFC AR(2)            0.0291     0.0773     0.3759   0.707    (-0.1225 | 0.1806)
Diff BAC to Diff WFC AR(2)-0.0509    0.0718     -0.7087  0.4785   (-0.1915 | 0.0898)
Diff C to Diff WFC AR(2)  0.0255     0.0574     0.4444   0.6567   (-0.0869 | 0.1379)
Diff GS to Diff WFC AR(2) 0.0235     0.0599     0.3922   0.6949   (-0.0939 | 0.1409)
Diff JPM to Diff WFC AR(2)0.015      0.0801     0.1878   0.851    (-0.142 | 0.1721)
Diff MS to Diff WFC AR(2) -0.0556    0.0493     -1.1276  0.2595   (-0.1522 | 0.041)
==========================================================================================

我们可以使用plot_z()方法来绘制潜在变量:

model.plot_z(list(range(0,6)),figsize=(15,5))
http://www.pyflux.com/notebooks/VAR/output_11_0.png

我们可以使用plot_fit()绘制样本内拟合图:

model.plot_fit(figsize=(15,5))
http://www.pyflux.com/notebooks/VAR/output_13_0.png http://www.pyflux.com/notebooks/VAR/output_13_1.png http://www.pyflux.com/notebooks/VAR/output_13_2.png http://www.pyflux.com/notebooks/VAR/output_13_3.png http://www.pyflux.com/notebooks/VAR/output_13_4.png http://www.pyflux.com/notebooks/VAR/output_13_5.png

我们可以使用plot_predict()来用我们的模型进行前向预测:

model.plot_predict(past_values=19, h=5, figsize=(15,5))
http://www.pyflux.com/notebooks/VAR/output_15_0.png http://www.pyflux.com/notebooks/VAR/output_15_1.png http://www.pyflux.com/notebooks/VAR/output_15_2.png http://www.pyflux.com/notebooks/VAR/output_15_3.png http://www.pyflux.com/notebooks/VAR/output_15_4.png http://www.pyflux.com/notebooks/VAR/output_15_5.png

我们的模型表现如何?我们可以通过进行滚动样本内预测来了解——plot_predict_is():用于绘制图表:

model.plot_predict_is(h=30, figsize=((15,5)))
http://www.pyflux.com/notebooks/VAR/output_19_0.png http://www.pyflux.com/notebooks/VAR/output_19_1.png http://www.pyflux.com/notebooks/VAR/output_19_2.png http://www.pyflux.com/notebooks/VAR/output_19_3.png http://www.pyflux.com/notebooks/VAR/output_19_4.png http://www.pyflux.com/notebooks/VAR/output_19_5.png

类描述

class VAR(data, lags, integ, target, use_ols_covariance)

向量自回归模型(VAR)。

参数 类型 描述
data pd.DataFrame or np.ndarray 包含单变量时间序列
lags int 自回归滞后项的数量
integ int 数据差分次数 (默认: 0)
target string or int 指定使用DataFrame/array中的哪一列。
use_ols_covariance boolean 是否使用固定的OLS协方差

属性

latent_variables

一个包含模型潜在变量信息的pf.LatentVariables()对象,包括先验设置、任何拟合值、初始值和其他潜在变量信息。当模型被拟合时,这里就是潜在变量被更新/存储的地方。有关此对象内属性的信息以及访问潜在变量信息的方法,请参阅潜在变量文档。

方法

adjust_prior(index, prior)

调整模型潜在变量的先验分布。潜在变量及其索引可以通过打印附加到模型实例的latent_variables属性来查看。

参数 类型 描述
index int 要更改的潜变量索引
prior pf.Family instance Prior distribution, e.g. pf.Normal()

返回: void - 修改模型的 latent_variables 属性

fit(method, **kwargs)

估计模型的潜在变量。用户选择一个推断选项,该方法会返回一个结果对象,同时更新模型的latent_variables属性。

参数 类型 描述
method str 推断选项:例如 'M-H' 或 'MLE'

请参阅文档中的贝叶斯推断和经典推断部分,了解完整的推断选项列表。可以输入与所选特定推断模式相关的可选参数。

返回: 包含估计潜在变量信息的pf.Results实例

plot_fit(**kwargs)

绘制模型对数据的拟合情况。可选参数包括figsize,即绘图图形的尺寸。

返回 : void - 显示一个matplotlib绘图

plot_predict(h, past_values, intervals, **kwargs)

绘制模型的预测结果,并附带置信区间。

参数 类型 描述
h int 预测向前多少步
past_values int 要绘制的历史数据点数量
intervals boolean 是否绘制区间

可选参数包括figsize - 图表绘制的尺寸。请注意 如果您使用最大似然估计或变分推断,显示的区间将不会 反映潜在变量的不确定性。只有Metropolis-Hastings方法能提供完全贝叶斯 预测区间。由于平均场推断的局限性(无法考虑后验相关性), 变分推断的贝叶斯区间不予显示。

返回 : void - 显示一个matplotlib绘图

plot_predict_is(h, fit_once, fit_method, **kwargs)

绘制模型在样本内的滚动预测。这意味着用户假装数据的最后一部分是样本外的,并在每个时间段后进行预测并评估其表现。用户可以选择是在开始时一次性拟合参数,还是在每个时间步都进行拟合。

参数 类型 描述
h int 使用多少个先前的时间步
fit_once boolean 是否只拟合一次,还是每个时间步都拟合
fit_method str 选择哪种推断方法,例如'MLE'

可选参数包括figsize - 要绘制的图形尺寸。h是一个整数,表示要模拟性能的前几步。

返回 : void - 显示一个matplotlib绘图

plot_z(indices, figsize)

返回潜在变量及其相关不确定性的绘图。

参数 类型 描述
indices int or list 要绘制的潜变量索引
figsize tuple matplotlib图形的大小

返回 : void - 显示一个matplotlib绘图

predict(h)

返回模型预测结果的DataFrame。

参数 类型 描述
h int 预测向前多少步

返回 : pd.DataFrame - 模型预测结果

predict_is(h, fit_once, fit_method)

返回模型样本内滚动预测的DataFrame。

参数 类型 描述
h int 使用多少个先前的时间步
fit_once boolean 是否只拟合一次,还是每个时间步都拟合
fit_method str 选择哪种推断方法,例如'MLE'

返回 : pd.DataFrame - 模型预测结果

simulation_smoother(beta)

返回从Durbin和Koopman(2002)模拟平滑器中抽取的数据的np.ndarray数组。

参数 类型 描述
beta np.array 潜在变量的np.array数组

如果已经拟合了模型,建议直接使用model.latent_variables.get_z_values()作为beta输入。

返回 : np.ndarray - 来自模拟平滑器的样本

参考文献

Lütkepohl, H. 和 Kraetzig, M. (2004). 《应用时间序列计量经济学》. 剑桥大学出版社, 剑桥.