游程检验

*4)市场的有效性检验—游程检验:
1) 对分笔成交价格的变化进行计数。当发生持续同方向变化时我们记这些持续的变化为一次连续;
2) 统计样本内正向连续和负向连续的累积次数u。统计样本内正向变化次数n1和负向变化的次数n2。
3) 如果价格变化是随机的,则发生连续的次数的期望值为:e=(2*n1*n2)/(n1+n2)+1,
s=sqrt((2*n1*n2(2*n1*n2-n1-n2)/((n1+n2)^2*(n1+n2-1)))
4)利用样本的观测值和其期望值及标准差,我们检测样本的随机性。测试统计量为:z=(abs(u-e)-0.5)/s.
*;

data runcount;
   keep runs numpos numneg n;
   set test1 nobs=nobs end=last;
   retain runs 0 numpos 0;
       prevpos=( lag(dif_price) GE 0 );
       currpos=(dif_price  GE 0 );
   if currpos and prevpos then numpos+1;
   else if currpos and ^prevpos then do;
       runs+1;
       numpos+1;
       end;
   else if ^currpos and prevpos then runs+1;
   if last then do;
      numneg=nobs-numpos;
      n=nobs;
      output;
   end;     
run;

data waldwolf;
   label z='Wald-Wolfowitz Z'
         pvalue='Pr > |Z|';
   set runcount;
   mu = ( (2*numpos*numneg) / (numpos+numneg) ) + 1;
   sigmasq = ( (2*numpos*numneg) * (2*numpos*numneg-numneg-numpos) ) /
             ( ( (numpos+numneg)**2 ) * (numpos+numneg-1) );
   sigma=sqrt(sigmasq);
   drop sigmasq;
      
   if N GE 50 then Z = (Runs - mu) / sigma;
   else if Runs-mu LT 0 then Z = (Runs-mu+0.5)/sigma;
   else Z = (Runs-mu-0.5)/sigma;
   pvalue=2*(1-probnorm(abs(Z)));
run;
  
title  'Wald-Wolfowitz Test for Randomness';
title2 'H0: The data are random';
proc print data=waldwolf label noobs;
    var z pvalue;
    format pvalue pvalue.;
run;

详情参考:
http://support.sas.com/kb/33/092.html

    原文作者:游程编码问题
    原文地址: https://blog.csdn.net/yugao1986/article/details/8000026
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞