jinjin
2008-Feb-07 18:32 UTC
[R] how to randomly generate a n by n positive definite matrix in R ?
thanks -- View this message in context: http://www.nabble.com/how-to-randomly-generate-a-n-by-n-positive-definite-matrix-in-R---tp15340155p15340155.html Sent from the R help mailing list archive at Nabble.com.
Ravi Varadhan
2008-Feb-07 19:02 UTC
[R] how to randomly generate a n by n positive definite matrix in R ?
Here is a simple function: ########################################################## # Generating a random positive-definite matrix with user-specified positive eigenvalues # If eigenvalues are not specified, they are generated from a uniform distribution Posdef <- function (n, ev = runif(n, 0, 10)) { Z <- matrix(ncol=n, rnorm(n^2)) decomp <- qr(Z) Q <- qr.Q(decomp) R <- qr.R(decomp) d <- diag(R) ph <- d / abs(d) O <- Q %*% diag(ph) Z <- t(O) %*% diag(ev) %*% O return(Z) } pdmat <- Posdef(n=5, ev=1:5) eigen(pdmat)$val Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvaradhan at jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html ---------------------------------------------------------------------------- -------- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of jinjin Sent: Thursday, February 07, 2008 1:32 PM To: r-help at r-project.org Subject: [R] how to randomly generate a n by n positive definite matrix in R ? thanks -- View this message in context: http://www.nabble.com/how-to-randomly-generate-a-n-by-n-positive-definite-ma trix-in-R---tp15340155p15340155.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at 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.