%% Forecast Unconditional Response Series from VEC Model
% Consider a VEC model for the following seven macroeconomic series. Then, fit the model to the data and forecast responses 12 quarters into the future.
% 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.
%
% Forecast responses from the estimated model over a three-year horizon. Specify the entire data set as presample observations.

numperiods = 12;
Y0 = FRED.Variables;
Y = forecast(EstMdl,numperiods,Y0);

% Y is a 12-by-7 matrix of forecasted responses. Rows correspond to the forecast horizon, and columns correspond to the variables in EstMdl.SeriesNames.
%
% Plot the forecasted responses and the last 50 true responses.

fh = dateshift(FRED.Time(end),'end','quarter',1:12);

figure;
subplot(2,2,1)
h1 = plot(FRED.Time((end-49):end),FRED.GDP((end-49):end));
hold on
h2 = plot(fh,Y(:,1));
title('Gross Domestic Product');
ylabel('Index (scaled)');
xlabel('Date');
h = gca;
fill([FRED.Time(end) fh([end end]) FRED.Time(end)],h.YLim([1 1 2 2]),'k',...
    'FaceAlpha',0.1,'EdgeColor','none');
legend([h1 h2],'True','Forecast','Location','Best')
hold off
subplot(2,2,2)
h1 = plot(FRED.Time((end-49):end),FRED.GDPDEF((end-49):end));
hold on
h2 = plot(fh,Y(:,2));
title('GDP Deflator');
ylabel('Index (scaled)');
xlabel('Date');
h = gca;
fill([FRED.Time(end) fh([end end]) FRED.Time(end)],h.YLim([1 1 2 2]),'k',...
    'FaceAlpha',0.1,'EdgeColor','none');
legend([h1 h2],'True','Forecast','Location','Best')
hold off
subplot(2,2,3)
h1 = plot(FRED.Time((end-49):end),FRED.COE((end-49):end));
hold on
h2 = plot(fh,Y(:,3));
title('Paid Compensation of Employees');
ylabel('Billions of $ (scaled)');
xlabel('Date');
h = gca;
fill([FRED.Time(end) fh([end end]) FRED.Time(end)],h.YLim([1 1 2 2]),'k',...
    'FaceAlpha',0.1,'EdgeColor','none');
legend([h1 h2],'True','Forecast','Location','Best')
hold off
subplot(2,2,4)
h1 = plot(FRED.Time((end-49):end),FRED.HOANBS((end-49):end));
hold on
h2 = plot(fh,Y(:,4));
title('Nonfarm Business Sector Hours');
ylabel('Index (scaled)');
xlabel('Date');
h = gca;
fill([FRED.Time(end) fh([end end]) FRED.Time(end)],h.YLim([1 1 2 2]),'k',...
    'FaceAlpha',0.1,'EdgeColor','none');
legend([h1 h2],'True','Forecast','Location','Best')
hold off

figure;
subplot(2,2,1)
h1 = plot(FRED.Time((end-49):end),FRED.FEDFUNDS((end-49):end));
hold on
h2 = plot(fh,Y(:,5));
title('Federal Funds Rate');
ylabel('Percent');
xlabel('Date');
h = gca;
fill([FRED.Time(end) fh([end end]) FRED.Time(end)],h.YLim([1 1 2 2]),'k',...
    'FaceAlpha',0.1,'EdgeColor','none');
legend([h1 h2],'True','Forecast','Location','Best')
hold off
subplot(2,2,2)
h1 = plot(FRED.Time((end-49):end),FRED.PCEC((end-49):end));
hold on
h2 = plot(fh,Y(:,6));
title('Consumption Expenditures');
ylabel('Billions of $ (scaled)');
xlabel('Date');
h = gca;
fill([FRED.Time(end) fh([end end]) FRED.Time(end)],h.YLim([1 1 2 2]),'k',...
    'FaceAlpha',0.1,'EdgeColor','none');
legend([h1 h2],'True','Forecast','Location','Best')
hold off
subplot(2,2,3)
h1 = plot(FRED.Time((end-49):end),FRED.GPDI((end-49):end));
hold on
h2 = plot(fh,Y(:,7));
title('Gross Private Domestic Investment');
ylabel('Billions of $ (scaled)');
xlabel('Date');
h = gca;
fill([FRED.Time(end) fh([end end]) FRED.Time(end)],h.YLim([1 1 2 2]),'k',...
    'FaceAlpha',0.1,'EdgeColor','none');
legend([h1 h2],'True','Forecast','Location','Best')
hold off

% Copyright 2012 The MathWorks, Inc.