0
$\begingroup$

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 ?

$\endgroup$

marked as duplicate by Xi'an, Glen_b r Mar 5 '17 at 10:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ 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. $\endgroup$ – Glen_b Mar 5 '17 at 9:46
  • $\begingroup$ Can you explain the exact conditions that must be satisfied? How is the "correlation structure" specified? $\endgroup$ – Glen_b Mar 5 '17 at 9:54
  • $\begingroup$ 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 $\endgroup$ – Glen_b Mar 5 '17 at 9:56
  • $\begingroup$ @Glen_b thanks for your help. Looks like the links you provided and Richard's answer will solve the problem. $\endgroup$ – vivek Mar 5 '17 at 9:58
  • $\begingroup$ @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. $\endgroup$ – vivek Mar 5 '17 at 10:11
2
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ (+1): copulas indeed are the perfect solution to this question. $\endgroup$ – Xi'an Mar 5 '17 at 9:54
  • $\begingroup$ @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 $\endgroup$ – vivek Mar 5 '17 at 9:56
  • 1
    $\begingroup$ 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 $\endgroup$ – Glen_b Mar 5 '17 at 10:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.