%% Infer Residuals from a Regression Model with ARIMA Errors
% Forecast responses from the following regression model with ARMA(2,1) errors over a 30-period horizon:
% \begin{array}{*{20}{l}}
% \begin{array}{c}
% {y_t} = {X_t}\left[ {\begin{array}{*{20}{c}}
% {0.1}\\
% { - 0.2}
% \end{array}} \right] + {u_t}\\
% {u_t} = 0.5{u_{t - 1}} - 0.8{u_{t - 2}} + {\varepsilon _t} - 0.5{\varepsilon _{t - 1}},
% \end{array}
% \end{array}
% where \varepsilon_{t} is Gaussian with variance 0.1.
% Specify the regression model with ARIMA errors. Simulate responses from the model and two predictor series.

Mdl = regARIMA('Intercept', 0, 'AR', {0.5 -0.8}, ...
    'MA',-0.5,'Beta',[0.1 -0.2], 'Variance',0.1);
rng(1); % For reproducibility
X =  randn(100,2);
y = simulate(Mdl,100,'X',X);

%
% Infer, and then plot residuals. By default, infer backcasts for the necessary presample unconditional disturbances.

e = infer(Mdl,y,'X',X);

figure
plot(e)
title('Inferred Residuals')

% Copyright 2015 The MathWorks, Inc.