Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Generation of correlated random numbers

Subject: Generation of correlated random numbers

From: Elena

Date: 6 Feb, 2009 14:39:25

Message: 1 of 8

Hi!
I wanna do a very simple thing but I don't know how yo do it...
I want to generate some values for 2 random variables (with known distribution, mean values and standard deviations) but the random variables are not independent. If for example the correlation of these variables is ρ=0.7 , how can I define this correlation??

Thank u!

Subject: Generation of correlated random numbers

From: Roger Stafford

Date: 8 Feb, 2009 04:46:02

Message: 2 of 8

Elena <elena.k29@hotmail.com> wrote in message <30956643.1233931196387.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hi!
> I wanna do a very simple thing but I don't know how yo do it...
> I want to generate some values for 2 random variables (with known distribution, mean values and standard deviations) but the random variables are not independent. If for example the correlation of these variables is ρ=0.7 , how can I define this correlation??
>
> Thank u!

  If you have the Statistics Toolbox, use 'mvnrnd'. The needed covariance matrix would be

 SIGMA = [s1^2,p*s1*s2;p*s1*s2,s2^2]

where s1 and s2 are the specified variances and p the correlation. The needed MU consists of the two specified mean values, m1 and m2.

  If you don't have that toolbox, just do this:

 u = randn(1,n);
 v = randn(1,n);
 x = s1*u+m1;
 y = s2*(p*u+sqrt(1-p^2)*v)+m2;

Then x and y are each n-element row vectors having the required joint distributions.

  I have assumed here that you intended that these random variables be jointly normally distributed.

Roger Stafford

Subject: Generation of correlated random numbers

From: Peter Perkins

Date: 9 Feb, 2009 16:48:15

Message: 3 of 8

Elena wrote:

> I want to generate some values for 2 random variables (with known distribution, mean values and standard deviations) but the random variables are not independent. If for example the correlation of these variables is ρ=0.7 , how can I define this correlation??

In general, for arbitrary (linear) correlation and distributions, this is not possible. As Roger suggests, for the multivariate normal, it's easy. For other distributions, it's somewhere in between. You haven't provided enough information.

Subject: Generation of correlated random numbers

From: Jan

Date: 24 Feb, 2009 14:58:16

Message: 4 of 8

Elena wrote:
> Hi!
> I wanna do a very simple thing but I don't know how yo do it...
> I want to generate some values for 2 random variables (with known distribution, mean values and standard deviations) but the random variables are not independent. If for example the correlation of these variables is ρ=0.7 , how can I define this correlation??
>
> Thank u!

Hmm, maybe do something like draw pairs of random numbers from your two
random variables and throw some of them away, so that the correlation
gets a specific value. This way, the distribution of the single random
variables should remain unchanged (??) while you can get a correlation.

For example: Throw all pairs away, where the first random number is
smaller than the mean of its underlying distribution and the second
random number is larger than its mean... and vice versa. Try to find a
parameter in this throwing away business. Then try different parameter
values and measure, which correlation is the outcome in each case.

This is a rahter low sophisticated approach...

Greetings
Jan

Subject: Generation of correlated random numbers

From: Roger Stafford

Date: 24 Feb, 2009 19:20:04

Message: 5 of 8

Jan <janila@web.de> wrote in message <go11u8$1kdh$1@gwdu112.gwdg.de>...
> Elena wrote:
> > Hi!
> > I wanna do a very simple thing but I don't know how yo do it...
> > I want to generate some values for 2 random variables (with known distribution, mean values and standard deviations) but the random variables are not independent. If for example the correlation of these variables is ρ=0.7 , how can I define this correlation??
> >
> > Thank u!
>
> Hmm, maybe do something like draw pairs of random numbers from your two
> random variables and throw some of them away, so that the correlation
> gets a specific value. This way, the distribution of the single random
> variables should remain unchanged (??) while you can get a correlation.
>
> For example: Throw all pairs away, where the first random number is
> smaller than the mean of its underlying distribution and the second
> random number is larger than its mean... and vice versa. Try to find a
> parameter in this throwing away business. Then try different parameter
> values and measure, which correlation is the outcome in each case.
>
> This is a rahter low sophisticated approach...
>
> Greetings
> Jan

  Jan, you have asserted "the distribution of the single random variables should remain unchanged" with the selective rejection you described. I would be very surprised if that statement were true. Any such selection as this would almost inevitably alter both random variables' individual distributions.

  What is it you find objectionable about using 'mvnrnd' or the method I showed you? Do you want something other than jointly normal random variables? Perhaps you should describe your problem more fully.

Roger Stafford

Subject: Generation of correlated random numbers

From: Roger Stafford

Date: 10 Apr, 2009 21:32:00

Message: 6 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gmlo2a$srg$1@fred.mathworks.com>...
> Elena <elena.k29@hotmail.com> wrote in message <30956643.1233931196387.JavaMail.jakarta@nitrogen.mathforum.org>...
> > I want to generate some values for 2 random variables (with known distribution, mean values and standard deviations) but the random variables are not independent. If for example the correlation of these variables is ρ=0.7 , how can I define this correlation??
> > ......
>
> If you have the Statistics Toolbox, use 'mvnrnd'. The needed covariance matrix would be
>
> SIGMA = [s1^2,p*s1*s2;p*s1*s2,s2^2]
>
> where s1 and s2 are the specified variances and p the correlation.
> .......
> Roger Stafford

  Elena, I have noticed an error in my earlier response Feb. 8 in this thread. Where I said, "where s1 and s2 are the specified variances", I should have said, "where s1 and s2 are the specified standard deviations". I hope you were able to make the necessary corrections. Please accept my apologies.

Roger Stafford

Subject: Generation of correlated random numbers

From: Sheng-Yun

Date: 8 Oct, 2009 09:25:02

Message: 7 of 8

Roger,
      I want to generate 12 random data with known distribution, mean values and standard deviations. However, there are correlations between variable 1, 5 and 9 (that is, there are three correlations, P15, P19, and P59). The left variables are independent.
     What should I do with three correlations? I do not have statistics toolbox.

Thank you very much.
Shengyun




"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <grodsg$pi3$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gmlo2a$srg$1@fred.mathworks.com>...
> > Elena <elena.k29@hotmail.com> wrote in message <30956643.1233931196387.JavaMail.jakarta@nitrogen.mathforum.org>...
> > > I want to generate some values for 2 random variables (with known distribution, mean values and standard deviations) but the random variables are not independent. If for example the correlation of these variables is ρ=0.7 , how can I define this correlation??
> > > ......
> >
> > If you have the Statistics Toolbox, use 'mvnrnd'. The needed covariance matrix would be
> >
> > SIGMA = [s1^2,p*s1*s2;p*s1*s2,s2^2]
> >
> > where s1 and s2 are the specified variances and p the correlation.
> > .......
> > Roger Stafford
>
> Elena, I have noticed an error in my earlier response Feb. 8 in this thread. Where I said, "where s1 and s2 are the specified variances", I should have said, "where s1 and s2 are the specified standard deviations". I hope you were able to make the necessary corrections. Please accept my apologies.
>
> Roger Stafford

Subject: Generation of correlated random numbers

From: Imran Shafique Ansari

Date: 11 Jul, 2012 19:46:13

Message: 8 of 8

Very helpful indeed. Thanks Roger.

"Roger Stafford" wrote in message <gmlo2a$srg$1@fred.mathworks.com>...
> Elena <elena.k29@hotmail.com> wrote in message <30956643.1233931196387.JavaMail.jakarta@nitrogen.mathforum.org>...
> > Hi!
> > I wanna do a very simple thing but I don't know how yo do it...
> > I want to generate some values for 2 random variables (with known distribution, mean values and standard deviations) but the random variables are not independent. If for example the correlation of these variables is ?=0.7 , how can I define this correlation??
> >
> > Thank u!
>
> If you have the Statistics Toolbox, use 'mvnrnd'. The needed covariance matrix would be
>
> SIGMA = [s1^2,p*s1*s2;p*s1*s2,s2^2]
>
> where s1 and s2 are the specified variances and p the correlation. The needed MU consists of the two specified mean values, m1 and m2.
>
> If you don't have that toolbox, just do this:
>
> u = randn(1,n);
> v = randn(1,n);
> x = s1*u+m1;
> y = s2*(p*u+sqrt(1-p^2)*v)+m2;
>
> Then x and y are each n-element row vectors having the required joint distributions.
>
> I have assumed here that you intended that these random variables be jointly normally distributed.
>
> Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us