/* Lag k serial correlation test for U(0,1) sequence */ new; cls; x=rndu(100,1); call scorrtest(x,1); call scorrtest(x,2); call scorrtest(x,3); call scorrtest(x,4); proc scorrtest(x,k); local n,nk,x1,x2,r,Z,pval; if ismiss(x); errorlog "Warning: missing data found."; x=packr(x); endif; if not(x>=0 and x<=1); errorlog "Error: This is not U(0,1)."; retp("."); endif; n=rows(x); x1=x[1:n-k]; x2=x[k+1:n]; nk=n-k; r=(1/nk*sumc(x1.*x2)-meanc(x1)^2)/(1/nk*sumc((x-meanc(x1))^2)); Z=sqrt(nk)*r/sqrt(13); pval=2*(1-cdfn(abs(Z))); /* results */ print "Serial Correlation Test(lag k):"; print/rz "n =" n;; print/rz " (n-k)=" n-k; print/rz "k =" k; print "r(k)=" r; print "Z =" Z; print "pval=" pval; /* graph */ library pgraph; graphset; pqgwin auto; _pcross=1; _plctrl=-1; title("lag"$+ftocv(k,1,0)); xy(x1,x2); retp(x1); endp;