%% Simulate a Regression Model with Nonstationary Multiplicative Seasonal Errors
% This example shows how to simulate sample paths from a regression model with multiplicative seasonal ARIMA errors using simulate. The time series is monthly international airline passenger numbers from 1949 to 1960.
% Load the airline and recessions data sets. Transform the response.

load('Data_Airline.mat')
load Data_Recessions
y = log(Data);

%
% Construct the predictor (X), which determines whether the country was in a recession during the sampled period. A 0 in row t means the country was not in a recession in month t, and a 1 in row t means that it was in a recession in month t.

X = zeros(numel(dates),1); % Preallocation
for j = 1:size(Recessions,1)
    X(dates >= Recessions(j,1) & dates <= Recessions(j,2)) = 1;
end

%
% Define index sets that partition the data into estimation and forecast samples.

nSim = 60; % Forecast period
T = length(y);
estInds = 1:(T-nSim);
foreInds = (T-nSim+1):T;

%
% Estimate the regression model with multiplicative seasonal errors:
% y_t = X_t\beta + u_t
% (1 - L)(1 - L^{12})u_t = (1+BL)(1+B_{12}L^{12})\varepsilon_t.
% Set the regression model intercept to 0 since it is not identifiable in an integrated model.

Mdl = regARIMA('D',1,'Seasonality',12,'MALags',1,'SMALags',12,...
    'Intercept',0);
EstMdl = estimate(Mdl,y(estInds),'X',X(estInds));

%
% Use the estimated coefficients of the model (contained in EstMdl), to simulate airline passenger counts over the 60-month horizon. Infer the residuals, and use them as a presample.

[e0,u0] = infer(EstMdl,y(estInds),'X',X(estInds));
rng(5);
numPaths = 500;
ySim = simulate(EstMdl,nSim,'numPaths',numPaths,'E0',e0,...
    'U0',u0,'X',X(foreInds));
meanYSim = mean(ySim,2);

figure
h1 = plot(dates(estInds),y(estInds));
title('{\bf Simulated Monthly Passenger Totals}')
hold on
plot(dates(foreInds),ySim,'Color',[.85,.85,.85])
h2 = plot(dates(foreInds),meanYSim,'k.-','LineWidth',2);
plot([dates(estInds(end)),dates(foreInds(1))],...
    [repmat(y(estInds(end)),numPaths,1),ySim(1,:)'],...
    'Color',[.85,.85,.85])
plot([dates(estInds(end)),dates(foreInds(1))],...
    [y(estInds(end)),meanYSim(1)],'k.-','LineWidth',2)
plot(dates(foreInds),y(foreInds))
datetick
legend([h1,h2],'Observations','Monte Carlo Forecasts',...
    'Location','NorthWest')
axis tight
hold off

% The simulated forecasts show growth and seasonal periodicity similar to the observed series. The regression model with SMA errors seems to forecast the series well, albeit slightly overestimating.
% Check the predictive performance of the model by:
% Varying the size of the forecast period
% Estimating the prediction mean square error (PMSE)
% Choosing the model with the lowest PMSE
% Copyright 2015 The MathWorks, Inc.