%% Forecast Responses from VAR(4) Model
% This example follows from Estimate VAR(4) Model.
% Create and estimate a VAR(4) model for the CPI growth rate and unemployment rates. Treat the last ten periods as the forecast horizon.

load Data_USEconModel
cpi = DataTable.CPIAUCSL;
unrate = DataTable.UNRATE;

rcpi = price2ret(cpi);
unrate = unrate(2:end);
Y = [rcpi unrate];

Mdl = varm(2,4);
EstMdl = estimate(Mdl,Y(1:(end-10),:));

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

YF = forecast(EstMdl,10,Y(1:(end-10),:));

%
% Plot the part of the series with their forecasted values on separate plots.

figure;
plot(DataTable.Time(end - 50:end),rcpi(end - 50:end));
hold on
plot(DataTable.Time((end - 9):end),YF(:,1))
h = gca;
fill(DataTable.Time([end - 9 end end end - 9]),h.YLim([1,1,2,2]),'k',...
    'FaceAlpha',0.1,'EdgeColor','none');
legend('True CPI growth rate','Forecasted CPI growth rate',...
    'Location','NW')
title('Quarterly CPI Growth Rate: 1947 - 2009');
ylabel('CPI growth rate');
xlabel('Year');
hold off

figure;
plot(DataTable.Time(end - 50:end),unrate(end - 50:end));
hold on
plot(DataTable.Time((end - 9):end),YF(:,2))
h = gca;
fill(DataTable.Time([end - 9 end end end - 9]),h.YLim([1,1,2,2]),'k',...
    'FaceAlpha',0.1,'EdgeColor','none');
legend('True unemployment rate','Forecasted unemployment rate',...
    'Location','NW')
title('Quarterly Unemployment Rate: 1947 - 2009');
ylabel('Unemployment rate');
xlabel('Year');
hold off

% Copyright 2012 The MathWorks, Inc.