[R] generating random covariance matrices (with a uniform distribution of correlations)

Petr Savicky savicky at praha1.ff.cuni.cz
Sat Jun 4 00:26:20 CEST 2011


On Fri, Jun 03, 2011 at 01:54:33PM -0700, Ned Dochtermann wrote:
> Petr,
> This is the code I used for your suggestion:
> 
> 	k<-6;kk<-(k*(k-1))/2
> 	x<-matrix(0,5000,kk)
> 	for(i in 1:5000){
> 	A.1<-matrix(0,k,k)
> 	rs<-runif(kk,min=-1,max=1)
> 	A.1[lower.tri(A.1)]<-rs
> 	A.1[upper.tri(A.1)]<-t(A.1)[upper.tri(A.1)]
> 	cors.i<-diag(k)
> 	t<-.001-min(Re(eigen(A.1)$values))
> 	new.cor<-cov2cor(A.1+(t*cors.i))
> 	x[i,]<-new.cor[lower.tri(new.cor)]}
> 	hist(c(x)); max(c(x)); median(c(x))
> 
> This, unfortunately, does not maintain the desired distribution of
> correlations.

I see. I overlooked that you require correlations and not
covariances.

The distribution of correlations cannot be chosen arbitrarily,
since it is not possible to have k variables, such that each
two of them have correlation less than -1/(k-1). For example,
it is not possible that all pairs among 3 variables have
correlation less than -0.5.

The reason is as follows. If X_1, ..., X_k have mean 0,
variance 1 and all pairs have correlation at most c, then

  E (X_1 + ... + X_k)^2 <= k(1 + (k-1)c)

If c < -1/(k-1), then the right hand side is negative,
which is not possible.

Can you relax the requirement on the negative correlations?

Petr.



More information about the R-help mailing list