Apologies for my limited knowledge of Random Variables. I am working on a problem which requires me to generate a correlation matrix with the absolute pairwise Pearson correlation should be close to 0.4 (off diagonal elements of the correlation matrix). To clarify further let me provide a concrete example. I am following the steps below:
Step 1: Get the sample correlation matrix between 5 products,
call it sample_corr matrix (just to get the correlation sign +ve or -ve)
> sample_corr
a b c d e
a 1.0000000 -0.1907169 1.0000000 -0.5872416 0.8346421
b -0.1907169 1.0000000 -0.1907169 0.6511824 -0.2783893
c 1.0000000 -0.1907169 1.0000000 -0.5872416 0.8346421
d -0.5872416 0.6511824 -0.5872416 1.0000000 -0.6638346
e 0.8346421 -0.2783893 0.8346421 -0.6638346 1.0000000
Now I need to discard the sample values and substitute them by number 0.4 (with maintaining the sign from the above sample matrix).
Step 2: replace values >0 and <0 with 0.4 and -0.4 respectively
sample_corr[sample_corr>0.0]<-0.4
sample_corr[sample_corr< 0]<-(-0.4)
diag(sample_corr)<-1
This replacement of the numbers is not a correct approach as it fails the positive-semi-definite test
> is.positive.semi.definite(sample_corr)
[1] FALSE
Now, despite that error, I use this matrix to generate normal random numbers using rmvnorm with "svd" option (assuming standard deviation as 1)
gen_norm<-rmvnorm(5000,mean = rep(0,5),sigma=sample_corr,method = "svd")
The obtained correlation structure is as follows:
> cor(gen_norm)
[,1] [,2] [,3] [,4] [,5]
[1,] 1.0000000 -0.2068077 -0.3214124 -0.2837120 -0.3949594
[2,] -0.2068077 1.0000000 -0.2800788 -0.3087180 0.3548876
[3,] -0.3214124 -0.2800788 1.0000000 -0.2260995 0.4010528
[4,] -0.2837120 -0.3087180 -0.2260995 1.0000000 -0.3444349
[5,] -0.3949594 0.3548876 0.4010528 -0.3444349 1.0000000
Which is not very close to desired correlation but is acceptable to what I was looking for.
My questions are: 1. Why don't I get an error while I am using rmvnorm() to generate the random numbers ? 2. How the correlation structure is corrected ?
sample_corr
are positive, so the matrix is positive definite. Can you show the code ofis.positive.semi.definite
? $\endgroup$ – javlacalle Mar 16 '17 at 16:32Sample_corr
has negative eigen value.> eigen(sample_corr, only.values = TRUE) $values [1] 2.0246211 1.4000000 1.4000000 0.3753789 -0.2000000
$\endgroup$ – vivek Mar 17 '17 at 3:11dput(sample_corr)
. $\endgroup$ – javlacalle Mar 17 '17 at 7:06