%% Forecast Responses from VEC Model
% This example follows from Estimate VEC Model.
% Create and estimate the VEC(1) model. Treat the last ten periods as the forecast horizon.

load Data_USEconVECModel
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);

Mdl = vecm(7,4,1)
Y = FRED{1:(end - 10),:};
EstMdl = estimate(Mdl,Y)

% Forecast 10 responses using the estimated model and in-sample data as presample observations.
%

YF = forecast(EstMdl,10,Y);

% On separate plots, plot part of the GDP and GPDI series with their forecasted values.
%

figure;
plot(FRED.Time(end - 50:end),FRED.GDP(end - 50:end));
hold on
plot(FRED.Time((end - 9):end),YF(:,1))
h = gca;
fill(FRED.Time([end - 9 end end end - 9]),h.YLim([1,1,2,2]),'k',...
    'FaceAlpha',0.1,'EdgeColor','none');
legend('True','Forecasted','Location','NW')
title('Quarterly Scaled GDP: 2004 - 2016');
ylabel('Billions of $ (scaled)');
xlabel('Year');
hold off

figure;
plot(FRED.Time(end - 50:end),FRED.GPDI(end - 50:end));
hold on
plot(FRED.Time((end - 9):end),YF(:,7))
h = gca;
fill(FRED.Time([end - 9 end end end - 9]),h.YLim([1,1,2,2]),'k',...
    'FaceAlpha',0.1,'EdgeColor','none');
legend('True','Forecasted','Location','NW')
title('Quarterly Scaled GPDI: 2004 - 2016');
ylabel('Billions of $ (scaled)');
xlabel('Year');
hold off

% Copyright 2012 The MathWorks, Inc.