marksweston / finance

A library for financial calculations in Ruby
https://rubygems.org/gems/finance
Other
217 stars 93 forks source link

IRR calculation errors: Singular Jacobian matrix and Failed to reduce function values. #24

Open bdeterling opened 10 years ago

bdeterling commented 10 years ago

I've been getting this error on many different series. I've tried various versions of Ruby 1.9 and 2.0. 1.9 tends to hang whereas 2.0 errors out fairly quickly. I've tried these same calculations in both Excel and the Java Apache POI library and they return good values. Any ideas?

Some examples:

[-1,-2,0,5].irr.to_f => 0.24189656303203824

[-1,-3,0,5].irr.to_f RuntimeError: Failed to reduce function values (newton.rb:66 in nlsolve)

[-13000,-3000,-3000,0,0,5000,10000,20000].irr.to_f RuntimeError: Singular Jacobian matrix. No change at x[0](jacobian.rb:58 in dfdxi)

tubedude commented 10 years ago

I've added a pull request that adds a guess param to IRR and XIRR. https://github.com/wkranec/finance/pull/30

The error I mentioned was solved by:

[-13000,-3000,-3000,0,0,5000,10000,20000].irr(0.5).to_f
# => 0.10803084598726499
marksweston commented 8 years ago

@tubedude as you've closed your own pull request rather than merge it in, I get the impression this is still an open issue. So what next?

tubedude commented 8 years ago

Yes. I can try to work on a better implementation. My solution is to guess an IRR and add this guess to the XIRR method. Today the XIRR method takes 1.0 as the initial guess which can leads to error like these. What do you think about it?