%% Monte Carlo Confidence Intervals on True FEVD
% Consider the 4-D VAR(2) model in docid:econ_ug#mw_316eab93-fafe-43b4-b513-077bca729ca1. Estimate and plot its orthogonalized FEVD and 95% Monte Carlo confidence intervals on the true FEVD.
% 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 FEVD and corresponding 95% Monte Carlo confidence intervals from the estimated VAR(2) model.

rng(1); % For reproducibility
[Decomposition,Lower,Upper] = fevd(Mdl);

% Decomposition, Lower, and Upper are 20-by-4-by-4 arrays representing the orthogonalized FEVD of Mdl and corresponding lower and upper bounds of the confidence intervals. For all arrays, rows correspond to consecutive time points from time 1 to 20, columns correspond to variables receiving a one-standard-deviation innovation shock at time 0, and pages correspond to the variables whose forecast error variance fevd decomposes. Mdl.SeriesNames specifies the variable order.
%
% Plot the orthogonalized FEVD with its confidence bounds of the bond rate when real income is shocked at time 0.

fevdshock2resp3 = Decomposition(:,2,3);
FEVDCIShock2Resp3 = [Lower(:,2,3) Upper(:,2,3)];

figure;
h1 = plot(1:20,fevdshock2resp3);
hold on
h2 = plot(1:20,FEVDCIShock2Resp3,'r--');
legend([h1 h2(1)],["FEVD" "95% Confidence Interval"],...
    'Location',"best")
xlabel("Forecast Horizon");
ylabel("Variance Contribution");
title("FEVD of IB When Y Is Shocked");
grid on
hold off

% In the long run, and when real income is shocked, the proportion of forecast error variance of  the bond rate settles between approximately 0 and 0.5 with 95% confidence.
% Copyright 2018 The MathWorks, Inc.
%
%