%% Unconditional Mean of ARMA Model
% Find the lag coefficients and constant of the truncated MA 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 MA model by obtaining the first five coefficients of the truncated approximation of the infinite-lag polynomial.

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

% To compute the constant of the MA 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 AR lag operator polynomial, as in this equation.
% y_t = \Phi^{-1}(L)1.5 + \Phi^{-1}(L)\Theta(L)\varepsilon_t
%
% To compute the inverse of AR lag operator polynomial, use the lag operator left-division object function mldivide.

Phi = LagOp([1 -0.2 0.1]);
PhiInv = mldivide(Phi,1,'RelTol',1e-5);

% PhiInv 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 PhiInv to the ARMA model constant to obtain the MA model constant.

maConstant = 1.5*sum(cell2mat(toCellArray(PhiInv)))

% The approximate MA model is
% y_t = 1.667 + 0.7\varepsilon_{t-1} + 0.04\varepsilon_{t-2} - 0.062\varepsilon_{t-3} - 0.0164\varepsilon_{t-4} +
% 0.0029\varepsilon_{t-5} + \varepsilon_t.
% Since the unconditional expected value of all innovations is 0, the unconditional expected value (or mean) of the response series is
% E\left( y_t\right) = 1.667.
% Copyright 2012 The MathWorks, Inc.