Main Content

VAR Model Case Study

This example shows how to analyze a VAR model.

案例研究概述

This section contains an example of the workflow described inVAR Model Workflow。该示例使用三个时间序列:GDP,M1货币供应和3个月的T-BILL率。示例显示:

  • 加载和转换平稳性数据

  • 将转换的数据划分为预先样本,估计和预测间隔,以支持进行回测实验金宝app

  • Making several models

  • 将模型拟合到数据

  • Deciding which of the models is best

  • 根据最佳模型进行预测

加载和转换数据

这filedata_useconmodel带有计量经济学工具箱™软件的船舶。该文件包含来自圣路易斯经济学数据(FRED)数据库中的时间序列。此示例使用了三个时间序列:

  • GDP (GDP

  • M1货币供应(M1SL

  • 3-month T-bill rate (TB3MS

加载数据集。为真实GDP创建变量。

加载data_useconmodeldatatable.rgdp = datatable.gdp./datatable.gdpdef*100;

Plot the data to look for trends.

figure subplot(3,1,1) plot(DataTable.Time,DataTable.RGDP,'r');标题('Real GDP')gridonsubplot(3,1,2); plot(DataTable.Time,DataTable.M1SL,'b');标题('M1')gridon子图(3,1,3);绘图(datatable.time,datatable.tb3ms,'k')标题('3-mo t-bill')gridon

这real GDP and M1 data appear to grow exponentially, while the T-bill returns show no exponential growth. To counter the trends in real GDP and M1, take a difference of the logarithms of the data. Also, stabilize the T-bill series by taking the first difference. Synchronize the date series so that the data has the same number of rows for each column.

RGDPG = PRICE2RET(DATATABLE.RGDP);m1slg = Price2Ret(datatable.m1sl);dtb3ms = diff(datatable.tb3ms);data = array2timetable([rgdpg m1slg dtb3ms],。。。'RowTimes',,,,数据Table.Time(2:end),'VariableNames',,,,{'RGDP''m1sl''TB3MS'});figure subplot(3,1,1) plot(Data.Time,Data.RGDP,'r');标题('Real GDP')gridonsubplot(3,1,2); plot(Data.Time,Data.M1SL,'b');标题('M1')gridon子图(3,1,3);plot(Data.Time,Data.TB3MS,'k'), 标题('3-mo t-bill')gridon

这scale of the first two columns is about 100 times smaller than the third. Multiply the first two columns by 100 so that the time series are all roughly on the same scale. This scaling makes it easy to plot all the series on the same plot. More importantly, this type of scaling makes optimizations more numerically stable (for example, maximizing loglikelihoods).

数据{:,1:2} = 100*Data{:,1:2}; figure plot(Data.Time,Data.RGDP,'r');抓住on绘图(data.time,dation.m1sl,'b');datetick('X')gridonplot(Data.Time,Data.TB3MS,'k');传奇('Real GDP',,,,'M1',,,,'3-mo t-bill');抓住离开

Select and Fit the Models

You can select many different models for the data. This example uses four models.

  • VAR(2)具有对角度自回归

  • VAR(2) with full autoregressive

  • VAR(4) with diagonal autoregressive

  • VAR(4) with full autoregressive

从系列的开头删除所有缺失值。

idx = all(〜smissing(data),2);data = data(idx,:);

Create the four models.

numseries = 3; dnan = diag(nan(numseries,1)); seriesnames = {'Real GDP',,,,'M1',,,,'3-mo t-bill'}; VAR2diag = varm('ar',{dnan dnan},“系列名称”,系列名称);VAR2full = varm(numseries,2); VAR2full.SeriesNames = seriesnames; VAR4diag = varm('ar',,,,{dnan dnan dnan dnan},“系列名称”,系列名称);var4full = varm(numseries,4);var4full.seriesnames =系列名称;

这matrixdnan是一个对角线矩阵values along its main diagonal. In general, missing values specify the presence of the parameter in the model, and indicate that the parameter needs to be fit to data. MATLAB® holds the off diagonal elements,0,,,,fixed during estimation. In contrast, the specifications forVAR2fullandvar4full具有由values. Therefore,estimatefits full matrices for autoregressive matrices.

To assess the quality of the models, create index vectors that divide the response data into three periods: presample, estimation, and forecast. Fit the models to the estimation data, using the presample period to provide lagged data. Compare the predictions of the fitted models to the forecast data. The estimation period is in sample, and the forecast period is out of sample (also known asbacktesting)。

对于两个VAR(4)型号,前样品周期是数据。为VAR(2)模型使用相同的预样品周期,以便所有模型都适合相同的数据。这是模型拟合比较所必需的。对于这两种型号,预测期是最终的10%行数据。这estimation period for the models goes from row 5 to the 90% row. Define these data periods.

idxpre = 1:4;t = ceil(.9*size(data,1));idxest = 5:t;idxf =(t+1):size(数据,1);fh = numel(idxf);

现在,模型和时间序列存在,您可以轻松地将模型拟合到数据。

[estmdl1,estse1,logl1,e1] = estimate(var2diag,data {idxest,:},,。。。'Y0',数据{idxpre,:});[EstMdl2,EstSE2,logL2,E2] = estimate(VAR2full,Data{idxEst,:},。。。'Y0',数据{idxpre,:});[estmdl3,estse3,logl3,e3] = estimate(var4diag,data {idxest,:},,。。。'Y0',数据{idxpre,:});[EstMdl4,EstSE4,logL4,E4] = estimate(VAR4full,Data{idxEst,:},。。。'Y0',数据{idxpre,:});
  • estMdlmodel objects are the fitted models.

  • estSE结构包含拟合模型的标准误差。

  • logLvalues are the loglikelihoods of the fitted models, which you use to help select the best model.

  • e向量是残差,其大小与估计数据相同。

检查模型充分性

You can check whether the estimated models are stable and invertible by displaying theDescription每个对象的属性。(这些模型中没有MA术语,因此模型一定是可逆的。)描述表明所有估计的模型都是稳定的。

estMdl1.Description EstMdl2.Description EstMdl3.Description EstMdl4.Description

AR-Stationaryappears in the output indicating that the autoregressive processes are stable.

You can compare the restricted (diagonal) AR models to their unrestricted (full) counterparts usinglratiotest。该测试拒绝或未能拒绝以下假设,即限制模型是足够的,默认的5%公差。这是样本中的测试。

应用似然比测试。您必须从返回的摘要结构中提取估计参数的数量summarize。这n, pass the differences in the number of estimated parameters and the loglikelihoods tolratiotestto perform the tests.

其中回答=总结(EstMdl1);np1 =其中回答。NumEstimatedParameters; results2 = summarize(EstMdl2); np2 = results2.NumEstimatedParameters; results3 = summarize(EstMdl3); np3 = results3.NumEstimatedParameters; results4 = summarize(EstMdl4); np4 = results4.NumEstimatedParameters; reject1 = lratiotest(logL2,logL1,np2 - np1) reject3 = lratiotest(logL4,logL3,np4 - np3) reject4 = lratiotest(logL4,logL2,np4 - np2)

1results indicate that the likelihood ratio test rejected both the restricted models in favor of the corresponding unrestricted models. Therefore, based on this test, the unrestricted VAR(2) and VAR(4) models are preferable. However, the test does not reject the unrestricted VAR(2) model in favor of the unrestricted VAR(4) model. (This test regards the VAR(2) model as an VAR(4) model with restrictions that the autoregression matrices AR(3) and AR(4) are 0.) Therefore, it seems that the unrestricted VAR(2) model is the best model.

To find the best model in a set, minimize the Akaike information criterion (AIC). Use in-sample data to compute the AIC. Calculate the criterion for the four models.

AIC = AICBIC([[Logl1 Logl2 Logl3 Logl4],[NP1 NP2 NP3 NP4])

这best model according to this criterion is the unrestricted VAR(2) model. Notice, too, that the unrestricted VAR(4) model has lower Akaike information than either of the restricted models. Based on this criterion, the unrestricted VAR(2) model is best, with the unrestricted VAR(4) model coming next in preference.

To compare the predictions of the four models against the forecast data, use为了ecast。此函数既返回平均时间序列的预测,又返回一个误差协方差矩阵,该矩阵给出了均值间隔。这是一个样本外计算。

[FY1,FYCov1] = forecast(EstMdl1,fh,Data{idxEst,:}); [FY2,FYCov2] = forecast(EstMdl2,fh,Data{idxEst,:}); [FY3,FYCov3] = forecast(EstMdl3,fh,Data{idxEst,:}); [FY4,FYCov4] = forecast(EstMdl4,fh,Data{idxEst,:});

estimate approximate 95% forecast intervals for the best fitting model.

extractMSE = @(x)diag(x)'; MSE = cellfun(extractMSE,FYCov2,'UniformOutput',错误的);SE = SQRT(Cell2Mat(MSE));yfi = zeros(fh,estmdl2.numseries,2);yfi(::,:,1)= fy2-2*se;yfi(::,:,2)= fy2 + 2*se;

This plot shows the predictions of the best fitting model in the shaded region to the right.

数字;为了j = 1:EstMdl2.NumSeries subplot(3,1,j); h1 = plot(Data.Time((end-49):end),Data{(end-49):end,j}); holdon;h2 = plot(Data.Time(idxF),FY2(:,j)); h3 = plot(Data.Time(idxF),YFI(:,j,1),'K--');plot(Data.Time(idxF),YFI(:,j,2),'K--');标题(estMdl2.SeriesNames{j}); h = gca; fill([Data.Time(idxF(1)) h.XLim([2 2]) Data.Time(idxF(1))],。。。h.YLim([1 1 2 2]),'k',,,,'FaceAlpha',0.1,'edgeColor',,,,'没有任何');传奇([H1 H2 H3],'True',,,,'预报',,,,'95% Forecast interval',,,,。。。'Location',,,,'northwest')抓住离开;结尾

It is now straightforward to calculate the sum-of-squares error between the predictions and the data.

error1 = Data{idxF,:} - FY1; Error2 = Data{idxF,:} - FY2; Error3 = Data{idxF,:} - FY3; Error4 = Data{idxF,:} - FY4; SSerror1 = Error1(:)' * Error1(:); SSerror2 = Error2(:)' * Error2(:); SSerror3 = Error3(:)' * Error3(:); SSerror4 = Error4(:)' * Error4(:); figure bar([SSerror1 SSerror2 SSerror3 SSerror4],.5) ylabel(“平方错误”)(GCA,'XTickLabel',,,,。。。{'ar2 diag''ar2 full''ar4 diag''AR4 full'}) 标题('Sum of Squared Forecast Errors'

这predictive performances of the four models are similar.

完整的AR(2)模型似乎是最好和最简约的合适。它的模型参数如下。

summarize(EstMdl2)

Forecast Observations

您可以使用拟合模型进行预测或预测(estMdl2)either by:

  • Calling为了ecastand passing the last few rows ofYF

  • 与几个时间序列模拟simulate

在这两种情况下,都会改变预测,使其与原始时间序列直接相当。

从最新时间开始使用拟合模型产生10个预测为了ecast

[YPred,YCov] = forecast(EstMdl2,10,Data{idxF,:});

Transform the predictions by undoing the scaling and differencing applied to the original data. Make sure to insert the last observation at the beginning of the time series before usingcumsumto undo the differencing. And, since differencing occurred after taking logarithms, insert the logarithm before usingcumsum

yfirst = datatable(idx,{'RGDP''m1sl''TB3MS'});endpt = yfirst {end,:};endpt(:,1:2)= log(endpt(:,1:2));ypred(::,1:2)= ypred(:,1:2)/100;% Rescale percentageYPred = [EndPt; YPred];%准备暨YPred(:,1:3) = cumsum(YPred(:,1:3)); YPred(:,1:2) = exp(YPred(:,1:2)); fdates = dateshift(YFirst.Time(end),'结尾',,,,'quarter',,,,0:10);%插入预测范围figure为了j = 1:estmdl2.numseries子图(3,1,j)图(fdates,ypred(::,j),'-b')抓住on绘图(yfirst.time,yfirst {:,j},'k')gridon标题(estMdl2.SeriesNames{j}) h = gca; fill([fdates(1) h.XLim([2 2]) fdates(1)],h.YLim([1 1 2 2]),'k',,,,。。。'FaceAlpha',0.1,'edgeColor',,,,'没有任何');抓住离开结尾

情节展示推断为蓝色虚线lines in the light gray forecast horizon, and the original data series in solid black.

查看本图中的最后几年,以了解预测与最新数据点的关系。

YLast = YFirst(170:end,:); figure为了j = 1:estmdl2.numseries子图(3,1,j)图(fdates,ypred(::,j),'b--')抓住onplot(YLast.Time,YLast{:,j},'k')gridon标题(estMdl2.SeriesNames{j}) h = gca; fill([fdates(1) h.XLim([2 2]) fdates(1)],h.YLim([1 1 2 2]),'k',,,,。。。'FaceAlpha',0.1,'edgeColor',,,,'没有任何');抓住离开结尾

预测显示了实际GDP和M1的增加,利率略有下降。但是,预测没有错误条。

Alternatively, you can generate 10 predictions from the fitted model beginning at the latest times usingsimulate。该方法模拟了2000个时间序列时间,然后生成每个时期的均值和标准偏差。每个时期偏差的平均值是该时期的预测。

Simulate a time series from the fitted model beginning at the latest times.

RNG(1);% For reproducibilityysim =模拟(estmdl2,10,'Y0',数据{idxf,:},'NumPaths',,,,2000);

Transform the predictions by undoing the scaling and differencing applied to the original data. Make sure to insert the last observation at the beginning of the time series before usingcumsumto undo the differencing. And, since differencing occurred after taking logarithms, insert the logarithm before usingcumsum

endpt = yfirst {end,:};endPt(1:2) = log(EndPt(1:2)); YSim(:,1:2,:) = YSim(:,1:2,:)/100; YSim = [repmat(EndPt,[1,1,2000]);YSim]; YSim(:,1:3,:) = cumsum(YSim(:,1:3,:)); YSim(:,1:2,:) = exp(YSim(:,1:2,:));

计算每个系列的平均值和标准偏差,并绘制结果。该图的平均值为黑色,红色为+/- 1标准偏差。

YMean = mean(YSim,3); YSTD = std(YSim,0,3); figure为了j = 1:estmdl2.numseries子图(3,1,j)图(fdates,ymean(::,j),'k')gridon抓住on绘图(ylast.time(end-10:end),ylast {end-10:end,j},'k')plot(fdates,YMean(:,j) + YSTD(:,j),'--r')plot(fdates,YMean(:,j) - YSTD(:,j),'--r')标题(estMdl2.SeriesNames{j}) h = gca; fill([fdates(1) h.XLim([2 2]) fdates(1)],h.YLim([1 1 2 2]),'k',,,,。。。'FaceAlpha',0.1,'edgeColor',,,,'没有任何');抓住离开结尾

这些地块显示GDP的增长增加,M1的中度至少量增长以及对BILL速率方向的不确定性。

也可以看看

对象

职能

Related Topics