ymhong / Computer Codes .gitee-modal { width: 500px !important; }

Explore and code with more than 8 million developers，Free private repositories ！：）
4. Hong and Li 2005 RFS.txt 10.04 KB
ymhong authored 2021-03-05 00:34 . first

/* PROGRAM TO COMPUTE TEST STATISTICS for any given Zee's */
/* using NEW ENTROPY PAPER: Hong and Li (2001) */

/* 5-30-2001 */
/* 6-14-2001 */
/* 9-06-2002 */

new;

/* CHANGES: */
/* CHANGE 1: THE SAMPLE SIZE,ALTERNATIVES(Zee,Zee2),... and the LOAD DATA FILE NAME */
/* CHANGE 2: statistics & output */

/* CHANGE 1: THE SAMPLE SIZE,ALTERNATIVES(Zee,Zee2),... and the LOAD DATA FILE NAME */
N=281;     /*   full sample size: 564 for daily; .... */
ALTER=2;   /*   ALTER=1 for Zee, ALTER=2 for Zee2 */
nDIM=3;    /*   ALWAYS: nDIM=3, since 3-dimensional affine models are only considered */

if ALTER==1; /* Zee */
NumCol=7;
load zdata[N,NumCol]=D:\HL\Z.txt; /* get NumCol-1 interest rate sample of size N */
else;       /* Zee2 */
NumCol=4;
load zdata[N,NumCol]=D:\HL\Z.txt; /* get NumCol-1 interest rate sample of size N */
endif;

rate=1;
do until rate==nDIM+2; /* rate == nDIM+1  refers to aggregated data */

if ALTER==1; /* Zee */
if rate==1;
data=zdata[.,3];
elseif rate==2;
data=zdata[.,5];
elseif rate==3;
data=zdata[.,7];
elseif rate==4; /* aggregated data */
alldata=zdata[.,3]~zdata[.,5]~zdata[.,7];
alltdata=alldata';
data=alltdata[.,1];
i=1;
do until i==N;
data=data|alltdata[.,i+1];
i=i+1;
endo;
N=N*nDIM;
endif;
else;        /* Zee2 */                                                                       		/* Zee2 */
if rate==1;
data=zdata[.,2];
elseif rate==2;
data=zdata[.,3];
elseif rate==3;
data=zdata[.,4];
elseif rate==4; /* aggregated data */
alldata=zdata[.,2]~zdata[.,3]~zdata[.,4];
alltdata=alldata';
data=alltdata[.,1];
i=1;
do until i==N;
data=data|alltdata[.,i+1];
i=i+1;
endo;
N=N*nDIM;
endif;
endif;

PROC k(u);
LOCAL Y;
Y=(15/16)*((1-u.*u)^2).*(abs(u).<=1.0);
RETP(Y);
ENDP;

/* Partial Integrals of u^L.*k(u) over interval [-1,b]: from -1 to b */

PROC Wk(l,b);
LOCAL Y;
Y=(15/16)*( (b^(l+1)-(-1)^(l+1))/(l+1) - 2*(b^(l+3)-(-1)^(l+3))/(l+3) + (b^(l+5)-(-1)^(l+5))/(l+5) );
RETP (Y);
ENDP;

/* LOCATION DEPENDENT KERNEL */
/* This modified kernel ensures that the integral of Kh(x,y,h) over y in [0,1] will be unity for any pair of (x,h) */
PROC Kh(x,y,h);
/* x = evaluation point, y = sample point (can be a column vector), h=bandwidth */
LOCAL Z,u;
u=(x-y)/h;
Z=(x.<h).*(1-x.>=h).*(1./h).*k(u)./Wk(0,x./h) + (x.>=h).*(1-x.>=h).*(1./h).*k(u) + (1-x.<h).*(x.>=h).*(1./h).*k(u)./Wk(0,(1-x)./h);
RETP(Z);
ENDP;

/* first method to compute variance */
/* ASYMPTOTIC VARIANCE OF TEST STATISTICS  (not depend on data) */

PROC VARFUNm1(w,u,v);
LOCAL Y;
Y=k(w+u).*k(u).*k(w+v).*k(v);
RETP(Y);
ENDP;

/* U1 is the support of kernel k(u) */

Let U1={1,
-1};
_INTREC=0;
_INTORD=20;

/* INTEGRAL OF [INT k0(w+u)k0(u)]^2 */

/* square below is due to bivariate density estimation */
/* 2 = factor from U-statistics */

Sigma2M1=2*Sigma2M1.*Sigma2M1;

/* standard deviation */

SigmaM1=sqrt(Sigma2M1);

/* first method to compute centering constant */

/* CENETRING CONSTANTS */

/* THIS IS integral of k(u)^2 from -1 to b */

PROC Wk2(b);
LOCAL Y;
Y=(15/16)^2*(b-(4/3).*b^3+(6/5).*b^5-(4/7).*b^7+(1/9).*b^9 - ((-1)-(4/3)*(-1)^3+(6./5)*(-1)^5-(4/7)*(-1)^7+(1/9)*(-1)^9));
RETP(Y);
ENDP;

/* main part (first part) of centering constant */

Center1M1=Wk2(1);

/* second part of centering constant: due to boundary kernel */

PROC CENFUNm1(b);
LOCAL Y;
Y=Wk2(b)./Wk(0,b)^2;
RETP(Y);
ENDP;

/* U2a is support of boundary index b */
/* U2b is support of k(u) */

Let U2a={1,
0};
Let U2b={1,
-1};
_INTREC=0;
_INTORD=20;

/* this is the total centering constant: it depends on bandwidth h  but doesn't depend on data*/

PROC CENTERm1(h);
LOCAL Y;
Y=(1./h-2).*Center1M1 + 2.*Center2M1;
Y=Y^2-1;
RETP(Y);
ENDP;

/* SEEDS NUMBERS */

iseed1=2555130;
iseed2=2660427;

X=data;

/* BANDWIDTH SELECTION */

SDx=STDC(X);

h=SDx.*N^(-1/6);

/* For bivariate pdf estimator : pair of vector (X(t),X(t-j)) */

PROC X1(j);
LOCAL Y;
Y=X[j+1:N,1];
RETP(Y);
ENDP;

PROC X2(j);
LOCAL Y;
Y=X[1:N-j,1];
RETP(Y);
ENDP;

/* bivariate pdf estimator at lag j */

PROC Fhat(x,y,j);
LOCAL Z;
Z=Kh(x,X1(j),h).*Kh(y,X2(j),h);
Z=meanc(Z);
RETP(Z');
ENDP;

/* Jmax= maximum lag order */

Jmax=20;

/* Matrices of test statistics: entropy, quadratic form, and Hellinger distance */

Itest=zeros(1,Jmax);
Qtest=Itest;
Htest=Itest;

/* Centering vectors  of 1 x Jmax */

CM1=Itest;
CM2=CM1;
CM3=CM1;

j=1;
do until j==Jmax+1;

/* log-likelihood function at lag j */

PROC Ijhat(x,y);
LOCAL Z;
Z=Fhat(x,y,j);
Z=ln(Z+(Z.==0));
/* (Z.==0) means that we exclude zero estimates */
RETP(Z);
ENDP;

/* Squared distance at lag j */

PROC Qjhat(x,y);
LOCAL Z;
Z=Fhat(x,y,j)-1;
Z=Z.*Z;
RETP(Z);
ENDP;

/* Squared distance of Hellinger at lag j */

PROC Hjhat(x,y);
LOCAL Z;
Z=sqrt(Fhat(x,y,j))-1;
Z=Z.*Z;
RETP(Z);
ENDP;

/* Method 1 to compute centering constant: asymptotic approach */

CM1[1,j]=CENTERm1(h);

/* Menthod 2 to compute centering constant: univariate data-driven approach */

PROC CENFUNm2(u);
LOCAL Y;
Y=Kh(u,X,h);
Y=meanc(Y.*Y);
RETP(Y');
ENDP;

Let U3={1,
0};
_INTREC=0;
_INTORD=20;

CM2[1,j]=(Cjhat.*Cjhat-1);

/* Method 3 to compute centering constant */
/* most accurate centering constant */

PROC CENFUNm3(u,v);
LOCAL Y;
Y=Kh(u,X1(j),h).*Kh(v,X2(j),h)-1;
Y=meanc(Y.*Y);
RETP(Y');
ENDP;

Let U3={1,
0};
Let  U4={1,
0};
_INTREC=0;
_INTORD=20;

CM3[1,j]=Cjhat;

/* Note: Entropy should have a negative sign */

Itest[1,j]=2*(N-j)*h *Ijtest;

Qtest[1,j]=(N-j)*h*Qjtest;

Htest[1,j]=4*(N-j)*h*Hjtest;

j=j+1;
endo;

/* Method 1: to compute variance: asumptotic approach */
/* Sigma has been given before the loop */

/* Method 2: data-driven method to compute variance for test statistic */
/* Variance estimator */

PROC VARFUNm2(u,v);
LOCAL Z;
Z=Kh(u,X,h).*Kh(v,X,h);
Z=meanc(Z);
Z=Z.*Z;
RETP(Z');
ENDP;

Let U3={1,
0};
Let U4={1,
0};
_INTREC=0;
_INTORD=20;

/* 2 = square */

/* saved for the next method */
Sigma2A=Sigma2M2;

/* Note to multiply h^2 */
Sigma2M2=2*h^2*(Sigma2M2^2-1);
SigmaM2=sqrt(Sigma2M2);

/* Method 3 to compute variance of tests */
/* alternative method to compute variance */

PROC VARFUNm3(u,v);
LOCAL Z;
Z=Kh(u,X,h).*Kh(v,X,h);
Z=meanc(Z);
RETP(Z');
ENDP;

Sigma2M3=2*h^2*(Sigma2A^2 - 2*Sigma2B^2 + 1);
SigmaM3=sqrt(Sigma2M3);

/* TEST STATISTICS */

/* individual tests */

/* USE THE FIRST METHOD TO COMPUTE MEAN CONSTANT */

Itest1=(Itest-h*CM1)./SigmaM1;
Qtest1=(Qtest-h*CM1)./SigmaM1;
Htest1=(Htest-h*CM1)./SigmaM1;

/* USE THE SECOND METHOD TO COMPUTE MEAN CONSTANT */

Itest2=(Itest-h*CM2)./SigmaM1;
Qtest2=(Qtest-h*CM2)./SigmaM1;
Htest2=(Htest-h*CM2)./SigmaM1;

/* USE THE 3rd METHOD TO COMPUTE THE MEAN */

Itest3=(Itest-h*CM3)./SigmaM1;
Qtest3=(Qtest-h*CM3)./SigmaM1;
Htest3=(Htest-h*CM3)./SigmaM1;

/* USE THE FIRST METHOD TO COMPUTE MEAN CONSTANT */

Itest4=(Itest-h*CM1)./SigmaM2;
Qtest4=(Qtest-h*CM1)./SigmaM2;
Htest4=(Htest-h*CM1)./SigmaM2;

/* USE THE SECOND METHOD TO COMPUTE MEAN CONSTANT */

Itest5=(Itest-h*CM2)./SigmaM2;
Qtest5=(Qtest-h*CM2)./SigmaM2;
Htest5=(Htest-h*CM2)./SigmaM2;

/* USE THE 3rd METHOD TO COMPUTE THE MEAN */

Itest6=(Itest-h*CM3)./SigmaM2;
Qtest6=(Qtest-h*CM3)./SigmaM2;
Htest6=(Htest-h*CM3)./SigmaM2;

/* USE THE FIRST METHOD TO COMPUTE MEAN CONSTANT */

Itest7=(Itest-h*CM1)./SigmaM3;
Qtest7=(Qtest-h*CM1)./SigmaM3;
Htest7=(Htest-h*CM1)./SigmaM3;

/* USE THE SECOND METHOD TO COMPUTE MEAN CONSTANT */

Itest8=(Itest-h*CM2)./SigmaM3;
Qtest8=(Qtest-h*CM2)./SigmaM3;
Htest8=(Htest-h*CM2)./SigmaM3;

/* USE THE 3rd METHOD TO COMPUTE THE MEAN */

Itest9=(Itest-h*CM3)./SigmaM3;
Qtest9=(Qtest-h*CM3)./SigmaM3;
Htest9=(Htest-h*CM3)./SigmaM3;

Itest =  Itest1~Itest2~Itest3~Itest4~Itest5~Itest6~Itest7~Itest8~Itest9;
Qtest=Qtest1~Qtest2~Qtest3~Qtest4~Qtest5~Qtest6~Qtest7~Qtest8~Qtest9;
Htest=Htest1~Htest2~Htest3~Htest4~Htest5~Htest6~Htest7~Htest8~Htest9;

/* Cumulative tests */

Itest05= sqrt(5)*meanc(Itest[.,1:5]');
Itest10=sqrt(10)*meanc(Itest[.,1:10]');
Itest15=sqrt(15)*meanc(Itest[.,1:15]');
Itest20=sqrt(20)*meanc(Itest[.,1:20]');

Qtest05= sqrt(5)*meanc(Qtest[.,1:5]'); 						/* This is what needed for affine */
Qtest10=sqrt(10)*meanc(Qtest[.,1:10]'); 						/* This is what needed for affine */
Qtest15=sqrt(15)*meanc(Qtest[.,1:15]'); 						/* This is what needed for affine */
Qtest20=sqrt(20)*meanc(Qtest[.,1:20]'); 						/* This is what needed for affine */

Htest05=sqrt(5)*meanc(Htest[.,1:5]');
Htest10=sqrt(10)*meanc(Htest[.,1:10]');
Htest15=sqrt(15)*meanc(Htest[.,1:15]');
Htest20=sqrt(20)*meanc(Htest[.,1:20]');

/* CHANGE 2: test statistics */
QtestTEMP=rate~Qtest1;

/* 1 x (Jmax+4) vector */

Itest=Itest~Itest05~Itest10~Itest15~Itest20;
Qtest=Qtest~Qtest05~Qtest10~Qtest15~Qtest20;
Htest=Htest~Htest05~Htest10~Htest15~Htest20;

SigmaM1~SigmaM2~SigmaM3~CM1[.,1]~CM2[.,1]~CM3[.,1:10];

Test=Itest~Qtest~Htest;

/* CHANGE 3: test statistics & output */

Wtest05= sqrt(5)*meanc(Qtest[.,1:5]'); 						    /* This is what needed for affine */
Wtest10=sqrt(10)*meanc(Qtest[.,1:10]'); 						/* This is what needed for affine */
Wtest15=sqrt(15)*meanc(Qtest[.,1:15]'); 						/* This is what needed for affine */
Wtest20=sqrt(20)*meanc(Qtest[.,1:20]'); 						/* This is what needed for affine */
Wtest=rate~Wtest05~Wtest10~Wtest15~Wtest20;
if rate==1;			 						/* This is what needed for affine */
Wtests=Wtest';
Qtests=QtestTEMP';
else;
Wtests=Wtests~Wtest';
Qtests=Qtests~QtestTEMP';
endif;

rate=rate+1;
endo;											/* End the cycle over different interest rates */

if ALTER==1; /* Zee */
output file=D:\HL\Wtests_Z.txt reset;
Wtests;
output off;
output file=D:\HL\Qtests_Z.txt reset;
Qtests;
output off;
else;       /* Zee2 */
output file=D:\HL\Wtests_Z2.txt reset;
Wtests;
output off;
output file=D:\HL\Qtests_Z2.txt reset;
Qtests;
output off;
endif;


R
1
https://gitee.com/ymhong/code.git
git@gitee.com:ymhong/code.git
ymhong
code
Computer Codes
master