大標本理論に基づく仮説検定の有意水準を有限標本で評価(Box-Pierce and Ljung-Box)
プログラム
#include<oxstd.h>
main(){
decl cn,creject,crepeat;
decl dp,dq1,dq2;
decl vg,vr,vx;
decl i,j;
// cn: sample size, dp:# of autocorrelations
// crepeat: number of simulations
cn=50;dp=4;crepeat=50000;
vg=vr=zeros(dp+1,1);creject=zeros(2,1);
for(i=0;i<crepeat;++i){
dq1=dq2=0;
// vx=ranu(cn,1)-0.5;
// Use, for example, a normal distribution for x.
vx=rann(cn,1);vx=vx-meanc(vx);
// Autocovariance
for(j=0;j<dp+1;++j){vg[j]=(1/cn)*vx[j:cn-1]'*vx[0:cn-j-1];}
// Autocorrelation
vr=(1/vg[0])*vg;
// Box-Pierce Statistics
dq1=cn*sumsqrc(vr[1:dp]);
// Ljung-Box Statistics
for(j=1;j<dp+1;++j){dq2=dq2+vr[j]^2/(cn-j);}
dq2=cn*(cn+2)*dq2;
// Calculate p-value and test the H0:rho_1=...=rho_4=0
if(tailchi(dq1,dp) < 0.05){creject[0]+=1;}
if(tailchi(dq2,dp) < 0.05){creject[1]+=1;}
}
println("Comparison of Box-Pierce and Ljung-Box tests:",
" (Sample Size=",cn,")");
println(
"%r",{"Size"},
"%c",{"True","Box-Pierce","Ljung-Box"},
0.05~creject'/crepeat);
}
計算結果
Comparison of Box-Pierce and Ljung-Box tests: (Sample Size=50)
True Box-Pierce Ljung-Box
Size 0.050000 0.035580 0.050540