%% Convert a Structural VARMA model to a Structural VMA model
% Find the coefficients of the truncated, structural VMA equivalent of the structural, stationary, and invertible VARMA model
% \begin{array}{*{20}{l}}\left\{ {\left[ {\begin{array}{*{20}{c}}
% 1&{0.2}&{ - 0.1}\\
% {0.03}&1&{ - 0.15}\\
% {0.9}&{ - 0.25}&1
% \end{array}} \right] + \left[ {\begin{array}{*{20}{c}}
% {0.5}&{-0.2}&{-0.1}\\
% {-0.3}&{-0.1}&{0.1}\\
% {0.4}&{-0.2}&{-0.05}
% \end{array}} \right]{L^4} + \left[ {\begin{array}{*{20}{c}}
% {0.05}&{-0.02}&{-0.01}\\
% {-0.1}&{-0.01}&{-0.001}\\
% {0.04}&{-0.02}&{-0.005}
% \end{array}} \right]{L^8}} \right\}{y_t} =\\
% \left\{ {\left[ {\begin{array}{*{20}{c}}
% { 1}&{0}&{0}\\
% {0}&{1}&{0}\\
% {0}&{0}&{1}
% \end{array}} \right] + \left[ {\begin{array}{*{20}{c}}
% { - 0.02}&{ 0.03}&{0.3}\\
% {0.003}&{0.001}&{0.01}\\
% { 0.3}&{0.01}&{0.01}
% \end{array}} \right]{L^4}} \right\}{\varepsilon _t}\end{array}
% where y_t = \left[y_{1t}\;\;\;y_{2t}\;\;\;y_{3t}\right]' and \varepsilon_t = \left[\varepsilon_{1t}\;\;\;\varepsilon_{2t}\;\;\;\varepsilon_{3t}\right]'.
% The VARMA model is in lag operator notation because the response and innovation vectors are on opposite sides of the equation.
% Create a cell vector containing the VAR matrix coefficients. Because this model is a structural model, start with the coefficient of y_t and enter the rest in order by lag. Construct a vector that indicates the degree of the lag term for the corresponding coefficients.

var0 = {[1 0.2 -0.1; 0.03 1 -0.15; 0.9 -0.25 1],...
    [0.5 -0.2 -0.1; -0.3 -0.1 0.1; 0.4 -0.2 -0.05],...
    [0.05 -0.02 -0.01; -0.1 -0.01 -0.001; 0.04 -0.02 -0.005]};
var0Lags = [0 4 8];

%
% Create a cell vector containing the VMA matrix coefficients. Because this model is a structural model, start with the coefficient of \varepsilon_t and enter the rest in order by lag. Construct a vector that indicates the degree of the lag term for the corresponding coefficients.

vma0 = {eye(3),...
    [-0.02 0.03 0.3; 0.003 0.001 0.01; 0.3 0.01 0.01]};
vma0Lags = [0 4];

%
% arma2ma requires LagOp lag operator polynomials for input arguments that comprise structural VAR or VMA models. Construct separate LagOp polynomials that describe the VAR and VMA components of the VARMA model.

VARLag = LagOp(var0,'Lags',var0Lags);
VMALag = LagOp(vma0,'Lags',vma0Lags);

% VARLags and VMALags are LagOp lag operator polynomials that describe the VAR and VMA components of the VARMA model.
%
% Convert the VARMA model to a VMA model by obtaining the coefficients of the truncated approximation of the infinite-lag polynomial. Specify to return at most 12 lagged terms.

numLags = 12;
VMA = arma2ma(VARLag,VMALag,numLags)

% VMA is a LagOP lag operator polynomial. All coefficients except those corresponding to lags 0, 4, 8, and 12 are 3-by-3 matrices of zeros.
%
% Display the nonzero coefficients of the resulting VMA model.

lag2Idx = VMA.Lags + 1; % Lags start at 0.  Add 1 to convert to indices.
vmaCoeff = toCellArray(VMA);

for j = 1:numel(lag2Idx)
    fprintf('___________Lag %d__________\n',lag2Idx(j) - 1)
    fprintf('%8.3f %8.3f %8.3f \n',vmaCoeff{lag2Idx(j)})
    fprintf    ('__________________________\n')
end

% Copyright 2015 The MathWorks, Inc.