# Generate three correlated uniformly-distributed random variables

Suppose we have

$$X_1 \sim \textrm{unif}(n,0,1),$$ $$X_2 \sim \textrm{unif}(n,0,1),$$

where $\textrm{unif}(n,0,1)$ is uniform random sample of size n, and

$$Y=X_1,$$

$$Z = 0.4 X_1 + \sqrt{1 - 0.4}X_2.$$

Then the correlation between $Y$ and $Z$ is $0.4$.

How can I extend this to three variables: $X_1$, $X_2$, $X_3$?

• I have edited your question to make it easier to read. Please, check everything is OK. Regarding your question, in what sense would you extend your procedure? Correlation is define for two random variables, so it is not clear for me to get what you mean. – ocram Jul 6 '12 at 13:25
• $Z$ is not uniform, so if you're trying to generalize that result it doesn't seem like you're trying to generate three correlated uniform RVs. Are you wondering about how to compute the correlation between $X_1$ and $aX_1+bX_2+cX_3$? – MånsT Jul 6 '12 at 13:28
• Suppose we've $X_1$, $X_2$, $X_3$ $~unif(n,0,1)$, and $Y=f(X_2,X_3)$, $Z=f(X_1,X_2,X_3)$. Then what are $Y$ and $Z$? – user9292 Jul 6 '12 at 13:35
• $$\{\text{Distributions of correlated uniforms}\} \subset \{\text{Copulas}\}$$ – cardinal Jul 6 '12 at 13:41
• Why does n enter into the discussion? If X1 and X2 are univariate random variables aren't they simply uniform on[0,1]? – Michael Chernick Jul 6 '12 at 13:43

The question contains several errors as noted in comments - as defined in the question, Z is neither uniform nor has the specified correlation.

cardinal mentions copulas, and that's the most general way to go about it. However, there are several quite easy ways to get correlated uniforms (which can be seen as mere shortcuts to different kinds of copulas).

So let's start with some ways to get a pair of correlated uniforms.

1) If you add two uniforms the result is triangular, not uniform. But you can use the cdf of the resulting variable as a transform to take the result back to a uniform. The result isn't linearly correlated any more, of course.

Here's an R function to transform a symmetric triangular on (0,2) to standard uniform

t2u = function(x) ifelse(x<1, x^2, 2-(2-x)^2)/2

Let's check that it does give a uniform

u1 = runif(30000)
u2 = runif(30000)
v1 = t2u(u1+u2)

And it's correlated with u1 and u2:

> cor(cbind(u1,u2,v1))
u1          u2        v1
u1 1.000000000 0.006311667 0.7035149
u2 0.006311667 1.000000000 0.7008528
v1 0.703514895 0.700852805 1.0000000

but not linearly, due to the monotonic transformation to uniformity

With this as a tool we can generate some additional variables to get three equicorrelated uniforms:

u3 = runif(30000)
v2 = t2u(u1+u3)
v3 = t2u(u2+u3)

cor(cbind(v1,v2,v3))
v1        v2        v3
v1 1.0000000 0.4967572 0.4896972
v2 0.4967572 1.0000000 0.4934746
v3 0.4896972 0.4934746 1.0000000

The relationship between the v-variables all look like this:

--

A second alternative is to generate by taking a mixture. Instead of summing uniforms, take them with fixed probabilities.

e.g.

z = ifelse(rbinom(30000,1,.7),u1,u2)

cor(cbind(u1,z))
u1         z
u1 1.0000000 0.7081533
z  0.7081533 1.0000000

Which can again be used to generate multiple correlated uniforms.

--

A third simple approach is to generate correlated normals and transform to uniformity.

n1=rnorm(30000)
n2=rnorm(30000)
n3=rnorm(30000)
x=.6*n1+.8*n2
y=.6*n2+.8*n3
z=.6*n3+.8*n1
cor(cbind(x,y,z))

x         y         z
x 1.0000000 0.4763703 0.4792897
y 0.4763703 1.0000000 0.4769403
z 0.4792897 0.4769403 1.0000000

So now we convert to uniform:

w1 = pnorm(x)
w2 = pnorm(y)
w3 = pnorm(z)
cor(cbind(w1,w2,w3))
w1        w2        w3
w1 1.0000000 0.4606723 0.4623311
w2 0.4606723 1.0000000 0.4620257
w3 0.4623311 0.4620257 1.0000000

One nice thing about methods 2 and 3 is that you get plenty of variety in your choice of how correlated things might be (and they don't have to be equicorrelated like the examples here).

There's a large variety of other approaches of course, but these are all quick and easy.

The tricky part is getting exactly the desired population correlation; it's not quite so simple as when you just want correlated Gaussians. Quantibex's answer at Generate pairs of random numbers uniformly distributed and correlated gives an approach that modifies my third method here which should give about the desired population correlation.

• Glen_b. Thank you, very beautiful and interesting answer! – user9292 Mar 26 '15 at 3:46
• I don't understand where the 0.6 and 0.8 in your third approach come from. – manuel Nov 13 '18 at 9:56
• From a particular choice of $\rho$ in $\rho N_i + \sqrt{1-\rho^2} N_j$, (if $N_i$ and $N_j$ have unit variance) will give a result that has a correlation of $\rho$ with $N_i$ and $\sqrt{1-\rho^2}$ with $N_j$. Because of those correlations, $X$, $Y$ and $Z$ are then equicorrelated normals. (This isn't the most efficient possible way to do that, but it is a way.) – Glen_b Nov 13 '18 at 10:34

First, do you assume that $X_1,X_2$ are independent? If they are, then correlation coefficient between $Z$ and $X_1$ is not $0.4$. It would be $0.4$ if $Y$ were defined as $Y = 0.4X_1+\sqrt{1-(0.4)^2}X_2$.

A simple look at the definition of the correlation coefficient formula and the law of cosines should convince you that $\rho$ is a $\cos$ between $2$ series if the series are treated as vectors with each datapoint being treated as a dimension of a vector. If you have $3$ pair-wise independent series, that's three vectors all of which are orthogonal to each other (because the $\cos$'s of the angles between them are all $0$'s.

This should start you on the way to decomposing a series into its components the same way you'd decompose a vector into its orthogonal components.