[R] Cholesky Decomposition in R
Ravi Varadhan
RVaradhan at jhmi.edu
Wed Mar 11 14:25:37 CET 2009
You got an "A+" on the homework, Doug!
I got a "C-" for suggesting svd(), which clearly doesn't yield a lower (or
upper) triangular factorization.
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 Douglas Bates
Sent: Tuesday, March 10, 2009 10:14 PM
To: Manli Yan
Cc: r-help at r-project.org
Subject: Re: [R] Cholesky Decomposition in R
On Tue, Mar 10, 2009 at 4:33 PM, Manli Yan <manliyanrhelp at gmail.com> wrote:
> Hi everyone:
> I try to use r to do the Cholesky Decomposition,which is A=LDL',so
> far I only found how to decomposite A in to LL' by using chol(A),the
> function
> Cholesky(A) doesnt work,any one know other command to decomposte A in
> to LDL'
>
> My r code is:
> library(Matrix)
> A=matrix(c(1,1,1,1,5,5,1,5,14),nrow=3)
>
>> chol(A)
> [,1] [,2] [,3]
> [1,] 1 1 1
> [2,] 0 2 2
> [3,] 0 0 3
>
>> Cholesky(A)
> Error in function (classes, fdef, mtable) :
> unable to find an inherited method for function "Cholesky", for
> signature "matrix"
>
> whatz wrong???
The answer, surprisingly, is in the documentation, accessible as
?Cholesky
which says that the first argument has to be a sparse, symmetric matrix.
Because the Cholesky function is intended for sparse matrices it is not the
best approach. The object returned is rather obscure
> Cholesky(as(A, "dsCMatrix"), LDL = TRUE)
'MatrixFactorization' of Formal class 'dCHMsimpl' [package "Matrix"] with 10
slots
..@ x : num [1:6] 1 1 1 4 1 9
..@ p : int [1:4] 0 3 5 6
..@ i : int [1:6] 0 1 2 1 2 2
..@ nz : int [1:3] 3 2 1
..@ nxt : int [1:5] 1 2 3 -1 0
..@ prv : int [1:5] 4 0 1 2 -1
..@ colcount: int [1:3] 3 2 1
..@ perm : int [1:3] 0 1 2
..@ type : int [1:4] 2 0 0 1
..@ Dim : int [1:2] 3 3
It turns out that the factorization you want is encoded in the 'x'
slot but not in an obvious way. Even if you ask for an expansion
> expand(Cholesky(as(A, "dsCMatrix"), LDL = TRUE))
$P
[1,] | . .
[2,] . | .
[3,] . . |
$L
[1,] 1 . .
[2,] 1 2 .
[3,] 1 2 3
the result is converted from the LDL' factor to the LL' factor.
A better approach is to consider how the LDL' factorization is related to
the R'R form of the factorization returned by chol()
> ch <- chol(A)
> dd <- diag(ch)
> L <- t(ch/dd)
> DD <- dd^2
> L
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 1 1 0
[3,] 1 1 1
> DD
[1] 1 4 9
This is all rather backwards in that the whole purpose of the LDL'
form of the factorization is to avoid taking square roots to get the
diagonal elements and to contend with positive semidefinite matrices.
In other words, the LDL' form avoids some of the possible problems of the
LL' form but not if you go through the LL' form to get to it.
I think the underlying reason that an LDL' form is not directly available in
R is because there is no Lapack subroutine for it.
Let me know what our grade on the homework is.
______________________________________________
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.
More information about the R-help
mailing list