%% Convert VAR Model to VEC Model Using Cell Arrays
% Consider converting the following VAR(3) model to a VEC(2) model.
% {y_t} = \left[ {\begin{array}{*{20}{c}}
% {0.5}\\
% {1}\\
% { - 2}
% \end{array}} \right] + \left[ {\begin{array}{*{20}{c}}
% {0.54}&{0.86}&{ - 0.43}\\
% {1.83}&{0.32}&{0.34}\\
% { - 2.26}&{ - 1.31}&{3.58}
% \end{array}} \right]{y_{t - 1}} + \left[ {\begin{array}{*{20}{c}}
% {0.14}&{ - 0.12}&{0.05}\\
% {0.14}&{0.07}&{0.10}\\
% {0.07}&{0.16}&{0.07}
% \end{array}} \right]{y_{t - 3}} + {\varepsilon _t}.
% Specify the coefficient matrices (A_1, A_2, and A_3) of the VAR(3) model terms y_{t-1}, y_{t-2}, and y_{t-3}.

A1 = [0.54  0.86 -0.43;
      1.83  0.32  0.34;
     -2.26 -1.31  3.58];
A2 = zeros(3); 
A3 = [0.14 -0.12 0.05;
      0.14  0.07 0.10;
      0.07  0.16 0.07];

%
% Pack the matrices into separate cells of a 3 dimensional cell vector. Put A1 into the first cell, A2 into the second cell, and A3 into the third cell.

VAR = {A1 A2 A3};

%
% Compute the coefficient matrices of \Delta y_{t-1} and \Delta y_{t-2}, and error-correction coefficient matrix of the equivalent VEC(2) model.

[VEC,C] = var2vec(VAR);
size(VEC)

% The specification of a cell array of matrices for the input argument indicates that the VAR(3) model is a reduced-form model expressed as a difference equation. VAR{1} is the coefficient of y_{t-1}, and subsequent elements correspond to subsequent lags.
% VEC is a 1-by-2 cell vector of 3-by-3 coefficient matrices for the VEC(2) equivalent of the VAR(3) model. Because the VAR(3) model is in reduced form, the equivalent VEC model is also. That is, VEC{1} is the coefficient of \Delta y_{t-1}, and subsequent elements correspond to subsequent lags. The orientation of VEC corresponds to the orientation of VAR.
%
% Display the VEC(2) model coefficients.

B1 = VEC{1}
B2 = VEC{2}
C

% Since the constant offsets between the models are equivalent, the resulting VEC(2) model is
% \begin{array}{rcl}\Delta {y_t} &=& \left[ {\begin{array}{*{20}{c}}
% {0.5}\\
% {1}\\
% { - 2}
% \end{array}} \right] + \left[ {\begin{array}{*{20}{c}}
% { - 0.14}&{0.12}&{ - 0.05}\\
% { - 0.14}&{ - 0.07}&{ - 0.10}\\
% { - 0.07}&{ - 0.16}&{ - 0.07}
% \end{array}} \right]\Delta {y_{t - 1}} + \left[ {\begin{array}{*{20}{c}}
% { - 0.14}&{0.12}&{ - 0.05}\\
% { - 0.14}&{ - 0.07}&{ - 0.10}\\
% { - 0.07}&{ - 0.16}&{ - 0.07}
% \end{array}} \right]\Delta {y_{t - 2}}\\ &+& \left[ {\begin{array}{*{20}{c}}
% { - 0.32}&{0.74}&{ - 0.38}\\
% {1.97}&{ - 0.61}&{0.44}\\
% { - 2.19}&{ - 1.15}&{2.65}
% \end{array}} \right]{y_{t - 1}} + {\varepsilon _t}\end{array}.
% Copyright 2015 The MathWorks, Inc.