Re: [R] [SPAM] - constructing arbitrary (positive definite) covariance matrix - Found word(s) list error in the Text body
Thanks to everyone who responded to my email. Moshe's email explains clearly why my matrices were not positive definite for certain negative correlations. I now have better understanding of the problem. Thanks Mizan 2008/6/27 Moshe Olshansky [EMAIL PROTECTED]: If the main diagonal element of matrix A is 1 and the off diagonal element is a then for any vector x we get that t(x)*A*x = (1-a)*sum(x^2) +a*(sum(x))^2 . If we want A to be positive (semi)definite we need this expression to be positive (non-negative) for any x!= 0. Since sum(x)^2/sum(x*2) = n where n is the dimension of the matrix and equality is possible we get that A is positive (semi)definite if and only if -1/(n-1) = a = 1 (sharp inequalities for positive definiteness). Since any symmetric (semi)positive definite matrix can be a covariance matrix this describes all the matrices which satisfy the requirement. --- On Fri, 27/6/08, Patrick Burns [EMAIL PROTECTED] wrote: From: Patrick Burns [EMAIL PROTECTED] Subject: Re: [R] [SPAM] - constructing arbitrary (positive definite) covariance matrix - Found word(s) list error in the Text body To: [EMAIL PROTECTED] Cc: Mizanur Khondoker [EMAIL PROTECTED], r-help@r-project.org Received: Friday, 27 June, 2008, 3:15 AM To make David's approach a little more concrete: You can always have correlations all equal to 1 -- the variables are all the same, except for the names you've given them. You can have two variables with correlation -1, but you can't get a third variable that has -1 correlation to both of the first two. Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and A Guide for the Unwilling S User) [EMAIL PROTECTED] wrote: Well, if you think about the geometry, all correlations equal usually won't work. Think of the SDs as the sides of a simplex and the correlations as the cosines of the angles between the sides (pick one variable as the 'origin'.) Only certain values will give a valid covariance or correlation matrix. HTH, David L. Reiner, PhD Head Quant Rho Trading Securities, LLC -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mizanur Khondoker Sent: Thursday, June 26, 2008 11:11 AM To: r-help@r-project.org Subject: [SPAM] - [R] constructing arbitrary (positive definite) covariance matrix - Found word(s) list error in the Text body Dear list, I am trying to use the 'mvrnorm' function from the MASS package for simulating multivariate Gaussian data with given covariance matrix. The diagonal elements of my covariance matrix should be the same, i.e., all variables have the same marginal variance. Also all correlations between all pair of variables should be identical, but could be any value in [-1,1]. The problem I am having is that the matrix I create is not always positive definite (and hence mvrnorm fails). Is there any simple way of constructing covariance matrix of the above structure (equal variance, same pairwise correlation from [-1, 1]) that will always be positive definite? I have noticed that covraince matrices created using the following COV function are positive definite for -0.5 r 1. However, for r -0.5, the matrix is not positive definite. Does anyone have any idea why this is the case? For my simualtion, I need to generate multivariate data for the whole range of r, [-1, 1] for a give value of sd. Any help/ suggestion would be greatly appreciated. Examples COV-function (p = 3, sd = 1, r= 0.5){ cov - diag(sd^2, ncol=p, nrow=p) for (i in 1:p) { for (j in 1:p) { if (i != j) { cov[i, j] - r * sd*sd } } } cov } library(MASS) ### Simualte multivarite gaussin data (works OK) Sigma-COV(p = 3, sd = 2, r= 0.5) mu-1:3 mvrnorm(5, mu=mu, Sigma=Sigma) [,1] [,2] [,3] [1,] 1.2979984 1.843248 4.460891 [2,] 2.1061054 1.457201 3.774833 [3,] 2.1578538 2.761939 4.589977 [4,] 0.8775056 4.240710 2.203712 [5,] 0.2698180 2.075759 2.869573 ### Simualte multivarite gaussin data ( gives Error) Sigma-COV(p = 3, sd = 2, r= -0.6) mu-1:3 mvrnorm(5, mu=mu, Sigma=Sigma) Error in mvrnorm(5, mu = mu, Sigma = Sigma) : 'Sigma' is not positive definite __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Mizanur Khondoker Division of Pathway Medicine (DPM) The University of Edinburgh Medical School The Chancellor's Building 49 Little France Crescent Edinburgh EH16 4SB United Kingdom Tel: +44 (0) 131 242 6287 Fax: +44 (0) 131 242 6244 http://www.pathwaymedicine.ed.ac.uk
Re: [R] [SPAM] - constructing arbitrary (positive definite) covariance matrix - Found word(s) list error in the Text body
Well, if you think about the geometry, all correlations equal usually won't work. Think of the SDs as the sides of a simplex and the correlations as the cosines of the angles between the sides (pick one variable as the 'origin'.) Only certain values will give a valid covariance or correlation matrix. HTH, David L. Reiner, PhD Head Quant Rho Trading Securities, LLC -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mizanur Khondoker Sent: Thursday, June 26, 2008 11:11 AM To: r-help@r-project.org Subject: [SPAM] - [R] constructing arbitrary (positive definite) covariance matrix - Found word(s) list error in the Text body Dear list, I am trying to use the 'mvrnorm' function from the MASS package for simulating multivariate Gaussian data with given covariance matrix. The diagonal elements of my covariance matrix should be the same, i.e., all variables have the same marginal variance. Also all correlations between all pair of variables should be identical, but could be any value in [-1,1]. The problem I am having is that the matrix I create is not always positive definite (and hence mvrnorm fails). Is there any simple way of constructing covariance matrix of the above structure (equal variance, same pairwise correlation from [-1, 1]) that will always be positive definite? I have noticed that covraince matrices created using the following COV function are positive definite for -0.5 r 1. However, for r -0.5, the matrix is not positive definite. Does anyone have any idea why this is the case? For my simualtion, I need to generate multivariate data for the whole range of r, [-1, 1] for a give value of sd. Any help/ suggestion would be greatly appreciated. Examples COV-function (p = 3, sd = 1, r= 0.5){ cov - diag(sd^2, ncol=p, nrow=p) for (i in 1:p) { for (j in 1:p) { if (i != j) { cov[i, j] - r * sd*sd } } } cov } library(MASS) ### Simualte multivarite gaussin data (works OK) Sigma-COV(p = 3, sd = 2, r= 0.5) mu-1:3 mvrnorm(5, mu=mu, Sigma=Sigma) [,1] [,2] [,3] [1,] 1.2979984 1.843248 4.460891 [2,] 2.1061054 1.457201 3.774833 [3,] 2.1578538 2.761939 4.589977 [4,] 0.8775056 4.240710 2.203712 [5,] 0.2698180 2.075759 2.869573 ### Simualte multivarite gaussin data ( gives Error) Sigma-COV(p = 3, sd = 2, r= -0.6) mu-1:3 mvrnorm(5, mu=mu, Sigma=Sigma) Error in mvrnorm(5, mu = mu, Sigma = Sigma) : 'Sigma' is not positive definite -- Mizanur Khondoker Division of Pathway Medicine (DPM) The University of Edinburgh Medical School The Chancellor's Building 49 Little France Crescent Edinburgh EH16 4SB United Kingdom Tel: +44 (0) 131 242 6287 Fax: +44 (0) 131 242 6244 http://www.pathwaymedicine.ed.ac.uk/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] [SPAM] - constructing arbitrary (positive definite) covariance matrix - Found word(s) list error in the Text body
To make David's approach a little more concrete: You can always have correlations all equal to 1 -- the variables are all the same, except for the names you've given them. You can have two variables with correlation -1, but you can't get a third variable that has -1 correlation to both of the first two. Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and A Guide for the Unwilling S User) [EMAIL PROTECTED] wrote: Well, if you think about the geometry, all correlations equal usually won't work. Think of the SDs as the sides of a simplex and the correlations as the cosines of the angles between the sides (pick one variable as the 'origin'.) Only certain values will give a valid covariance or correlation matrix. HTH, David L. Reiner, PhD Head Quant Rho Trading Securities, LLC -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mizanur Khondoker Sent: Thursday, June 26, 2008 11:11 AM To: r-help@r-project.org Subject: [SPAM] - [R] constructing arbitrary (positive definite) covariance matrix - Found word(s) list error in the Text body Dear list, I am trying to use the 'mvrnorm' function from the MASS package for simulating multivariate Gaussian data with given covariance matrix. The diagonal elements of my covariance matrix should be the same, i.e., all variables have the same marginal variance. Also all correlations between all pair of variables should be identical, but could be any value in [-1,1]. The problem I am having is that the matrix I create is not always positive definite (and hence mvrnorm fails). Is there any simple way of constructing covariance matrix of the above structure (equal variance, same pairwise correlation from [-1, 1]) that will always be positive definite? I have noticed that covraince matrices created using the following COV function are positive definite for -0.5 r 1. However, for r -0.5, the matrix is not positive definite. Does anyone have any idea why this is the case? For my simualtion, I need to generate multivariate data for the whole range of r, [-1, 1] for a give value of sd. Any help/ suggestion would be greatly appreciated. Examples COV-function (p = 3, sd = 1, r= 0.5){ cov - diag(sd^2, ncol=p, nrow=p) for (i in 1:p) { for (j in 1:p) { if (i != j) { cov[i, j] - r * sd*sd } } } cov } library(MASS) ### Simualte multivarite gaussin data (works OK) Sigma-COV(p = 3, sd = 2, r= 0.5) mu-1:3 mvrnorm(5, mu=mu, Sigma=Sigma) [,1] [,2] [,3] [1,] 1.2979984 1.843248 4.460891 [2,] 2.1061054 1.457201 3.774833 [3,] 2.1578538 2.761939 4.589977 [4,] 0.8775056 4.240710 2.203712 [5,] 0.2698180 2.075759 2.869573 ### Simualte multivarite gaussin data ( gives Error) Sigma-COV(p = 3, sd = 2, r= -0.6) mu-1:3 mvrnorm(5, mu=mu, Sigma=Sigma) Error in mvrnorm(5, mu = mu, Sigma = Sigma) : 'Sigma' is not positive definite __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] [SPAM] - constructing arbitrary (positive definite) covariance matrix - Found word(s) list error in the Text body
If the main diagonal element of matrix A is 1 and the off diagonal element is a then for any vector x we get that t(x)*A*x = (1-a)*sum(x^2) +a*(sum(x))^2 . If we want A to be positive (semi)definite we need this expression to be positive (non-negative) for any x!= 0. Since sum(x)^2/sum(x*2) = n where n is the dimension of the matrix and equality is possible we get that A is positive (semi)definite if and only if -1/(n-1) = a = 1 (sharp inequalities for positive definiteness). Since any symmetric (semi)positive definite matrix can be a covariance matrix this describes all the matrices which satisfy the requirement. --- On Fri, 27/6/08, Patrick Burns [EMAIL PROTECTED] wrote: From: Patrick Burns [EMAIL PROTECTED] Subject: Re: [R] [SPAM] - constructing arbitrary (positive definite) covariance matrix - Found word(s) list error in the Text body To: [EMAIL PROTECTED] Cc: Mizanur Khondoker [EMAIL PROTECTED], r-help@r-project.org Received: Friday, 27 June, 2008, 3:15 AM To make David's approach a little more concrete: You can always have correlations all equal to 1 -- the variables are all the same, except for the names you've given them. You can have two variables with correlation -1, but you can't get a third variable that has -1 correlation to both of the first two. Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and A Guide for the Unwilling S User) [EMAIL PROTECTED] wrote: Well, if you think about the geometry, all correlations equal usually won't work. Think of the SDs as the sides of a simplex and the correlations as the cosines of the angles between the sides (pick one variable as the 'origin'.) Only certain values will give a valid covariance or correlation matrix. HTH, David L. Reiner, PhD Head Quant Rho Trading Securities, LLC -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mizanur Khondoker Sent: Thursday, June 26, 2008 11:11 AM To: r-help@r-project.org Subject: [SPAM] - [R] constructing arbitrary (positive definite) covariance matrix - Found word(s) list error in the Text body Dear list, I am trying to use the 'mvrnorm' function from the MASS package for simulating multivariate Gaussian data with given covariance matrix. The diagonal elements of my covariance matrix should be the same, i.e., all variables have the same marginal variance. Also all correlations between all pair of variables should be identical, but could be any value in [-1,1]. The problem I am having is that the matrix I create is not always positive definite (and hence mvrnorm fails). Is there any simple way of constructing covariance matrix of the above structure (equal variance, same pairwise correlation from [-1, 1]) that will always be positive definite? I have noticed that covraince matrices created using the following COV function are positive definite for -0.5 r 1. However, for r -0.5, the matrix is not positive definite. Does anyone have any idea why this is the case? For my simualtion, I need to generate multivariate data for the whole range of r, [-1, 1] for a give value of sd. Any help/ suggestion would be greatly appreciated. Examples COV-function (p = 3, sd = 1, r= 0.5){ cov - diag(sd^2, ncol=p, nrow=p) for (i in 1:p) { for (j in 1:p) { if (i != j) { cov[i, j] - r * sd*sd } } } cov } library(MASS) ### Simualte multivarite gaussin data (works OK) Sigma-COV(p = 3, sd = 2, r= 0.5) mu-1:3 mvrnorm(5, mu=mu, Sigma=Sigma) [,1] [,2] [,3] [1,] 1.2979984 1.843248 4.460891 [2,] 2.1061054 1.457201 3.774833 [3,] 2.1578538 2.761939 4.589977 [4,] 0.8775056 4.240710 2.203712 [5,] 0.2698180 2.075759 2.869573 ### Simualte multivarite gaussin data ( gives Error) Sigma-COV(p = 3, sd = 2, r= -0.6) mu-1:3 mvrnorm(5, mu=mu, Sigma=Sigma) Error in mvrnorm(5, mu = mu, Sigma = Sigma) : 'Sigma' is not positive definite __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.