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)
where cov(X,Y)=E(XY)-E(X)E(Y).
Now my question is how do you figure out E(XY)?
Thanks for the help in advance!
Thanks DLing!
So, you want to calculate the conditional mean?
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?
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.
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
Ksharp
Thanks alot for your help. It definitely helped!
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.
%let r=0.5;
data test;
call streaminit( 123 );
do i = 1 to 1000;
x1 = rand('normal');
x2 = rand('normal');
output;
end;
drop i;
run;
proc princomp data=test out=pc;
var x1 x2;
run;
data test1;
set pc;
yr = x1 * &r + x2 * sqrt( 1 - (&r)**2 );
yp = prin1 * &r + prin2 * sqrt( 1 - (&r)**2 );
run;
proc corr;
var x1 x2 yr prin1 prin2 yp;
run;
Thanks DLing!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.