% Define: integrated, cointegrated

% Modeling: VARs and VECs. VECs allow introduction of long-term dynamics to
% the short-term dynamics of the VAR through CI analysis. 

% See “Discussion” section in Johansen’s  1991 “Estimation and Hypothesis Testing of Cointegration Vectors in Gaussian Vector Autoregressive Models”
% 
% addresses three issues: 
% first) the problem of finding the number of cointegrating relations in nonstationary data
% next) the problem of estimating the cointegrating relations
% finally)testing interesting economic hypotheses about their structure 


clear; clc; close all

%% EGCITEST

  % Data on the term structure of interest rates in Canada:

  load Data_Canada
  Y = Data(:,3:end);
  names = series(3:end);
  plot(dates,Y)
  legend(names,'location','NW')
  grid on

  % Test for cointegration (and reproduce row 1 of Table II in [3]):

  [h,pValue,stat,cValue,reg] = egcitest(Y,'test',{'t1','t2'});

  % Plot the estimated cointegrating relation y1-Y2*b-X*a:

  a = reg(2).coeff(1);
  b = reg(2).coeff(2:3);
  plot(dates,Y*[1;-b]-a)
  grid on
  
%% All EGCI tests (permute Y)

P0 = perms([1 2 3]); % All regression orders
[~,idx] = unique(P0(:,1)); % Rows with unique regressand y1
P = P0(idx,:); % Unique regression orders
numPerms = size(P,1);

T0 = size(Y,1);
CIR = zeros(T0,numPerms);

for i = 1:numPerms
    
    YPerm = Y(:,P(i,:));
    [~,~,~,~,reg] = egcitest(YPerm);
    a = reg.coeff(1);
    b = reg.coeff(2:3);
    CIR(:,i) = YPerm*[1;-b]-a;
    
end

figure
plot(dates,CIR)
legend(strcat({'Cointegrating relation  '},num2str((1:numPerms)')),'location','NW');
grid on

% Every pair is identical--just reverses b coefficients

% Evidence for cointegration is stronger with R_m or R_l the regressand

% Test CIRs for stationarity

% Residuals plots = CIR plots

% No alpha estimation; error-correction modeling via res (see note)

%% JCITEST

  % Data on the term structure of interest rates in Canada:

  load Data_Canada
  Y = Data(:,3:end);
  names = series(3:end);
  plot(dates,Y)
  legend(names,'location','NW')
  grid on

  % Test for cointegration:

  [h,pValue,stat,cValue,mles] = jcitest(Y,'model','H1');

  % Plot the estimated cointegrating relations B'*y(t-1)+c0:
  
  YLag = Y(2:end,:);
  T = size(YLag,1);
  B = mles.r2.paramVals.B;
  c0 = mles.r2.paramVals.c0;  
  plot(dates(2:end),YLag*B+repmat(c0',T,1))
  grid on
  
%% Compare J and EG CI relations

[h,pValue,stat,cValue,reg] = egcitest(Y,'test','t2');
a = reg.coeff(1);
b = reg.coeff(2:3);

[h,pValue,stat,cValue,mles] = jcitest(Y,'model','H1*'); % Use H1* to replicate EG 'c'
B = mles.r2.paramVals.B;
c0 = mles.r2.paramVals.c0;
% Normalize 2nd J CIR wrt 1st variable:
B2n = B(:,2)/B(1,2);
c02n = c0(2)/B(1,2);
  
figure
hold on
plot(dates,Y*[1;-b]-a)
plot(dates,Y*B2n+c02n,'r-*')
grid on
legend('Engle-Granger OLS Estimate','Johansen MLE','Location','NW')
title('{\bf Cointegrating Relations}')

% 1st J CIR different from any EG CIR

% J Residuals plots ~= CIR plots

% Perms of Y result in same J CIRs

% No normalization (wrt any variable) makes 1st J CIR replicate any EG CIR:
figure
hold on
plot(dates,CIR)
B1n = B(:,1)/B(1,1);
c01n = c0(1)/B(1,1);
plot(dates,Y*B1n+c01n,'r-*')

figure
hold on
plot(dates,CIR)
B1n = B(:,1)/B(2,1);
c01n = c0(1)/B(2,1);
plot(dates,Y*B1n+c01n,'r-*')

figure
hold on
plot(dates,CIR)
B1n = B(:,1)/B(3,1);
c01n = c0(1)/B(3,1);
plot(dates,Y*B1n+c01n,'r-*')

% CIRs are not unique!

%% JCONTEST

% Data on Australian and U.S. prices:

load Data_JAustralian
p1 = Dataset.PAU; % Log Australian Consumer Price Index
p2 = Dataset.PUS; % Log U.S. Consumer Price Index
s12 = Dataset.EXCH; % Log AUD/USD Exchange Rate
Y = [p1 p2 s12];
plot(dates,Y)
datetick('x','yyyy')
legend(series(1:3),'Location','Best')
grid on

% Pretest the individual series for stationarity:

[h0,pValue0] = jcontest(Y,1,'BVec',{[1 0 0]',[0 1 0]',[0 0 1]'})

% Test for cointegration:

[h1,pValue1] = jcitest(Y)

% Test if p1 = p2 + s12 (purchasing power parity):

[h2,pValue2] = jcontest(Y,1,'BCon',[1 -1 -1]')

% Use imagesc(abs(C-CHatBar));colorbar; with sims like JCONSimTest1

%% Multicointegration & Hybrid systems (I(1) & I(0))

% Add stationary series to data and test it for a unit root

% Focus on flexibility of "Johansen Framework"

%% Graph size and power of tests