%% Test for Block Exogeneity
% Time series are block exogenous if they do not Granger-cause any other variables in a multivariate system. Test whether the effective federal funds rate is block exogenous with respect to the real GDP, personal consumption  expenditures, and inflation rates.
% Load the US macroeconomic data set Data_USEconModel.mat. Convert the price series to returns.

load Data_USEconModel
inflation = price2ret(DataTable.CPIAUCSL);
rgdprate = price2ret(DataTable.GDP./DataTable.GDPDEF);
pcerate = price2ret(DataTable.PCEC);

%
% Test whether the federal funds rate is nonstationary by conducting an augmented Dickey-Fuller test.  Specify that the alternative model has a drift term and an F test.

h = adftest(DataTable.FEDFUNDS,'Model',"ard")

% The test decision h = 0 indicates that the null hypothesis that the series has a unit root should not be rejected.
% To stabilize the federal funds rate series, apply the first difference to it.

dfedfunds = diff(DataTable.FEDFUNDS);

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

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

% Assume a 4-D VAR(3) model for the four series. Initialize the model by using the first three observations, and fit the model to the rest of the data.  Assign names to the series in the model.

Mdl = varm(4,3);
Mdl.SeriesNames = tbl.Properties.VariableNames;
EstMdl = estimate(Mdl,tbl{4:end,:},'Y0',tbl{1:3,:});

%
% Assess whether the federal funds rate is block exogenous with respect to the real GDP, personal consumption expenditures, and inflation rates. Conduct an F-based Wald test, and return the test decision and  summary table.  Suppress the test results display.

cause = "dfedfunds";
effects = ["inflation" "pcerate" "rgdprate"];
[h,Summary] = gctest(EstMdl,'Type',"blockwise",...
    'Cause',cause,'Effect',effects,'Test',"f",...
    'Display',false);

% gctest conducts one test. h = 1 indicates, at a 5% level of significance, rejection of the null hypothesis that the federal funds rate is block exogenous with respect to the other variables in the VAR model. This result suggests that the federal funds rate Granger-causes at least one of the other variables in the system.
% Alternatively, you can conduct the same blockwise Granger causality test by passing the data to the gctest function.

causedata = tbl.dfedfunds;
EffectsData = tbl{:,effects};
[hgc,pvalue,stat,cvalue] = gctest(causedata,EffectsData,...
    'NumLags',3,'Test',"f")

% To determine which variables are Granger-caused by the federal funds rate, conduct a leave-one-out test and specify the "cause" and "effects."

gctest(EstMdl,'Cause',cause,'Effect',effects);

% The test results suggest the following decisions, each at a 5% level of significance:
% Reject the claim that the federal funds rate is not a 1-step Granger-cause of the inflation rate, given all other variables in the VAR model.
% Reject the claim that the federal funds rate is not a 1-step Granger-cause of the personal consumption expenditures rate, given all other variables in the VAR model.
% Reject the claim that the federal funds rate is not a 1-step Granger-cause of the real GDP rate, given all other variables in the VAR model.
% Copyright 2018 The MathWorks, Inc.
%