marksweston / finance

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

irr and xirr could benefit from initial guess and number of iterations #25

Open Janther opened 10 years ago

Janther commented 10 years ago

In other implementations of the IRR function, one can set the initial guess and the number of iterations.

Here the irr function doesn't receive any parameter and the xirr receives the number of iterations but i don't see the function make use of this value.

is there a reason for this?

PS: I'm looking at the https://github.com/wkranec/finance/blob/master/lib/finance/cashflows.rb file PS2: also the tolerance could be nice. For example tolerance = 0.00000001

arbrown commented 10 years ago

Hi Janther. It looks like nlsolve (in the BigDecimal Module, newton.rb) doesn't have any code to handle a specific number of iterations: http://svn.codehaus.org/mojo/trunk/mojo/jruby-stdlib/src/main/resources/bigdecimal/newton.rb

On the other hand, you could manually set the tolerance. It would require changing the canned value that every Function object returns. I'll be digging around the code this weekend anyway, so I could submit a pull request to add this functionality if it would be useful to you. Although, the current tolerance is 1.0e-16, which is pretty small, so unless you know you want it to be less accurate than that for speed reasons, there's probably no reason to change it.

tubedude commented 10 years ago

Ok. I have been "spamming" about this, but I was able to add a Guess on IRR and XIRR in #30 But, Janther, you are right, I didn't notice XIIR iterations didn't had any use in the formulas. I'll update the pull request to remove it.

arbrown, Indeed the tolerance seems quite low. I've added a Config method to Finance that allows you to set a default Tolerance in the same PR #30.