比较R中的矩阵求逆-Cholesky方法有什么问题? [英] Comparing matrix inversions in R - what is wrong with the Cholesky method?
问题描述
我比较了各种方法来计算对称矩阵的逆:
I compared various methods to compute the inverse of a symmetric matrix:
- 解决(来自LAPCK软件包)
- 解决(但使用更高的机器精度)
- qr.solve(据说更快)
- ginv(MASS软件包,实施Moore-Penrose算法)
- chol2inv(使用Cholesky分解)
通过特征值比较逆矩阵:
The inverse-matrix was compared through their eigenvalues:
R
library(MASS)
## Create the matrix
m = replicate(10, runif(n=10))
m[lower.tri(m)] = t(m)[lower.tri(m)]
## Inverse the matrix
inv1 = solve(m)
inv2 = solve(m, tol = .Machine$double.eps)
inv3 = qr.solve(m)
inv4 = ginv(m)
inv5 = chol2inv(m)
## Eigenvalues of the inverse
em1=eigen(inv1)
em2=eigen(inv2)
em3=eigen(inv3)
em4=eigen(inv4)
em5=eigen(inv5)
## Plot the abs of the eigenvalues (may be complex)
myPch=c( 20, 15, 17, 25, 3 )
plot(abs(em1$values),pch=myPch[1],cex=1.5)
points(abs(em2$values),pch=myPch[2], cex=1.5)
points(abs(em3$values),pch=myPch[3], cex=1.5)
points(abs(em4$values),pch=myPch[4], cex=1.5)
points(abs(em5$values),pch=myPch[5], cex=1.5)
legend( "topright", c("solve","solve-double","solve-fast","Moore-Penrose","Cholesky"), pch=myPch )
如您所见,Cholesky方法给出的反函数与其他方法明显不同.
As you can see the inverse given by the Cholesky method is clearly different from the other.
根据此帖子,如果矩阵是对称的(在我们的情况下为是),则首选Cholesky方法: 矩阵求逆还是Cholesky?
According to this post, if the matrix is symmetric (in our case yes), the Cholesky method is to be preferred: Matrix inversion or Cholesky?
但是solve()是官方扩展"的R方法来反转方法,我可能会误会一些...
but solve() being the "official-wellspread" R method to invert method, I may rather misunderstand something...
有什么好建议吗?
预先感谢
推荐答案
您需要将Cholesky分解传递给chol2inv
:
You need to pass the Cholesky decomposition to chol2inv
:
inv5 = chol2inv(chol(m))
如果m
是正定的(对于您不可复制的输入来说可能不是),则此结果应与其他方法相同.
If m
is positive definite (which it probably isn't for your not-reproducible input) this should give the same result as the other methods.
这篇关于比较R中的矩阵求逆-Cholesky方法有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!