为什么在R中求解会给出奇怪的逆?

时间:2014-09-22 07:33:02

标签: r matrix inverse matrix-inverse

M矩阵

     1000          793504755
793504755    629649829362825

这是我在R中的矩阵M. solve(M)出现此错误:

solve(M)
Error in solve.default(M) : 
  system is computationally singular: reciprocal condition number = 8.36282e-20

而excel中的MMINV为这个2 x 2矩阵提供了所需的输出

使用某种分解算法R计算是否反转?为什么它不为我的矩阵提供输出?矩阵行列式也不为零。实际上它是一个很大的价值。

2 个答案:

答案 0 :(得分:4)

您遇到了麻烦,因为您的矩阵病态。您可以使用Matrix包解决它,而不必担心中间步骤:

library(Matrix)
solve(Matrix(M))
2 x 2 Matrix of class "dsyMatrix"
              [,1]          [,2]
[1,]  1.899097e+04 -2.393303e-02
[2,] -2.393303e-02  3.016117e-08

但结果中有一些数字不精确:

M %*% solve(Matrix(M))
2 x 2 Matrix of class "dgeMatrix"
            [,1] [,2]
[1,] 1.000000004    0
[2,] 0.001953125    1

这是因为倒数条件数小于双精度所提供的数值精度。

rcond(M)
[1] 8.362817e-20
storage.mode(M)
[1] "double"
.Machine$double.eps
[1] 2.220446e-16

但是,包gmp使用有理数提供精确的精度,以便为您提供所需的结果:

library(gmp)
solve(as.bigz(M))
Big Rational ('bigq') 2 x 2 matrix:
     [,1]                  [,2]              
[1,] 932814562019/49118837 -5877813/245594185
[2,] -5877813/245594185    40/1326208599     
M %*% solve(matrix(as.bigz(c(M)),2))
Big Rational ('bigq') 2 x 2 matrix:
     [,1] [,2]
[1,] 1    0   
[2,] 0    1

答案 1 :(得分:3)

这会遇到数值上的困难。您可以使用"手册"大卫建议的方法或使用Choleski分解得到一个合理的估计:

chol2inv(chol(M))
#              [,1]          [,2]
#[1,]  1.899097e+04 -2.393303e-02
#[2,] -2.393303e-02  3.016117e-08

chol2inv(chol(M)) %*% M
#             [,1]        [,2]
#[1,] 1.000000e+00 0.001953125
#[2,] 3.552714e-15 1.000000000

但是,我建议避免计算此矩阵的倒数。