# Why can't I simulate variables with negative correlation? How can I fix it?

I would like to simulate data with different correlation matrices, with this method:

M = matrix(c(1.0,  0.6,  0.6, 0.6,
0.6,  1.0, -0.2, 0.0,
0.6, -0.2,  1.0, 0.0,
0.6,  0.0,  0.0, 1.0 ),
nrow=4, ncol=4)


### Cholesky-decomposition

L = chol(M)
nvars = dim(L)[1]


### Random variables:

r = t(L) %*% matrix(rnorm(nvars * megf), nrow=nvars, ncol=megf)
r = t(r)


It worked with positive correlations, but I also need negative. Why doesn't it work? How can I do that?

Source of the code

Your correlation matrix is not positive definite. This means that it is not possible for a real dataset to have generated it.

> det(M)
[1] -0.2496


This works and has a negative correlation:

> M=matrix(c(1.0,  0.6,  0.6, 0.6,
0.6,  1.0, -0.2, 0.3,
0.6, -0.2,  1.0, 0.3,
0.6,  0.3,  0.3, 1.0)
,nrow=4, ncol=4)
>
> det(M)
[1] 0.0528


Your code doesn't run, because megf doesn't get defined.

You can save a little effort by using the mvrnorm() function, in the MASS package.

> library(MASS)
> set.seed(1234)  #Set seed for replicability
> r <- mvrnorm(n=1000, Sigma=M, mu=rep(0, 4) )
> cor(r)
[,1]       [,2]       [,3]      [,4]
[1,] 1.0000000  0.5748690  0.6330390 0.5950443
[2,] 0.5748690  1.0000000 -0.1879727 0.2915380
[3,] 0.6330390 -0.1879727  1.0000000 0.3048610
[4,] 0.5950443  0.2915380  0.3048610 1.0000000

• Note that it is not enough just to check the sign of the determinant to know whether a matrix is positive definite. You can have two negative eigenvalues that make the determinant positive. I think that you should use eigen in the answer to check the eigenvalues of the matrix to see that all of them are indeed positive. This answer might in some sense imply that looking at the sign of the determinant is enough to determine if the matrix is positive definite. @Jeremy Miles – Gumeo Oct 17 '15 at 14:03

Cholesky method works with negative correlations. It does require positive definite matrix, of course, but the matrix can have negative elements in it, see this example.