%% Infer Residuals from ARMAX Model
% Infer residuals from an ARMAX model.
% Specify an ARMA(1,2) model using known parameters for the response (MdlY) and an AR(1) model for the predictor data (MdlX).

MdlY = arima('AR',0.2,'MA',{-0.1,0.6},'Constant',...
    1,'Variance',2,'Beta',3);
MdlX = arima('AR',0.3,'Constant',0,'Variance',1);

%
% Simulate response and predictor data with 102 observations.

rng 'default'; % For reproducibility
X = simulate(MdlX,102);
Y = simulate(MdlY,102,'X',X);

%
% Use the first two responses as presample data, and infer residuals for the remaining 100 observations.

E = infer(MdlY,Y(3:end),'Y0',Y(1:2),'X',X);
figure;
plot(E);
title 'Inferred Residuals'; 

% Copyright 2015 The MathWorks, Inc.