%% Return IRFs of VAR Model
% Compute the generalized IRF of the 2-D VAR(3) model
% {y_t} = \left[ {\begin{array}{*{20}{c}}
% {1}&{ - 0.2}\\
% { - 0.1}&{0.3}
% \end{array}} \right]{y_{t - 1}} - \left[ {\begin{array}{*{20}{c}}
% {0.75}&{ - 0.1}\\
% { - 0.05}&{0.15}
% \end{array}} \right]{y_{t - 2}} + \left[ {\begin{array}{*{20}{c}}
% {0.55}&{ - 0.02}\\
% { - 0.01}&{0.03}
% \end{array}} \right]{y_{t - 3}} + {\varepsilon _t}.
% In the equation, y_t = [y_{1,t}\;\;\;y_{2,t}]', \varepsilon_t = [\varepsilon_{1,t}\;\;\; \varepsilon_{2,t}]', and, for all t, \varepsilon_t is Gaussian with mean zero and covariance matrix
% \Sigma  = \left[ {\begin{array}{*{20}{c}}
% {0.5}&{ - 0.1}\\
% { - 0.1}&{0.25}
% \end{array}} \right].
% Create a cell vector of matrices for the autoregressive coefficients as you encounter them in the model as expressed in difference-equation notation. Specify the innovation covariance matrix.

AR1 = [1 -0.2; -0.1 0.3];
AR2 = -[0.75 -0.1; -0.05 0.15];
AR3 = [0.55 -0.02; -0.01 0.03];
ar0 = {AR1 AR2 AR3};

InnovCov = [0.5 -0.1; -0.1 0.25];

%
% Compute the entire generalized IRF of y_t. Because no MA terms exist, specify an empty array ([]) for the second input argument.

Y = armairf(ar0,[],'Method','generalized','InnovCov',InnovCov);
size(Y)
Y(10,1,2)

% Y is a 31-by-2-by-2 array of impulse responses. Rows correspond to times 0 through 30 in the forecast horizon, columns correspond to the variables that armairf shocks at time 0, and pages correspond to the impulse response of the variables in the system.  For example, the generalized impulse response of variable 2 at time 10 in the forecast horizon, when variable 1 is shocked at time 0, is Y(11,1,2) = -0.0116.
% armairf satisfies the stopping criterion after 31 periods. You can specify to stop sooner using the 'NumObs' name-value pair argument. This practice is beneficial when the system has many variables.
%
% Compute and display the generalized impulse responses for the first 10 periods.

Y10 = armairf(ar0,[],'Method','generalized','InnovCov',InnovCov,...
    'NumObs',10)

% Y10 is a 10-by-2-by-2 array of impulse responses.  Rows correspond to times 0 through 9 in the forecast horizon.
% The impulse responses appear to fade with increasing time, which suggests a stable system.
%
%  Copyright 2018 The MathWorks, Inc.