%% Plot Generalized FEVDs of Structural VARMA Model in Lag Operator Notation
% Plot the entire FEVD of the structural VARMA(8,4) model
% \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 FEVDs of the VARMA model.

armafevd(VARLag,VMALag,'Method','generalized');

% armafevd returns three figures. Figure k contains the generalized FEVD of variable k to a shock applied to all other variables at time 0.
% You can attribute most of the forecast error variance of variable 1 to a shock to variable 1.  Shocks to variables 2 and 3 contribute similarly to the forecast error variance of variable 1.
% You can attribute most of the forecast error variance of variable 2 to a shock to variable 2.  A shock to variable 3 does not contribute much to the forecast error variance of variable 2.
% You can attribute most of the forecast error variance of variable 3 to shocks to variables 1 and 3, each contributing similar amounts.  A shock to variable 2 does not contribute much to the forecast error variance of variable 3.
% Copyright 2018 The MathWorks, Inc.
%