%% Estimate and Plot VAR Model IRF
% Fit a 4-D VAR(2) model to Danish money and income rate series.  Then, estimate and plot the orthogonalized IRF from the estimated model.
% Load the Danish money and income data set.

load Data_JDanish

% The data set includes four time series in the table DataTable.  For more details on the data set, enter Description at the command line.
%
% Assuming that the series are stationary, create a varm model object that represents a 4-D VAR(2) model. Specify the variable names.

Mdl = varm(4,2);
Mdl.SeriesNames = DataTable.Properties.VariableNames;

% Mdl is a varm model object specifying the structure of a 4-D VAR(2) model; it is a template for estimation.
%
% Fit the VAR(2) model to the data set.

Mdl = estimate(Mdl,DataTable.Series);

% Mdl is a fully specified varm model object representing an estimated 4-D VAR(2) model.
%
% Estimate the orthogonalized IRF from the estimated VAR(2) model.

Response = irf(Mdl);

% Response is a 20-by-4-by-4 array representing the IRF of Mdl. Rows correspond to consecutive time points from time 0 to 19, columns correspond to variables receiving a one-standard-deviation innovation shock at time 0, and pages correspond to responses of variables to the variable being shocked. Mdl.SeriesNames specifies the variable order.
% Display the IRF of the bond rate (variable 3, IB) when the log of real income (variable 2, Y) is shocked at time 0.

Response(:,2,3)

%
% Plot the IRFs of all series on separate plots by passing the estimated AR coefficient matrices and innovations covariance matrix of Mdl to armairf.

armairf(Mdl.AR,[],"InnovCov",Mdl.Covariance);

% Each plot shows the four IRFs of a variable when all other variables are shocked at time 0. Mdl.SeriesNames specifies the variable order.
% Copyright 2018 The MathWorks, Inc.