Xav*_*ent 2 r matrix matrix-inverse
我比较了计算对称矩阵的逆的各种方法:
通过特征值比较逆矩阵:
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 )
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,Cholesky 方法给出的逆函数与其他方法明显不同。
根据这篇文章,如果矩阵是对称的(在我们的例子中是),则首选 Cholesky 方法: 矩阵求逆还是 Cholesky?
但solve()是“官方-wellspread”R方法来反转方法,我可能会误解一些东西......
有什么好的建议吗?
提前致谢,
您需要将 Cholesky 分解传递给chol2inv:
inv5 = chol2inv(chol(m))
Run Code Online (Sandbox Code Playgroud)
如果m是正定的(这可能不适合您不可重现的输入),这应该给出与其他方法相同的结果。