%% Estimate Generalized FEVD of VEC Model
% Consider the 4-D VEC(2) model with two cointegrating relations in docid:econ_ug#mw_101bd8af-f7f3-42a7-959b-5dfcfef8a950. Estimate the generalized FEVD of the system for 100 periods.
% Load the Danish money and income data set, then estimate the VEC(2) model.

load Data_JDanish

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

%
% Estimate the generalized FEVD from the estimated VEC(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.08.
% Copyright 2018 The MathWorks, Inc.