%% Test for Cointegration Using the Johansen Test
% This example shows how to assess whether a multivariate time series has multiple cointegrating relations using the Johansen test.
% Load Data_Canada into the MATLAB® Workspace. The data set contains the term structure of Canadian interest rates [81]. Extract the short-term, medium-term, and long-term interest rate series.

load Data_Canada
Y = Data(:,3:end); % Interest rate data

%
% To illustrate the input and output structure of jcitest when conducting multiple tests, test for the cointegration rank using the default H1 model and two different lag structures.

[h,pValue,stat,cValue] = jcitest(Y,'Model','H1','Lags',1:2);

% The default "trace" test assesses null hypotheses H(r) of cointegration rank less than or equal to r against the alternative H(n), where n is the dimension of the data. The summaries show that the first test rejects a cointegration rank of 0 (no cointegration) and just barely rejects a cointegration rank of 1, but fails to reject a cointegration rank of 2. The inference is that the data exhibit 1 or 2 cointegrating relationships. With an additional lag in the model, the second test fails to reject any of the cointegration ranks, providing little by way of inference. It is important to determine a reasonable lag length for the VEC model (as well as the general form of the model) before testing for cointegration.
% Because the Johansen method, by its nature, tests multiple rank specifications for each specification of the remaining model parameters, jcitest returns the results in the form of tabular arrays, and indexes by null rank and test number.
%
% Display the test results, h.

h

% Column headers indicate tests r0, r1, and r2, respectively, of H(0), H(1), and H(2) against H(3). Row headers t1 and t2 indicate the two separate tests (two separate lag structures) specified by the input parameters.
%
% Access the result for the second test at null rank r = 0 using tabular array indexing.

h20 = h.r0(2)

% Copyright 2012 The MathWorks, Inc.