Date: Sat, 27 Jul 1996 09:57:02 +0300 Dvora Zomer "SAS(r) Discussion" Dvora Zomer Re: autocorrelated random numbers-CORRECTION

Sorry! It was a bad idea to run through more than 250 letters in two hours at the very end of a week! In my answer I wrote: ======================================== Dear Mark: In his answer to your question Tim Berryhill showed a program that will generate autocorrelated numbers. Unfortunately, he did not said how to choose coefficient 'a' and 'b' in the main equation x(i+1)=a*x(i)+b*variate for getting predefined correlation 'r'. The answer is: a=r/(1+r**2) b=1/(1+r**2) ; !!!!!!!!!!!!!!!!!!!!!!!!!THIS IS WRONG!!!!!!!!!!!!!!!!!!!!!!!!!!!!! THE CORRECT ANSWER IS: if 'x(i)' and 'variate' are independent standard Gauss , then a=r, b=sqrt(1-r**2) .

Thus for generating two series with predefined auto- and crosscorrelations you can use the following .

/* problem: generate two sequences {x(i)} and {y(i)} so, that Var(x)=Var(y)=1, Cov(x(i),x(i+1))=a, Cov(y(i),y(i+1))=b, Cov(x(i),y(i))=c

Solution: +++++++++++++++++++++++++++++++++++ */ data a (replace=yes keep= x y ); /* N, a,b,c - just for example */ N=20000 ; a=0.2 ; b=0.4 ; c=0.7 ; /*.............................. */ ka=sqrt(1-a**2) ; kb=sqrt(1-b**2) ; kc=sqrt(1-c**2) ; m=c*(1-a*b)/ka/kb ; if abs(m)>=1 then abort return ; km=sqrt(1-m**2) ; /* the first values */ x=rannor(789054) ; y0=rannor(123456) ; y=a*x+ka*y0 ; output ; do i=2 to N ; addx=rannor(123456) ; addy0=rannor(123456) ; addy=m*addx+km*addy0 ; x=a*x+ka*addx ; y=b*y+kb*addy ; output ; end ; run ; data b; set a ; retain x2 y2 ; output ; x2=x ; y2=y ; run; proc corr pearson ; var x2 x y2 y ; run; ========================= Execution of this program will give:

CORRELATION ANALYSIS

4 'VAR' Variables: X2 X Y2 Y

Simple Statistics

Variable N Mean Std Dev Sum

X2 19999 0.0003744 1.00461 7.48734 X 20000 0.0004076 1.00460 8.15103 Y2 19999 0.01347 1.00340 269.32089 Y 20000 0.01343 1.00339 268.66632

Simple Statistics

Variable Minimum Maximum

X2 -4.07777 3.98698 X -4.07777 3.98698 Y2 -3.74807 4.17530 Y -3.74807 4.17530

CORRELATION ANALYSIS

Pearson Correlation Coefficients / Prob > |R| under Ho: Rho=0 / Number of Observations

X2 X Y2 Y

X2 1.00000 0.19924 0.69619 0.28383 0.0 0.0001 0.0 0.0 19999 19999 19999 19999

X 0.19924 1.00000 0.13096 0.69615 0.0001 0.0 0.0001 0.0 19999 20000 19999 20000

Y2 0.69619 0.13096 1.00000 0.40048 0.0 0.0001 0.0 0.0 19999 19999 19999 19999

Y 0.28383 0.69615 0.40048 1.00000 0.0 0.0 0.0 0.0 19999 20000 19999 20000 ===========================================================