%% Conduct Granger Causality Test
% Conduct a Granger causality test to assess whether the M1 money supply has an impact on the predictive distribution of the consumer price index (CPI).
% 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, and CPIAUCSL is the table variable containing the CPI. For more details, enter Description at the command line.
%
% Visually assess whether the series are stationary by plotting them in the same figure.

figure;
yyaxis left
plot(DataTable.Time,DataTable.CPIAUCSL)
ylabel("CPI");
yyaxis right
plot(DataTable.Time,DataTable.M1SL);
ylabel("Money Supply");

% Both series are nonstationary.
% Stabilize the series by converting them to rates.

m1slrate = price2ret(DataTable.M1SL);
inflation = price2ret(DataTable.CPIAUCSL);

%
% Assume that a VAR(1) model is an appropriate multivariate model for the rates.  Conduct a default Granger causality test to assess whether the M1 money supply rate Granger-causes the  inflation rate.

h = gctest(m1slrate,inflation)

% The test decision h is 1, which indicates the rejection of the null hypothesis that the M1 money supply rate does not Granger-cause inflation.
% Copyright 2018 The MathWorks, Inc.