views:

633

answers:

2

I need to inverse a variance-covariance matrix in Ruby and vector by matrix multiplication. Which numerical Ruby library/Gem should I use?

A: 

Try using the 'matrix' library:

http://www.ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html
fd
+2  A: 

A numerically more stable possibility than direct inversion is to use a Cholesky decomposition with the package you find here:

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

Better than inverting l is to use the method described in the wikpedia article linked above.

If your problem is numerically too unstable then consider the Singular Value Decomposition, but I don't have code for it.