clc;
clear;
load oil;
Y1=x(:,1:2); % SC and WTI
Y2=x(:,[2,1]); % WTI and SC
Y3=x(:,[1,3]); % SC and Brent
Y4=x(:,[3,1]); % Brent and SC
Y5=x(:,[2,3]); % WTI and Brent
Y6=x(:,[3,2]); % Brent and WTI
Y = Y3;
% [h,pValue,stat,cValue,mles] = jcitest(Y1);
% [h,pValue,stat,cValue,mles] = jcontest(Y,1,'ACon','ACon');
[h,~,~,~,reg1,reg2] = egcitest(Y); %
c0 = reg1.coeff(1); % constant term
b = reg1.coeff(2);
beta = [1;-b];
q = 1; % the lag order of VECM model
[numObs,numDims] = size(Y);
tBase = (q+2):numObs; % Commensurate time base, all lags
T = length(tBase); % Effective sample size
YLags = lagmatrix(Y,0:(q+1)); % Y(t-k) on observed time base
LY = YLags(tBase,(numDims+1):2*numDims); % Y(t-1) on commensurate time base
% Form multidimensional differences so that the numDims-wide block of columns in DelatYLags contains (1-L)Y(t-k+1).
DeltaYLags = zeros(T,(q+1)*numDims);
for k = 1:(q+1)
DeltaYLags(:,((k-1)*numDims+1):k*numDims) = ...
YLags(tBase,((k-1)*numDims+1):k*numDims) ...
- YLags(tBase,(k*numDims+1):(k+1)*numDims);
end
DY = DeltaYLags(:,1:numDims); % (1-L)Y(t)
DLY = DeltaYLags(:,(numDims+1):end); % [(1-L)Y(t-1),...,(1-L)Y(t-q)]
% Regress the first difference of the series onto the q lagged differences and the estimated cointegration term. Include an intercept in the regression.
X = [(LY*beta-c0),DLY,ones(T,1)]; % when Ax=b��then x=A\b
P = (X\DY)'; % [alpha,B1,...,Bq,c1] ,DY=X*P'
% Display the VEC model coefficients.
alpha = P(:,1); %
B1 = P(:,2:3); % lag 1 order in VECM model
% B2 = P(:,4:5); % lag 2 order in VECM model
% B3 = P(:,6:7); % lag 3 order in VECM model
c1 = P(:,end); % constant term
% Residual term
epsilon = DY-X*P';
% standard deviation
sigma = std(epsilon);
% correlation matrix
rho = corrcoef (epsilon);
% correlation
rho1 = rho(1,2);
评论3