%% Bootstrap Confidence Intervals on True FEVD
% Consider the 4-D VEC(2) model with two cointegrating relations in docid:econ_ug#mw_101bd8af-f7f3-42a7-959b-5dfcfef8a950. Estimate and plot its orthogonalized FEVD and 90% bootstrap confidence intervals on the true FEVD.
% Load the Danish money and income data set, then estimate the VEC(2) model. Return the  residuals from model estimation.

load Data_JDanish

Mdl = vecm(4,2,2);
Mdl.SeriesNames = DataTable.Properties.VariableNames;
[Mdl,~,~,E] = estimate(Mdl,DataTable.Series);
T = size(DataTable,1) % Total sample size
n = size(E,1)         % Effective sample size

% E is a 52-by-4 array of residuals.  Columns correspond to the variables in Mdl.SeriesNames. The estimate function requires Mdl.P = 3 observations to initialize a VEC(2) model for estimation. Because presample data (Y0) is unspecified, estimate takes the first three  observations in the specified response data to initialize the model.  Therefore, the resulting effective sample size is T – Mdl.P = 52, and rows of E correspond to the observation indices 4 through T.
%
% Estimate the orthogonalized FEVD and corresponding 90% bootstrap confidence intervals from the estimated VEC(2) model. Draw 500 paths of length n from the series of residuals.

rng(1); % For reproducibility
[Decomposition,Lower,Upper] = fevd(Mdl,"E",E,"NumPaths",500,...
    "Confidence",0.9);

%
% 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(0:19,fevdshock2resp3);
hold on
h2 = plot(0:19,FEVDCIShock2Resp3,'r--');
legend([h1 h2(1)],["FEVD" "90% Confidence Interval"],...
    'Location',"best")
xlabel("Time Index");
ylabel("Response");
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.6 with 90% confidence.
% Copyright 2018 The MathWorks, Inc.
%
%