%% Conduct Leave-One-Out Granger Causality Test
% Conduct a leave-one-out Granger causality test to assess whether each variable in a 3-D VAR  model Granger-causes another variable, given the third variable. The variables in the VAR model are the M1 money supply, consumer price index (CPI), and US gross domestic product (GDP).
% Load the US macroeconomic data set Data_USEconModel.mat.

load Data_USEconModel

% The data set includes the MATLAB® timetable DataTable, which contains 14 variables measured from Q1 1947 through Q1 2009.
% M1SL is the table variable containing the M1 money supply.
% CPIAUCSL is the table variable containing the CPI.
% GDP is the table variable containing the US GDP.
% For more details, enter Description at the command line.
%
% Visually assess whether the series are stationary.

plot(DataTable.Time,DataTable.CPIAUCSL)
ylabel("Money Supply");

plot(DataTable.Time,DataTable.M1SL)
ylabel("CPI");

plot(DataTable.Time,DataTable.GDP)
ylabel("GDP")

% All series are nonstationary.
% Stabilize the series.
% Convert the M1 money supply prices to returns.
% Convert the CPI to the inflation rate.
% Convert the GDP to the real GDP rate with respect to year 2000 dollars.

m1slrate = price2ret(DataTable.M1SL);
inflation = price2ret(DataTable.CPIAUCSL);
rgdprate = price2ret(DataTable.GDP./DataTable.GDPDEF);

% Preprocess the data by removing all missing observations (indicated by NaN).

tbl = table(m1slrate,inflation,rgdprate);
tbl = rmmissing(tbl);
T = size(tbl,1); % Total sample size

%
% Fit VAR models, with lags ranging from 1 to 4, to the series. Initialize each fit by specifying the first four observations. Store the Akaike information criteria (AIC) of the fits.

numseries = 3;
numlags = (1:4)';
nummdls = numel(numlags);

% Partition time base.
maxp = max(numlags); % Maximum number of required presample responses
idxpre = 1:maxp;
idxest = (maxp + 1):T;

% Preallocation
EstMdl(nummdls) = varm(numseries,0);
aic = zeros(nummdls,1);

% Fit VAR models to data.
Y0 = tbl{idxpre,:}; % Presample
Y = tbl{idxest,:};  % Estimation sample
for j = 1:numel(numlags)
    Mdl = varm(numseries,numlags(j));
    Mdl.SeriesNames = tbl.Properties.VariableNames;
    EstMdl(j) = estimate(Mdl,Y,'Y0',Y0);
    results = summarize(EstMdl(j));
    aic(j) = results.AIC;
end

[~,bestidx] = min(aic);
p = numlags(bestidx)
BestMdl = EstMdl(bestidx);

% A VAR(3) model yields the best fit.
%
% For each variable and equation in the system, conduct a leave-one-out Granger causality test to assess whether a variable in the fitted VAR(3) model is the 1-step Granger-cause of another variable, given the third variable.

h = gctest(BestMdl)

% gctest conducts six tests, so h is a 6-by-1 logical vector of test decisions corresponding to the rows of the test summary table. The results indicate the following decisions, at a 5% level of significance:
% Do not reject the claim that the inflation rate is not the 1-step Granger-cause of the M1 money supply rate, given the real GDP rate (h(1) = 0).
% Do not reject the claim that the real GDP rate is not the 1-step Granger-cause of the M1 money supply rate, given the inflation rate (h(2) = 0).
% Do not reject the claim that the M1 money supply rate is not the 1-step Granger-cause of the inflation rate, given the real GDP rate (h(3) = 0).
% Reject the claim that the real GDP rate is not the 1-step Granger-cause of the inflation rate, given the M1 money supply rate (h(4) = 1).
% Do not reject the claim that the M1 money supply rate is not the 1-step Granger-cause of the real GDP rate, given the inflation rate (h(5) = 0).
% Reject the claim that the inflation rate is not the 1-step Granger-cause of the real GDP rate, given the M1 money supply rate  (h(6) = 1).
% Because the inflation and real GDP rates are 1-step Granger-causes of each other, they constitute a feedback loop.
% Copyright 2018 The MathWorks, Inc.
%