09-14-2011 06:06 PM
Hello. I am trying to simulate in sas about a case where two randomly generated normal distributions (say X and Y with 1000 observations)
have correlation of 0.5 (corr(X,Y)=0.5).
I know that corr(X,Y)= cov(X,Y)/(stdX*stdY)
Now my question is how do you figure out E(XY)?
Thanks for the help in advance!
09-14-2011 06:26 PM
I might be missing something but from the simulation, multiply each x*y and take the average?
Or do you need an explicit mathematical formula?
09-14-2011 06:59 PM
the main purpose of this simulation is to generate two normal distributions with correlation of 0.5 using the random number generator rannor in SAS.
Since two normal dist. are not independent I don't know how i can come up with the joint density function of f(x,y) in order to figure out E(XY). Any other ideas?
Thanks for the help.
09-15-2011 01:01 AM
You do not need to figure out E(XY), there is a formula for calculating correlation coefficient.
You can hard code to get that .I used Standard Normal Distribution N(0,1), but It looks like hard to get cov=0.5.
So I set it cov=0.1
%macro corr; %do %until(&found eq Y); data normal(drop=i); do i=1 to 1000; x=rannor(0); y=rannor(0); output; end; run; proc corr data=normal outp=corr(where=(_type_='CORR')) noprint ; var x; with y; run; %let found=N; data _null_; set corr; if round(x,.1) eq .1 then call symputx('found','Y'); run; %end; %mend corr; %corr
09-15-2011 03:00 PM
There's no need to hunt for the correlation. It is generated through this process:
For more variables, in general, take the components matrix and post multiply by Cholesky decomposition of R, the correlation matrix.
call streaminit( 123 );
do i = 1 to 1000;
x1 = rand('normal');
x2 = rand('normal');
proc princomp data=test out=pc;
var x1 x2;
yr = x1 * &r + x2 * sqrt( 1 - (&r)**2 );
yp = prin1 * &r + prin2 * sqrt( 1 - (&r)**2 );
var x1 x2 yr prin1 prin2 yp;
Need further help from the community? Please ask a new question.