Re: [R] positive semi-definite matrix

From: Martin Maechler <>
Date: Sat 22 Jul 2006 - 01:00:41 EST

>>>>> "Duncan" == Duncan Murdoch <> >>>>> on Fri, 21 Jul 2006 09:44:42 -0400 writes:

    Duncan> On 7/21/2006 8:59 AM, roger bos wrote:
>> I have a covariance matrix that is not positive semi-definite matrix and I
>> need it to be via some sort of adjustment. Is there any R routine or
>> package to help me do this?

    Duncan> I think you need to be more specific about what have and what you want, 
    Duncan> but if the matrix is symmetric and nearly positive semi-definite (but 
    Duncan> not exactly because of rounding error), you might try something like

    Duncan> fixit <- function(A) {
    Duncan> eig <- eigen(A, symmetric = TRUE)
    Duncan> eig$values <- pmax(0, eig$values)
    Duncan> return(eig$vectors %*% diag(eig$values) %*% t(eig$vectors))     Duncan> }

    Duncan> Rounding error means this is not guaranteed to be positive     Duncan> semi-definite, but it will be very close.

A slightly more general and stable version of the above is available via sfsmisc::posdefify(.) :


Martin Maechler, ETH Zurich mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Sat Jul 22 02:16:25 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 22 Jul 2006 - 04:15:58 EST.

Mailing list information is available at Please read the posting guide before posting to the list.