%% Determine Cointegration Rank of VEC Model
% This example shows how to convert an n-dimensional VAR model to a VEC model, and then compute and interpret the cointegration rank of the resulting VEC model.
% The rank of the error-correction coefficient matrix, C, determines the cointegration rank. If rank(C) is:
% Zero, then the converted VEC(p) model is a stationary VAR(p - 1) model in terms of \Delta y_t, without any cointegration relations.
% n, then the VAR(p) model is stable in terms of y_t.
% The integer r such that 0 < r < n, then there are r cointegrating relations. That is, there are r linear combinations that comprise stationary series. You can factor the error-correction term into the two n-by- r matrices C = \alpha\beta^\prime.  \alpha contains the adjustment speeds, and \beta the cointegration matrix. This factorization is not unique.
% For more details, see Cointegration and Error Correction and [80], Chapter 6.3.
% Consider the following VAR(2) model.
% {y_t} = \left[ {\begin{array}{*{20}{c}}
% 1&{0.26}&0\\
% { - 0.1}&1&{0.35}\\
% {0.12}&{ - 0.05}&{1.15}
% \end{array}} \right]{y_{t - 1}} + \left[ {\begin{array}{*{20}{c}}
% { - 0.2}&{ - 0.1}&{ - 0.1}\\
% {0.6}&{ - 0.4}&{ - 0.1}\\
% { - 0.02}&{ - 0.03}&{ - 0.1}
% \end{array}} \right]{y_{t - 2}} + {\varepsilon _t}.
% Create the variables A1 and A2 for the autoregressive coefficients. Pack the matrices into a cell vector.

A1 = [1 0.26 0; -0.1 1 0.35; 0.12 -0.5 1.15];
A2 = [-0.2 -0.1 -0.1; 0.6 -0.4 -0.1; -0.02 -0.03 -0.1];
Var = {A1 A2};

%
% Compute the autoregressive and error-correction coefficient matrices of the equivalent VEC model.

[Vec,C] = var2vec(Var);

% Because the degree of the VAR model is 2, the resulting VEC model has degree q = 2 - 1. Hence, Vec is a one-dimensional cell array containing the autoregressive coefficient matrix.
%
% Determine the cointegration rank by computing the rank of the error-correction coefficient matrix C.

r = rank(C)

% The cointegrating rank is 2. This result suggests that there are two independent linear combinations of the three variables that are stationary.
% Copyright 2015 The MathWorks, Inc.