2

I have the following matrix:

j <- matrix(c(1,1,.5,1,1,.5,.5,.5,1), nrow=3, ncol=3)

Which is positive semi-definite, because all of the eigenvalues are >= 0.

Source: https://math.stackexchange.com/questions/40849/how-to-check-if-a-symmetric-4-times4-matrix-is-positive-semi-definite

> eigen(j, symmetric = TRUE)
$values
[1] 2.3660254 0.6339746 0.0000000

$vectors
           [,1]       [,2]          [,3]
[1,] -0.6279630 -0.3250576  7.071068e-01
[2,] -0.6279630 -0.3250576 -7.071068e-01
[3,] -0.4597008  0.8880738 -1.942890e-15

However, the cholesky decomposition fails...

> chol(j)
Error in chol.default(j) : 
  the leading minor of order 2 is not positive definite

I also adapted some code from the internet...

cholesky_matrix <- function(A){
    # http://rosettacode.org/wiki/Cholesky_decomposition#C

    L <- matrix(0,nrow=nrow(A),ncol=ncol(A))
    colnames(L) <- colnames(A)
    rownames(L) <- rownames(A)

    m <- ncol(L)


    for(i in 1:m){
        for(j in 1:i){
            s <- 0
            if(j > 1){
                for(k in 1:(j-1)){
                    s <- s + L[i,k]*L[j,k]
                }
            }
            if(i == j){
                L[i,j] <- sqrt(A[i,i] - s)
            } else {
                L[i,j] <- (1 / L[j,j])*(A[i,j] - s)
            }
        }
    }
    return(L)    
}

Which also "fails" with NaNs.

> cholesky_matrix(j)
     [,1] [,2] [,3]
[1,]  1.0    0    0
[2,]  1.0    0    0
[3,]  0.5  NaN  NaN

Does anyone have any idea what is going on? Why is my decomposition failing?

Community
  • 1
  • 1
JoeBass
  • 519
  • 7
  • 18

1 Answers1

3

The eigenvalues of your matrix are

> eigen(j)
$values
[1] 2.366025e+00 6.339746e-01 4.440892e-16

the last of which is effectively zero, within the limits of numerical precision. Per ?chol:

Compute the Choleski factorization of a real symmetric positive-definite square matrix.

(emphasis mine)

That said, you can still get the decomposition by setting pivot=TRUE, which is able to handle semi-definiteness:

> chol(j, pivot=TRUE)
     [,1]      [,2] [,3]
[1,]    1 0.5000000    1
[2,]    0 0.8660254    0
[3,]    0 0.0000000    0
attr(,"pivot")
[1] 1 3 2
attr(,"rank")
[1] 2
Warning message:
In chol.default(j, pivot = TRUE) :
  the matrix is either rank-deficient or indefinite
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187