[R] generate two sets of random numbers that are correlated

Duncan Murdoch murdoch.duncan at gmail.com
Thu Aug 11 19:33:27 CEST 2011


On 11/08/2011 12:01 PM, Kathie wrote:
> almost forgot. In fact, I want to generate correlated Poisson random vectors.

Saying you want two random variables to be correlated doesn't specify 
the joint distribution, so there will be a lot of solutions.  Here's 
one, for the case where both variables have the same mean mu, and you 
want a positive correlation.

We know that the sum of independent Poissons is Poisson, so we'll 
generate 3 variables: X with mean nu, and Y & Z with mean mu-nu, and return
A = X+Y and B = X+Z.  If nu=0 then A and B are independent, and if 
nu=mu, they have correlation 1, so you must be able to solve for a value 
where they have any desired correlation in between.

If the means aren't the same, this method will still work up to a point, 
but you won't be able to get really high correlations.

If you want negative correlations it's harder, but you could use the 
following trick:  Generate U ~ Unif(0, 1).  Calculate A by the inverse 
CDF method from U.  Compute V to be equal to U if U < a or U > 1-a, and 
equal to 1-U otherwise.  Calculate B by the inverse CDF method on V.

Then both U and V will have Poisson distributions (and you can choose 
the means as you like), and there will be some range of achievable 
correlations which will be quite close to [-1, 1].  The joint 
distribution will be very weird, but you didn't say that was a problem...

Some R code:

U <- runif(10000)
A <- qpois(U, 5)
a <- 0.115
V <- ifelse(U < a | U > 1-a, U, 1-U)
B <- qpois(V, 5)
cor(A, B)

This gives a correlation around 0.4.


Duncan Murdoch



More information about the R-help mailing list