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

load Data_JDanish

% The data set includes four times series in the table DataTable.  For more details on the data set, enter Description at the command line.
%
% Create a vecm model object that represents a 4-D VEC(2) model with two cointegrating relations. Specify the variable names.

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

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

Mdl = estimate(Mdl,DataTable.Series);

% Mdl is a fully specified vecm model object representing an estimated 4-D VEC(2) model.
%
% Estimate the orthogonalized IRF from the estimated VEC(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)

%
% The armairf function plots the IRF of VAR models characterized by AR coefficient matrices.  Plot the IRF of a VEC model by:
% Expressing the VEC(2) model as a VAR(3) model by passing Mdl to varm
% Passing the VAR model AR coefficients and innovations covariance matrix to armairf
% Plot the VEC(2) model IRF for 40 periods.

VARMdl = varm(Mdl);
armairf(VARMdl.AR,[],"InnovCov",VARMdl.Covariance,...
    "NumObs",40);

% 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.
%