%% Simulate an MA Error Model
% This example shows how to simulate responses from a regression model with MA errors without specifying a presample.
% Specify the regression model with MA(8) errors:
% \begin{array}{l}{y_t} = 2 + {X_t}\left[ \begin{array}{l} - 2\\1.5\end{array} \right] + {u_t}\\{u_t} = {\varepsilon _t} + 0.4{\varepsilon _{t - 1}} - 0.3{\varepsilon _{t - 4}} + 0.2{\varepsilon _{t - 8}},\end{array}
% where \varepsilon_t is Gaussian with mean 0 and variance 0.5.

Beta = [-2; 1.5];
Intercept = 2;
b1 = 0.4;
b4 = -0.3;
b8 = 0.2;
Variance = 0.5;
Mdl = regARIMA('MA',{b1, b4, b8},'MALags',[1 4 8],...
    'Intercept',Intercept,'Beta',Beta,'Variance',Variance);

%
% Generate two length T = 100 predictor series by random selection from the standard Gaussian distribution.

T = 100;
rng(4); % For reproducibility
X = randn(T,2);

% The software treats the predictors as nonstochastic series.
%
% Generate and plot one sample path of responses from Mdl.

rng(5);
ySim = simulate(Mdl,T,'X',X);

figure
plot(ySim)
title('{\bf Simulated Response Series}')

% simulate requires Q = 8 presample innovations (\varepsilon_t) to initialize the error series. Without them, as in this case, simulate sets the necessary presample innovations to 0.
%
% Alternatively, use filter to filter a random innovation series through Mdl.

rng(5);
e = randn(T,1);
yFilter = filter(Mdl,e,'X',X);

figure
plot(yFilter)
title('{\bf Simulated Response Series Using Filtered Innovations}')

% The plots suggest that the simulated responses and the responses generated from the filtered innovations are equivalent.
%
% Simulate 1000 response paths from Mdl. Assess transient effects by plotting the unconditional disturbance (U) variances across the simulated paths at each period.

numPaths = 1000;
[Y,~,U] = simulate(Mdl,T,'NumPaths',numPaths,'X',X);

figure
h1 = plot(Y,'Color',[.85,.85,.85]);
title('{\bf 1000 Simulated Response Paths}')
hold on
h2 = plot(1:T,Intercept+X*Beta,'k--','LineWidth',2);
legend([h1(1),h2],'Simulated Path','Mean')
hold off

figure
h1 = plot(var(U,0,2),'r','LineWidth',2);
hold on
theoVarFix = (1+b1^2+b4^2+b8^2)*Variance;
h2 = plot([1 T],[theoVarFix theoVarFix],'k--','LineWidth',2);
title('{\bf Unconditional Disturbance Variance}')
legend([h1,h2],'Simulation Variance','Theoretical Variance')
hold off

% The simulated paths follow their theoretical mean, c + X\beta, which is not constant over time (and might look nonstationary).
% The variance of the process is not constant, but levels off at the theoretical variance by the 15th period. The theoretical variance of the MA(8) error model is
% (1 + b_1^2 + b_4^2 + b_8^2)\sigma _\varepsilon ^2 = \left( {1 + {{0.4}^2} + {{( - 0.3)}^2} + {{0.2}^2}} \right)0.5 = 0.645.
%
% You can reduce transient effects is by partitioning the simulated data into a burn-in portion and a portion for analysis. Do not use the burn-in portion for analysis. Include enough periods in the burn-in portion to overcome the transient effects.

burnIn = 1:15;
notBurnIn = burnIn(end)+1:T;
Y = Y(notBurnIn,:);
X = X(notBurnIn,:);
U = U(notBurnIn,:);
figure
h1 = plot(notBurnIn,Y,'Color',[.85,.85,.85]);
hold on
h2 = plot(notBurnIn,Intercept+X*Beta,'k--','LineWidth',2);
title('{\bf 1000 Simulated Response Paths for Analysis}')
legend([h1(1),h2],'Simulated Path','Mean')
axis tight
hold off

figure
h1 = plot(notBurnIn,var(U,0,2),'r','LineWidth',2);
hold on
h2 = plot([notBurnIn(1) notBurnIn(end)],...
    [theoVarFix theoVarFix],'k--','LineWidth',2);
title('{\bf Converged Unconditional Disturbance Variance}')
legend([h1,h2],'Simulation Variance','Theoretical Variance')
axis tight
hold off

% Unconditional disturbance simulation variances fluctuate around the theoretical variance due to Monte Carlo sampling error. Be aware that the exclusion of the burn-in sample from analysis reduces the effective sample size.
% Copyright 2015 The MathWorks, Inc.