%% Convert Structural VEC Model to VAR Model Using Lag Operator Polynomials
% Consider converting the following structural VEC(1) model to a structural VAR(2) model.
% \left[ {\begin{array}{*{20}{c}}
% {0.54}&{ - 2.26}\\
% {1.83}&{0.86}
% \end{array}} \right]\Delta {y_t} = \left[ {\begin{array}{*{20}{c}}
% {-0.07}&{-0.07}\\
% {0.01}&{0.02}
% \end{array}} \right]\Delta {y_{t - 1}} + \left[ {\begin{array}{*{20}{c}}
% { - 0.15}&{1.9}\\
% { - 3.15}&{ - 0.54}
% \end{array}} \right]{y_{t - 1}} + {\varepsilon _t}.
% Specify the coefficient matrices B_0 and B_1, and the error-correction coefficient C.

B0 = [0.54 -2.26;
      1.83  0.86];
B1 = [-0.07  -0.07
     0.01 0.02];
C = [-0.15  1.9;
     -3.15  -0.54];

%
% Pack the matrices into separate cells of a 3-dimensional cell vector. Put B0 into the first cell and B1 into the second cell. Negate the coefficients corresponding to all nonzero differenced lag terms.

VECCoeff = {B0; -B1};

%
% Create a lag operator polynomial that encompasses the autoregressive terms in the VEC(2) model.

VEC = LagOp(VECCoeff)

% VEC is a LagOp lag operator polynomial, and specifies the autoregressive lag operator polynomial in this equation
% (B_0 - B_1L)\Delta y_t = Cy_{t-1} + \varepsilon_t.
% L is the lag operator. If you expand the quantity and solve for  \Delta y_t, then the result is the VAR(2) model in difference-equation notation.
%
% Compute the coefficient matrices of the equivalent VAR(2) model.

VAR = vec2var(VEC,C)

% VEC.Coefficients{0} is A_0, the coefficient matrix of y_t. Subsequent elements in VAR.Coefficients correspond to subsequent lags in VEC.Lags.
% VAR is the VAR(2) equivalent of the VEC(1) model. Because the VEC(1) model is structural, the equivalent VAR(2) is as well. That is, VAR.Coefficients{0} is the coefficient of y_{t}, and subsequent elements correspond to subsequent lags in VAR.Lags.
%
% Display the VAR(2) model coefficients in difference-equation notation.

A0 = VAR.Coefficients{0}
A1 = -VAR.Coefficients{1}
A2 = -VAR.Coefficients{2}

% The resulting VAR(3) model is
% \left[ {\begin{array}{*{20}{c}}
% {0.54}&{ - 2.26}\\
% {1.83}&{0.86}
% \end{array}} \right]{y_t} = \left[ {\begin{array}{*{20}{c}}
% {0.32}&{ - 0.43}\\
% { - 1.31}&{0.34}
% \end{array}} \right]{y_{t - 1}} + \left[ {\begin{array}{*{20}{c}}
% {0.07}&{0.07}\\
% { - 0.01}&{ - 0.02}
% \end{array}} \right]{y_{t - 2}} + {\varepsilon _t}.
%
% Alternatively, reflect the lag operator polynomial VAR around lag 0 to obtain the difference-equation notation coefficients.

DiffEqnCoeffs = reflect(VAR);
A = toCellArray(DiffEqnCoeffs);
A{1} == A0
A{2} == A1
A{3} == A2

% Both methods produce the same coefficients.
% Copyright 2015 The MathWorks, Inc.