%% Convert Structural VEC model to VMA Model
% Approximate the coefficients of the structural VMA model that represents the structural VEC(8) 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\}{\Delta y_t} = \\
% \left[ {\begin{array}{*{20}{c}}
% { - 0.02}&{ 0.03}&{0.3}\\
% {0.05}&{0.1}&{0.01}\\
% { 0.3}&{0.01}&{0.01}
% \end{array}} \right]y_{t-1} + {\varepsilon _t}\end{array}
% where \Delta y_t = \left[\Delta y_{t,1}\;\;\;\Delta y_{t,2}\;\;\;\Delta y_{t,3}\right]', \varepsilon_t = \left[\varepsilon_{1t}\;\;\;\varepsilon_{2t}\;\;\;\varepsilon_{3t}\right]', and, for j = 1,2, and 3, \Delta y_{t,j} = y_{t,j} - y_{t-1,j}.
% Create a cell vector containing the VEC(8) model coefficient matrices. Start with the coefficient of \Delta 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.

VEC0 = {[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]};
vec0Lags = [0 4 8];
C = [-0.02 0.03 0.3; 0.05 0.1 0.01; 0.3 0.01 0.01];

%
% vec2var requires a LagOp lag operator polynomial for an input argument that comprises a structural VEC(8) model. Construct a LagOp lag operator polynomial that describes the VEC(8) model autoregressive coefficient matrix component (i.e., the coefficients of \Delta y_t and its lags).

VECLag = LagOp(VEC0,'Lags',vec0Lags);

% VECLag is a LagOp lag operator polynomial that describes the autoregressive component of the VEC(8) model.
%
% Compute the coefficients of the VAR(9) model equivalent to the VEC(8) model.

VAR = vec2var(VECLag,C)

% VAR is a LagOp lag operator polynomial. All coefficients except those corresponding to lags 0, 1, 4, 5, 8, and 9 are 3-by-3 matrices of zeros. The coefficients in VAR comprise a stable, structural VAR(9) model equivalent to the original VEC(8) model. The model is stable because the error-correction coefficient has full rank.
%
% Compute the coefficients of the VMA model approximation to the resulting VAR(9) model. Set numLags to return at most 12 lags.

numLags = 12;
VMA = arma2ma(VAR,[],numLags);

% VMA is a LagOp lag operator polynomial containing the coefficient matrices of the resulting VMA(12) model in VMA.Coefficients.  VMA{0} is the coefficient of \varepsilon_t, VMA{1} is the coefficient of \varepsilon_{t-1}, and so on.
% Copyright 2015 The MathWorks, Inc.