%% Infer Innovations from Model Containing Regression Component
% Estimate a VAR(4) model of the consumer price index (CPI), the unemployment rate, and the gross domestic product (GDP). Include a linear regression component containing the current quarter and the last four quarters of government consumption expenditures and investment (GCE). Infer model innovations.
% Load the Data_USEconModel data set. Compute the real GDP.

load Data_USEconModel
DataTable.RGDP = DataTable.GDP./DataTable.GDPDEF*100;

%
% Plot all variables on separate plots.

figure;
subplot(2,2,1)
plot(DataTable.Time,DataTable.CPIAUCSL);
ylabel('Index');
title('Consumer Price Index');
subplot(2,2,2)
plot(DataTable.Time,DataTable.UNRATE);
ylabel('Percent');
title('Unemployment Rate');
subplot(2,2,3)
plot(DataTable.Time,DataTable.RGDP);
ylabel('Output');
title('Real Gross Domestic Product');
subplot(2,2,4)
plot(DataTable.Time,DataTable.GCE);
ylabel('Billions of $');
title('Government Expenditures');

%
% Stabilize the CPI, GDP, and GCE by converting each to a series of growth rates. Synchronize the unemployment rate series with the others by removing its first observation.

inputVariables = {'CPIAUCSL' 'RGDP' 'GCE'};
Data = varfun(@price2ret,DataTable,'InputVariables',inputVariables);
Data.Properties.VariableNames = inputVariables;
Data.UNRATE = DataTable.UNRATE(2:end);

%
% Expand the GCE rate series to a matrix that includes its current value and up through four lagged values. Remove the GCE variable from Data.

rgcelag4 = lagmatrix(Data.GCE,0:4);
Data.GCE = [];

%
% Create a default VAR(4) model using the shorthand syntax.

Mdl = varm(3,4);
Mdl.SeriesNames = ["rcpi" "unrate" "rgdpg"];

%
% Estimate the model using the entire sample. Specify the GCE matrix as data for the regression component.

EstMdl = estimate(Mdl,Data.Variables,'X',rgcelag4);

%
% Infer innovations from the estimated model. Supply the predictor data. Return the loglikelihood objective function value.

[E,logL] = infer(EstMdl,Data.Variables,'X',rgcelag4);
logL

% E is a 240-by-3 matrix of inferred innovations. The columns contain the residuals corresponding to the CPI growth rate, unemployment rate, and GDP growth rate, respectively.
%
% Plot the residuals on separate plots. Synchronize the residuals with the dates by removing any missing observations from the data and  removing the first Mdl.P dates.

idx = all(~isnan([Data.Variables rgcelag4]),2);
datesr = DataTable.Time(idx);

figure;
for j = 1:Mdl.NumSeries
    subplot(2,2,j)
    plot(datesr((Mdl.P + 1):end),E(:,j));
    ylabel(Mdl.SeriesNames{j});
    xlabel('Date');
    title('Residual plot');
    hold on
    plot([min(datesr) max(datesr)],[0 0],'r--');
    hold off
end

% The residuals corresponding to the CPI and GDP growth rates exhibit heteroscedasticity because the CPI series appears to cycle through periods of higher and lower variance. Also, the first half of the GDP series seems to have higher variance than the latter half.
% Copyright 2012 The MathWorks, Inc.