%% Convert VARMA Model to VEC Model
% Approximate the coefficients of the VEC model that represents this stationary and invertible VARMA(8,4) model that is in lag operator form
% \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]'.
% Create a cell vector containing the VAR coefficient matrices. Start with the coefficient of y_t, and then 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 coefficient matrices. Start with the coefficient of \varepsilon_t, and then 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(8) and VMA(4) components of the VARMA(8,4) model.

VARLag = LagOp(VAR0,'Lags',var0Lags);
VMALag = LagOp(VMA0,'Lags',vma0Lags);

% VARLag and VMALag are LagOp lag operator polynomials that describe the VAR and VMA components of the VARMA model.
%
% Convert the VARMA(8,4) model to a VAR(p) model by obtaining the coefficients of the truncated approximation of the infinite-lag polynomial. Set numLags to return at most 12 lagged terms.

numLags = 12;
VAR = arma2ar(VARLag,VMALag,numLags)

% VAR 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. The coefficients in VAR.Coefficients comprise a structural VAR(12) model approximation of the original VARMA(8,4) model.
%
% Compute the coefficients of the VEC(11) model equivalent to the resulting VAR(12) model.

[VEC,C] = var2vec(VAR)

% VEC is a LagOp lag operator polynomial containing the coefficient matrices of the resulting VEC(11) model in VEC.Coefficients. VEC.Coefficients{0} is the coefficient of \Delta y_t, Vec{1} is the coefficient of \Delta y_{t-1}, and so on.
%
% Display the nonzero coefficients of the resulting VEC model.

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

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

% Copyright 2015 The MathWorks, Inc.