%% Infer VEC Model Innovations
% Consider a VEC model for the following seven macroeconomic series, and then fit the model to the data.
% Gross domestic product (GDP)
% GDP implicit price deflator
% Paid compensation of employees
% Nonfarm business sector hours of all persons
% Effective federal funds rate
% Personal consumption expenditures
% Gross private domestic investment
% Suppose that a cointegrating rank of 4 and one short-run term are appropriate, that is, consider a VEC(1) model.
% Load the Data_USEconVECModel data set.

load Data_USEconVECModel

% For more information on the data set and variables, enter Description at the command line.
%
% Determine whether the data needs to be preprocessed by plotting the series on separate plots.

figure;
subplot(2,2,1)
plot(FRED.Time,FRED.GDP);
title('Gross Domestic Product');
ylabel('Index');
xlabel('Date');
subplot(2,2,2)
plot(FRED.Time,FRED.GDPDEF);
title('GDP Deflator');
ylabel('Index');
xlabel('Date');
subplot(2,2,3)
plot(FRED.Time,FRED.COE);
title('Paid Compensation of Employees');
ylabel('Billions of $');
xlabel('Date');
subplot(2,2,4)
plot(FRED.Time,FRED.HOANBS);
title('Nonfarm Business Sector Hours');
ylabel('Index');
xlabel('Date');

figure;
subplot(2,2,1)
plot(FRED.Time,FRED.FEDFUNDS);
title('Federal Funds Rate');
ylabel('Percent');
xlabel('Date');
subplot(2,2,2)
plot(FRED.Time,FRED.PCEC);
title('Consumption Expenditures');
ylabel('Billions of $');
xlabel('Date');
subplot(2,2,3)
plot(FRED.Time,FRED.GPDI);
title('Gross Private Domestic Investment');
ylabel('Billions of $');
xlabel('Date');

%
% Stabilize all series, except the federal funds rate, by applying the log transform. Scale the resulting series by 100 so that all series are on the same scale.

FRED.GDP = 100*log(FRED.GDP);      
FRED.GDPDEF = 100*log(FRED.GDPDEF);
FRED.COE = 100*log(FRED.COE);       
FRED.HOANBS = 100*log(FRED.HOANBS); 
FRED.PCEC = 100*log(FRED.PCEC);     
FRED.GPDI = 100*log(FRED.GPDI);

%
% Create a VEC(1) model using the shorthand syntax. Specify the variable names.

Mdl = vecm(7,4,1);
Mdl.SeriesNames = FRED.Properties.VariableNames

% Mdl is a vecm model object. All properties containing NaN values correspond to parameters to be estimated given data.
%
% Estimate the model using the entire data set and the default options.

EstMdl = estimate(Mdl,FRED.Variables)

% EstMdl is an estimated vecm model object. It is fully specified because all parameters have known values. By default, estimate imposes the constraints of the H1 Johansen VEC model form by removing the cointegrating trend and linear trend terms from the model. Parameter exclusion from estimation is equivalent to imposing equality constraints to zero.
% Infer innovations from the estimated model.
%

E = infer(EstMdl,FRED.Variables);

% E is a 238-by-7 matrix of inferred innovations. Columns correspond to the variable names in EstMdl.SeriesNames.
% Alternatively, you can return residuals when you call estimate by supplying an output variable in the fourth position.
%
% Plot the residuals on separate plots. Synchronize the residuals with the dates by removing the first EstMdl.P dates.

idx = FRED.Time((EstMdl.P + 1):end);

figure;
subplot(2,2,1)
plot(idx,E(:,1));
title('Residuals: GDP');
ylabel('Index (scaled)');
xlabel('Date');
subplot(2,2,2)
plot(idx,E(:,2));
title('Residuals: GDP Deflator');
ylabel('Index (scaled');
xlabel('Date');
subplot(2,2,3)
plot(idx,E(:,3));
title('Residuals: PCE');
ylabel('Billions of $ (scaled)');
xlabel('Date');
subplot(2,2,4)
plot(idx,E(:,4));
title('Residuals: NBSH');
ylabel('Index (scaled)');
xlabel('Date');

figure;
subplot(2,2,1)
plot(idx,E(:,5));
title('Residuals: Federal Funds Rate');
ylabel('Percent');
xlabel('Date');
subplot(2,2,2)
plot(idx,E(:,6));
title('Residuals: COE');
ylabel('Billions of $ (scaled)');
xlabel('Date');
subplot(2,2,3)
plot(idx,E(:,7));
title('Residuals: GPDI');
ylabel('Billions of $ (scaled)');
xlabel('Date');

% The residuals corresponding to the federal funds rate exhibit heteroscedasticity.
% Copyright 2012 The MathWorks, Inc.