BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kzepilos
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
7 REPLIES 7
FriedEgg
SAS Employee

So, you want to calculate the conditional mean?

Reeza
Super User

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?

kzepilos
Calcite | Level 5

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.

Ksharp
Super User

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

kzepilos
Calcite | Level 5

Thanks alot for your help. It definitely helped!

DLing
Obsidian | Level 7

There's no need to hunt for the correlation.  It is generated through this process:

  1. Generate two separate random normal variables.  call them x1, x2.
  2. If you want "drawn from a population with correlation R", skip this.  If you want the drawn sample to have correlation R, then do this step:  put the two columns through principle components (PROC PRINCOMP or PROC FACTOR) to generate two factors that are completely uncorrelated.  The reason is that x1, x2 from above might have non-zero correlation.
  3. Use this formula:  Y = x1 * R + x2 * sqrt( 1 - R**2 ).  Y will be correlated with x1 with exactly correlation R.

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;

kzepilos
Calcite | Level 5

Thanks DLing!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1279 views
  • 7 likes
  • 5 in conversation