peliot / XIRR-and-XNPV

python implementation of Microsoft Excel's XNPV and XIRR
53 stars 23 forks source link

ValueError: negative number cannot be raised to a fractional power #3

Closed enpassanty closed 9 years ago

enpassanty commented 9 years ago

I tried using the function to calculate XIRR for the following cashflow:

[(datetime.date(2011, 12, 29), -9000), (datetime.date(2012, 1, 29), 305.38), (datetime.date(2012, 2, 29), 305.38), (datetime.date(2012, 3, 29), 305.38), (datetime.date(2012, 4, 29), 305.38), (datetime.date(2012, 5, 29), 305.38), (datetime.date(2012, 6, 29), 305.38), (datetime.date(2012, 7, 29), 305.38), (datetime.date(2012, 8, 29), 133.04)]

which returns the following error:

return sum([cf/(1+rate)**((t-t0).days/365.0) for (t,cf) in chron_order]) ValueError: negative number cannot be raised to a fractional power

Any idea what's going wrong?

peliot commented 9 years ago

Sorry it is not working for you. Here's a quick explanation and some other thoughts. XIRR is a numerical solver (as you see from the code), which is solving for the discount rate at which NPV=0. When you have a series of cash flows with a large negative return (as you do here), NPV as a function of discount rate looks pretty ugly as function, in such a way that makes it difficult for numerical solvers to converge on a solution. I wish the error messages were more informative, but when you get the error message you got, it really means that the solver could not converge on a solution (the reason for that particular error message is that the solver is testing a value for the discount rate for which NPV has no solution). Some other thoughts:

Regards, Philip

On Fri, Jan 9, 2015 at 10:19 PM, enpassanty notifications@github.com wrote:

I tried using the function to calculate XIRR for the following cashflow:

[(datetime.date(2011, 12, 29), -9000), (datetime.date(2012, 1, 29), 305.38), (datetime.date(2012, 2, 29), 305.38), (datetime.date(2012, 3, 29), 305.38), (datetime.date(2012, 4, 29), 305.38), (datetime.date(2012, 5, 29), 305.38), (datetime.date(2012, 6, 29), 305.38), (datetime.date(2012, 7, 29), 305.38), (datetime.date(2012, 8, 29), 133.04)]

which returns the following error:

return sum([cf/(1+rate)**((t-t0).days/365.0) for (t,cf) in chron_order]) ValueError: negative number cannot be raised to a fractional power

Any idea what's going wrong?

— Reply to this email directly or view it on GitHub https://github.com/peliot/XIRR-and-XNPV/issues/3.

enpassanty commented 9 years ago

thanks