Anexen / pyxirr

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

Negative IRR #24

Closed aluyca closed 2 years ago

aluyca commented 2 years ago

Hi

Pyxirr is really a good package. The performance is really good. When we using irr function, got a weird issue. If we feed 241, 361, or 481 cash flows (including 1 initial loan, and 240,360, and 480 monthly paybacks) to irr function, we got a negative number (-23.848998449508812). Can you please take a look and help us to fix it?

Here is our testing code:

from pyxirr import irr
import numpy_financial as npf

cf = [-172545.848122807] + [787.735232517999] * 480

print(len(cf))
irr_rt= irr(cf)*12
print(irr_rt)

irr_rt = npf.irr(cf)*12
print(irr_rt)
Anexen commented 2 years ago

Hi @aluyca, it looks like the root finding algorithm found just one of several solutions to the equation. An interesting point: LibreOffice shows the same result as pyxirr and mathematically it is correct because npv(pyxirr.irr(cf), cf) is close to zero and it's closer to zero than npv(npf.irr(cf), cf). I'll see what I can do

aluyca commented 2 years ago

Thanks a lot!

Anexen commented 2 years ago

Hi @aluyca , I've tweaked IRR so that it first tries to find a rate greater than zero. I hope this helps. Please try version 0.7.2.

aluyca commented 2 years ago

Thanks!