%% 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);

%
% Assume a 4-D VAR(3) model for the four series.  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 p-value, test statistic, and critical value.

cause = dfedfunds;
effects = [inflation rgdprate pcerate];
[hgc,pvalue,stat,cvalue] = gctest(cause,effects,'NumLags',2,...
    'Test',"f")

% The test decision hgc = 1 indicates that the null hypothesis that the federal funds rate is block exogenous should be rejected. This result suggests that the federal funds rate Granger-causes  at least one of the other variables in the system.
% To determine which variables the federal funds rate Granger-causes, you can run a leave-one-out test.  For more details, see gctest.
% Copyright 2018 The MathWorks, Inc.