%% Return VAR Model FEVDs
% Compute the generalized FEVDs of the two-dimensional 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 generalized FEVDs of y_t. Because no MA terms exist, specify an empty array ([]) for the second input argument.

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

% Y is a 31-by-2-by-2 array of FEVDs. Rows correspond to times 1 through 31 in the forecast horizon, columns correspond to the variables that armafevd shocks at time 0, and pages correspond to the FEVD of the variables in the system.  For example, the contribution to the forecast error variance of variable 2 at time 10 in the forecast horizon, attributable to a shock to variable 1, is Y(10,1,2) = 0.1302.
% armafevd 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 FEVDs for the first 10 periods.

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

% Y10 is a 10-by-2-by-2 array of FEVDs.  Rows correspond to times 1 through 10 in the forecast horizon.  In all FEVDs, the contributions appear to stabilize before 10 periods elapse.
% For each variable (page), compute the row sums.

sum(Y10,2)

% For generalized FEVDs, forecast error variance contributions at each period in the forecast horizon do not necessarily sum to one.  This characteristic is in contrast to orthogonalized FEVDs, in which all rows sum to one.
% Copyright 2018 The MathWorks, Inc.