%% Forecast Multiple Response Paths
% Forecast multiple response paths from a known SAR\left(1,0,0\right){\left(1,1,0\right)}_4 model by specifying multiple presample response paths.
% Create an arima model object that represents this quarterly SAR\left(1,0,0\right){\left(1,1,0\right)}_4 model:
% (1-0.5L)(1-0.2L^4)(1-L^4)y_t=1+\varepsilon_t,
% where \varepsilon_t is a standard Gaussian random variable.

Mdl = arima('Constant',1,'AR',0.5,'Variance',1,...
    'Seasonality',4,'SARLags',4,'SAR',0.2)

%
% Because Mdl contains autoregressive dynamic terms, forecast requires the previous Mdl.P responses to generate a t-period-ahead forecast from the model. Therefore, the presample should contain nine values.
% Generate a random 9-by-10 matrix representing 10 presample paths of length 9.

rng(1);
numpaths = 10;
Y0 = rand(Mdl.P,numpaths);

% Forecast 10 paths from the SAR model into a 12-quarter forecast horizon. Specify the presample observation paths Y0.

fh = 12;
YF = forecast(Mdl,fh,Y0);

% YF is a 12-by-10 matrix of independent forecasted paths.  YF(j,k) is the j-period-ahead forecast of path k. Path YF(:,k) represents the continuation of the presample path Y0(:,k).
% Plot the presample and forecasts.

Y = [Y0;...
     YF];

figure;
plot(Y);
hold on
h = gca;
px = [6.5 h.XLim([2 2]) 6.5];
py = h.YLim([1 1 2 2]);
hp = patch(px,py,[0.9 0.9 0.9]);
uistack(hp,"bottom");
axis tight
legend("Forecast period")
xlabel('Time (quarters)')
ylabel('Response paths')

% Copyright 2018 The MathWorks, Inc.