%% Plot Generalized IRF of VARMA Model
% Plot the entire IRF of the structural vector autoregression moving average model (VARMA(8,4))
% \begin{array}{*{20}{l}}\left\{ {\left[ {\begin{array}{*{20}{c}}
% 1&{0.2}&{ - 0.1}\\ {0.03}&1&{ - 0.15}\\ {0.9}&{ - 0.25}&1 \end{array}}
% \right] - \left[ {\begin{array}{*{20}{c}} { - 0.5}&{0.2}&{0.1}\\
% {0.3}&{0.1}&{ - 0.1}\\ { - 0.4}&{0.2}&{0.05} \end{array}} \right]{L^4} -
% \left[ {\begin{array}{*{20}{c}} { - 0.05}&{0.02}&{0.01}\\
% {0.1}&{0.01}&{0.001}\\ { - 0.04}&{0.02}&{0.005} \end{array}}
% \right]{L^8}} \right\}{y_t} =\\ \left\{ {\left[ {\begin{array}{*{20}{c}}
% { 1}&{0}&{0}\\ {0}&{1}&{0}\\ {0}&{0}&{1} \end{array}} \right] + \left[
% {\begin{array}{*{20}{c}} { - 0.02}&{ 0.03}&{0.3}\\
% {0.003}&{0.001}&{0.01}\\ { 0.3}&{0.01}&{0.01} \end{array}} \right]{L^4}}
% \right\}{\varepsilon _t}\end{array}
% where y_t = \left[y_{1t}\;\;\;y_{2t}\;\;\;y_{3t}\right]' and \varepsilon_t = \left[\varepsilon_{1t}\;\;\;\varepsilon_{2t}\;\;\;\varepsilon_{3t}\right]'.
% The VARMA model is in lag operator notation because the response and innovation vectors are on opposite sides of the equation.
% Create a cell vector containing the VAR matrix coefficients. Because this model is a structural model in lag operator notation, start with the coefficient of y_t and enter the rest in order by lag. Construct a vector that indicates the degree of the lag term for the corresponding coefficients (the structural-coefficient lag is 0).

var0 = {[1 0.2 -0.1; 0.03 1 -0.15; 0.9 -0.25 1],...
    -[-0.5 0.2 0.1; 0.3 0.1 -0.1; -0.4 0.2 0.05],...
    -[-0.05 0.02 0.01; 0.1 0.01 0.001; -0.04 0.02 0.005]};
var0Lags = [0 4 8];

%
% Create a cell vector containing the VMA matrix coefficients. Because this model is in lag operator notation, start with the coefficient of \varepsilon_t and enter the rest in order by lag. Construct a vector that indicates the degree of the lag term for the corresponding coefficients.

vma0 = {eye(3),...
    [-0.02 0.03 0.3; 0.003 0.001 0.01; 0.3 0.01 0.01]};
vma0Lags = [0 4];

%
% Construct separate lag operator polynomials that describe the VAR and VMA components of the VARMA model.

VARLag = LagOp(var0,'Lags',var0Lags);
VMALag = LagOp(vma0,'Lags',vma0Lags);

%
% Plot the generalized IRF of the VARMA model.

figure;
armairf(VARLag,VMALag,'Method','generalized');

% armairf returns three figures. Figure k contains the generalized IRF of variable k to a shock applied to all other variables at time 0. Because all IRFs fade after a finite number of periods, the VARMA model is stable.
% Copyright 2018 The MathWorks, Inc.