| Date: | Sat, 27 Jul 1996 09:57:02 +0300 |
| Reply-To: | Dvora Zomer <epid04@POST.TAU.AC.IL> |
| Sender: | "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU> |
| From: | Dvora Zomer <epid04@POST.TAU.AC.IL> |
| Subject: | Re: autocorrelated random numbers-CORRECTION |
| In-Reply-To: | <vines.vq39+hyyvla@go01.comp.pge.com> |
|---|
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
===========================================================
Sorry for a misleading answer!
Ilya Novikov Ph.D.
Senior Statistician fax : 972-3-5348360
Dept. of Clinical Epidemiology voice: 972-3-5303256
The Chaim Sheba Medical Center mail : epid04@post.tau.ac.il
Tel Hashomer Israel 52621
On Fri, 19 Jul 1996 TWB2%Rates%FAR@GO50.COMP.PGE.COM wrote:
> Mark, Generating pseudo random numbers is not for people who have to post here
> for suggestions. Further, posting C questions here is a lot like posting
> construction questions to a medical list "because, after all, lots of you guys
> work in buildings." That said, assuming you have a good random number generator
> you can simulate first order autocorrelation in SAS as follows:
> DATA AUTOCORR(KEEP=STEP RESULT);
> VARIATE=RANNOR(123456);
> BIAS=.2*VARIATE;
> DO STEP=1 TO 100;
> VARIATE=RANNOR(123456);
> RESULT=(.8*VARIATE)+BIAS;
> OUTPUT;
> BIAS=.2*VARIATE;
> END;
> STOP;
> RUN;
>
> You can use a different random number function if you prefer. You can change
> the degree of correlation by changing the values .2 and .8. You could simulate
> second order correlation by keeping a few more values around. This code is a
> little odd--the _N_ counter will always be 1, rather than running from 1 to 100.
> Because the datastep only iterates once, there is no need to RETAIN values.
>
> Tim Berryhill - Contract Programmer and General Wizard
> TWB2@PGE.COM
> Frequently at Pacific Gas & Electric Co., San Francisco
> The correlation coefficient between their views and
> my postings is slightly less than 0
> ----------------------[Reply - Original Message]----------------------
>
> Sent by:Mark Sale <Marksale@AOL.COM>
> Dear users,
> Can anyone help me generate autocorrelated random numbers? That is if the
> mean of the numbers is 1 and the number at time x is 1.5234 the probability
> of the number at time x+1 being greater than 1 is > 0.5. I'd actually like a
> reference to a method, since I want to write (or download) some C code to do
> this.
> Thanks
> Mark Sale M.D.
> Assistant Professor of Medicine
> Georgetown Univerity
>
> =====================================================================
>
|