%% Convert a Regression Model with ARMA Errors to an ARIMAX Model
% Convert a regression model with ARMA(4,1) errors to an ARIMAX model using the arima converter.
% Specify the regression model with ARMA(4,1) errors:
% \begin{array}{l}
% {y_t} = 1 + 0.5{X_t} + {u_t}\\
% {u_t} = 0.8{u_{t - 1}} - 0.4{u_{t - 4}} + {\varepsilon _t} + 0.3{\varepsilon _{t - 1}},
% \end{array}
% where \varepsilon_t is Gaussian with mean 0 and variance 1.

Mdl = regARIMA('AR',{0.8, -0.4},'MA',0.3,...
    'ARLags',[1 4],'Intercept',1,'Beta',0.5,...
    'Variance',1)

% You can verify that the lags of the autoregressive terms are 1 and 4 in the AR row.
%
% Generate random predictor data.

rng(1); % For reproducibility
T = 20;
X = randn(T,1);

%
% Convert Mdl to an ARIMAX model.

[ARIMAX,XNew] = arima(Mdl,'X',X);
ARIMAX

% The new arima model, ARIMAX, is
% {y_t} = 0.6 + {Z_t}\Gamma  + 0.8{y_{t - 1}} - 0.4{y_{t - 4}} + {\varepsilon _t} + 0.3{\varepsilon _{t - 1}},
% where
% {Z_t}\Gamma  = \left[ {\begin{array}{*{20}{c}}
% {0.5{x_1}}&{NaN}&{NaN}\\
% {0.5{x_2}}&{0.5{x_1}}&{NaN}\\
% {0.5{x_3}}&{0.5{x_2}}&{NaN}\\
% {0.5{x_4}}&{0.5{x_3}}&{NaN}\\
% {0.5{x_5}}&{0.5{x_4}}&{0.5{x_1}}\\
%  \vdots & \vdots & \vdots \\
% {{{0.5}_T}}&{0.5{x_{T - 1}}}&{0.5{x_{T - 4}}}
% \end{array}} \right]\left[ \begin{array}{c}
% 1\\
%  - 0.8\\
% 0.4
% \end{array} \right]
% and x_{j} is row j of X. Since the product of the autoregressive and integration polynomials is \phi (L) = (1 - 0.8L + 0.4{L^4}), ARIMAX.Beta is simply [1 -0.8 0.4]. Note that the software carries over the autoregressive and moving average coefficients from Mdl to ARIMAX. Also, Mdl.Intercept = 1 and ARIMAX.Constant = (1 - 0.8 + 0.4)(1) = 0.6, i.e., the regARIMA model intercept and arima model constant are generally unequal.
% Copyright 2015 The MathWorks, Inc.