5

我需要在 Ruby 中对方差 - 协方差矩阵进行逆矩阵乘法运算。我应该使用哪个数字 Ruby 库/Gem?

4

4 回答 4

3

比直接反演在数值上更稳定的可能性是将Cholesky 分解与您在此处找到的包一起使用:

require 'Cholesky.rb'
require 'pp'
# m is the covariance matrix you want to invert (it is positive semidefinite)
l = m.cholesky
li = l.inverse
lit = li.transpose
# lit*li is approximately the inverse and the next line shows this
pp lit*li*m

比反转 l 更好的是使用上面链接的维基百科文章中描述的方法。

如果您的问题在数值上太不稳定,请考虑Singular Value Decomposition,但我没有代码。

于 2009-06-09T14:30:37.613 回答
3

如果可以编译代码,请使用 ruby​​-gsl

gem install gsl

逆可以使用LU模块获得

inverse=GSL::Linalg::LU.invert(matrix)

于 2011-01-20T13:38:02.350 回答
1

尝试使用“矩阵”库:

http://www.ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html
于 2009-06-09T14:04:16.527 回答
0

NMatrix。支持各种操作,包括来自 BLAS 和 LAPACK 的一些操作(通过使用 ATLAS)。

于 2015-03-24T14:13:52.327 回答