Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to generate multiple correlated gaussian random variables?

326 views
Skip to first unread message

gary...@gmail.com

unread,
May 23, 2009, 9:35:46 PM5/23/09
to
In order to generate some test data, I need to be able generate N
correlated gaussian random variables, where all the random variables
have the same correlation to one another.

There's no problem when N is 2, but is there a free software package
or simple algorithm that can do it for N > 2? I assume there must be a
software package that can do it for a given correlation matrix, which
I could then set to have identical correlations, but I'm not sure what
freeware packages, if any, have that ability.

Thanks for any advice,
Gary

garyrob

unread,
May 23, 2009, 11:20:48 PM5/23/09
to
I've been researching the multiple correlated variate question
further, and find that one can use a Cholesky decomposition to get a
matrix that can be used to generate correlated variates from a matrix
of independent ones. But the correlation matrix needs to be positive
definite... this is getting into an area of math that I'm very
unfamiliar with. At this point I'm concerned that maybe it isn't even
possible to have a positive definite correlation matrix where all the
correlations are identical. Any help or insight would be apprieciated.

Gary

Ray Koopman

unread,
May 24, 2009, 12:28:33 AM5/24/09
to

Here's the easiest way to do it when r is positive.
Generate N+1 independent values from a Normal(0,1) population.
Call them x1, ..., xN, y.
Then let zi = xi*sqrt[1-r] + y*sqrt[r], i = 1...N.
z1,...,zN will be a sample from a multivariate normal population in
which all means = 0, all variances = 1, and all correlations = r.

garyrob

unread,
May 24, 2009, 4:03:12 PM5/24/09
to

> Here's the easiest way to do it when r is positive.
> Generate N+1 independent values from a Normal(0,1) population.
> Call them x1, ..., xN, y.
> Then let  zi = xi*sqrt[1-r] + y*sqrt[r], i = 1...N.
> z1,...,zN will be a sample from a multivariate normal population in
> which all means = 0, all variances = 1, and all correlations = r.


For the case of N=1 (meaning we want 2 values), I found a similar but
different approach
at http://www.pitt.edu/~wpilib/statfaq/gaussfaq.html:

> We will
> assume s1 and s2 are the standard deviations, c is the desired
> correlation between them (-1 <= c <= 1) and mu1 and mu2 are the
> desired means.
>
> First generate two unit normal random deviates, N1 and N2.
>
> X = mu1 + s1*N1
>
> Y = mu2 + c*s2*N1 + sqrt(1-c^2)*s2*N2
>
> X and Y will be bivariate random normal deviates with the desired
> distribution parameters.

For N=1 seems to be the same as yours, if we square the correlation
instead of using the correlation itself. The standard deviates
produced by this other method do seem to have the given correlation.
(If I generate a long series of them with a particular correlation,
and then calculate the correlation from the series, they appear to
match.) When I use your algorithm, I don't get the same correlation
out that I put in. Did you mean to use the square of the correlation
for r?

The other question I have regarding this is: I understand that each of
the variates you produce would be correlated with a desired
correlation to y, but it isn't intuitive to me that all being
correlated with the desired correlation to y means they'd also have
that correlation to each other. I'd think that others would be less
correlated to each other due to accumulated degree of differences...

Any thoughts/comments/explanations...


garyrob

unread,
May 24, 2009, 4:12:40 PM5/24/09
to
Oh, I get it now! Thanks. You are relying on the accumulated greater
correlation between your zi'z (compared to the correlation between y
and each of the zi's) to add up to the desired correlation. Perfect!
Thanks.

Greg

unread,
Jun 19, 2009, 2:40:27 PM6/19/09
to

There is a formula for the eigenvalues. A quick MATLAB simulation

for n = 1:10
A = eye(n)+ rand*(ones(n)-eye(n));
eig(A)
end

indicates that there is one unique eigenvalue
and n-1 equal eigenvalues; all positive.

You can probably deduce the general formula by
replacing the random number with integers
(even though they exceed 1),

Hope this helps,

Greg

r.day...@gmail.com

unread,
Mar 8, 2013, 10:35:44 AM3/8/13
to
0 new messages