# [R] how to randomly generate a n by n positive definite matrix in R ?

Thu Feb 7 20:02:30 CET 2008

```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.
----------------------------------------------------------------------------
-------

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

----------------------------------------------------------------------------
--------

-----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