%% Estimate Generalized FEVD of VAR Model
% Consider the 4-D VAR(2) model in docid:econ_ug#mw_316eab93-fafe-43b4-b513-077bca729ca1. Estimate the generalized FEVD of the system for 100 periods.
% Load the Danish money and income data set, then estimate the VAR(2) model.

load Data_JDanish

Mdl = varm(4,2);
Mdl.SeriesNames = DataTable.Properties.VariableNames;
Mdl = estimate(Mdl,DataTable.Series);

%
% Estimate the generalized FEVD from the estimated VAR(2) model over a forecast horizon with length 100.

Decomposition = fevd(Mdl,"Method","generalized","NumObs",100);

% Decomposition is a 100-by-4-by-4 array representing the generalized FEVD of Mdl.
% Plot the generalized FEVD of the bond rate when real income is shocked at time 0.

figure;
plot(1:100,Decomposition(:,2,3))
title("FEVD of IB When Y Is Shocked")
xlabel("Forecast Horizon")
ylabel("Variance Contribution")
grid on

% When real income is shocked, the contribution of the bond rate to the forecast error variance settles at approximately 0.061.
% Copyright 2018 The MathWorks, Inc.
%