%% Convert ARMA Model That Includes Constant to AR Model
% Find the lag coefficients and constant of the truncated AR approximation of this univariate, stationary, and invertible ARMA model.
% y_t = 1.5 + 0.2y_{t-1} - 0.1y_{t-2} + \varepsilon_t +
% 0.5\varepsilon_{t-1}.
% The ARMA model is in difference-equation notation because the left side contains only y_t and its coefficient of 1. Create separate vectors for the AR and MA lag term coefficients in order starting from t - 1.

ar0 = [0.2 -0.1];
ma0 = 0.5;

%
% Convert the ARMA model to an AR model by obtaining the first five coefficients of the truncated approximation of the infinite-lag polynomial.

numLags = 5;
ar = arma2ar(ar0,ma0,numLags)

% To compute the constant of the AR model, consider the ARMA model in lag operator notation.
% \left(1 - 0.2L + 0.1L^2\right)y_t = 1.5  + (1 + 0.5L)\varepsilon_t
% or
% \Phi(L)y_t = 1.5 + \Theta(L)\varepsilon_t
% Part of the conversion involves premultiplying both sides of the equation by the inverse of the MA lag operator polynomial, as in this equation.
% \Theta^{-1}(L)\Phi(L)y_t = \Theta^{-1}(L)1.5 + \varepsilon_t
%
% To compute the inverse of MA lag operator polynomial, use the lag operator left-division object function mldivide.

Theta = LagOp([1 0.5]);
ThetaInv = mldivide(Theta,1,'RelTol',1e-5);

% ThetaInv is a LagOp lag operator polynomial.
%
% The application of lag operator polynomials to constants results in the product of the constant with the sum of the coefficients. Apply ThetaInv to the ARMA model constant to obtain the AR model constant.

arConstant = 1.5*sum(cell2mat(toCellArray(ThetaInv)))

% The approximate AR model is
% y_t = 1 + 0.7y_{t-1} - 0.45y_{t-2} + 0.225y_{t-3} - 0.1125y_{t-4} +
% 0.0562y_{t-5} + \varepsilon_t.
% Copyright 2012 The MathWorks, Inc.