Anexen / pyxirr

Rust-powered collection of financial functions.
https://anexen.github.io/pyxirr/
The Unlicense
172 stars 16 forks source link

IRR function args for tolerance and max iterations #45

Closed Hvass-Labs closed 1 year ago

Hvass-Labs commented 1 year ago

Thanks for making this library!

Your irr function is about 15x faster than numpy_financial on my data.

Would it be possible to add arguments for the convergence-tolerance and max number of iterations for the root-finding algorithm? It seems they have recently added these to numpy_financial but it is not yet released.

I would like to try and make irr run even faster by sacrificing some precision of the result, and also to ensure the time-usage is limited and predictable. Because I need to run this on many thousands of arrays of data, as fast as possible, and I only need a few digits of precision.

There also seems to be a problem when the first number of the array is positive but there are negative numbers for some of the future amounts. This example runs 400x slower than normal and does not return a value or raise an exception:

from pyxirr import irr
x = [10.0, 1.0, 2.0, -3.0, 4.0]
irr(x)

I need it to return np.nan when a result could not be found within the max number of iterations allowed and the error is not within the given tolerance level.

By the way, pyxirr.__version__ is apparently also missing.

I hope you can make these small changes.

Thanks!

Anexen commented 1 year ago

Thank you for submitting an issue. I agree that there is a problem with the function being slow when there is no solution. This happens because several algorithms for finding roots are used internally. I'm working on the efficiency of finding roots to return None as soon as possible. I'm not sure that I can give the ability to control precision. As I wrote above, IRR uses several algorithms for finding roots and they use a different number of iterations and tolerance.

Anexen commented 1 year ago

Hi @Hvass-Labs, I fixed an issue with IRR being slow when there is no solution in v0.9.3 + improved an overall performance. I didn't add the ability to control the precision, because IRR uses several root-finding algorithms and they use a different number of iterations and tolerance. I'll probably add the ability to select an algorithm instead of trying different options automatically.

Hvass-Labs commented 1 year ago

Thanks! I actually ended up making my own Cython version, but I appreciate your effort and I'm sure someone else will find it useful! :-)