# Generating correlated uniform random variable with R [duplicate]

I am trying to generate Correlated Uniform Random Variables with given mean, standard and correlation structure. I have looked through various posts in this topic including this(Can I use the Cholesky-method for generating correlated random variables with given mean?) and seems to can't get through.

Here is how I am proceeding.

set.seed(12)
corr_raw=matrix(0.7, nrow = 4,ncol=4) # Correlations
diag(corr_raw)=c(1,1,1,1)
colnames(corr_raw)<-c("A","B","C","D")
rownames(corr_raw)<-colnames(corr_raw)

Cov=(std_dev%*%t(std_dev))*corr_raw # covariance matrix

#mean and standard deviation
mu<-c(1,3,2,4)
std_dev<-0.4*mu #assumed std_dev = 0.4*mean

#Cholesky Decomposition matrix L
L<-chol(Cov, pivot =T)
Z<-matrix(nrow = 0,ncol=10000)

for (i in colnames(Cov))
{
#stddev(Unif)=(B-A)/sqrt(12)=1; and mean(Unif)=(B+A)/2=0; A=-sqrt(3) and B=+sqrt(3)
Z<- rbind(Z,runif(10000,-1.732,1.732))
}
x=mu+L%*%Z  #transform to get the desired mean and variance
corr<-cor(t(x)) #find correlation of the generated random variable x


However, the corr(x) and corr_raw does not match. What am I doing wrong ?

• One thing (though not the only one) with the correlation is that even with multivariate normals, the sample correlation won't match the population correlation. You have a much worse problem than the correlation one however. When you compute $Lz$ the result will no longer be uniform so it will not satisfy the conditions. Commented Mar 5, 2017 at 9:46
• Can you explain the exact conditions that must be satisfied? How is the "correlation structure" specified? Commented Mar 5, 2017 at 9:54
• Some of the issues with generating correlated uniforms are discussed here, along with some possible ways to think about generating correlated uniforms: Generate three correlated uniformly-distributed random variables Commented Mar 5, 2017 at 9:56
• @Glen_b thanks for your help. Looks like the links you provided and Richard's answer will solve the problem. Commented Mar 5, 2017 at 9:58
• @Glen_b correlation structure refers to the pairwise correlation between RVs. For example, in the above example there are four random variables and the correlation structure refers to the correlation matrix which contains data for the pairwise correlations. Commented Mar 5, 2017 at 10:11

Try simulating from a multivariate normal distribution and then transforming the values by using the normal cdf.

This will produce correlated standard uniform variates. You can then shift and scale to get your desired mean and SD. Note that this will give you a given rank correlation.

More generally take a look at simulating from copulas.

• (+1): copulas indeed are the perfect solution to this question. Commented Mar 5, 2017 at 9:54
• @Richard thanks a lot for pointing that out. I understood now, that using rmvnorm and pnorm I should be able to get the standard uniform correlated random variables. Then I will do an affine transformation to arrive at the given mean and std deviation. Thanks Xi'an yes copulas looks like the answer I was looking for thankyou Commented Mar 5, 2017 at 9:56
• The problem with this approach is the OP is trying to achieve a particular correlation among the uniform variates, and that won't match the correlation of the normal variates Commented Mar 5, 2017 at 10:00