I wrote some code to calculate IRR and it's works fine...
import scipy.optimize as optimize
import datetime
def npv(cf, rate=0.1):
if len(cf) >= 2:
first_date = min([x[0] for x in cf])
dcf = [x[1] * (1 /
((1 + rate) ** ((x[0] - first_date).days / 365))) for x in cf]
return sum(dcf)
elif len(cf) == 1:
return cf[0][1]
else:
return 0
def irr(cf):
f = lambda x: npv(cf, rate=x)
r = optimize.newton(f, 0, maxiter=70)
return r
...but when I try this cashflow
cf=[(datetime.datetime(2018, 1, 10), -51089.94),
(datetime.datetime(2022, 10, 6), 4941.0)]
I get this error:
File "/Users/maxim/Dropbox/Python/FinProject/fintrack/main/models.py", line 503, in getIRR
return irr(cf)
File "/Users/maxim/Dropbox/Python/FinProject/fintrack/main/models.py", line 37, in irr
r = optimize.newton(f, 0, maxiter=70)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scipy/optimize/zeros.py", line 204, in newton
q1 = func(p1, *args)
File "/Users/maxim/Dropbox/Python/FinProject/fintrack/main/models.py", line 36, in <lambda>
f = lambda x: npv(cf, rate=x)
File "/Users/maxim/Dropbox/Python/FinProject/fintrack/main/models.py", line 27, in npv
((1 + rate) ** ((x[0] - first_date).days / 365))) for x in cf]
File "/Users/maxim/Dropbox/Python/FinProject/fintrack/main/models.py", line 27, in <listcomp>
((1 + rate) ** ((x[0] - first_date).days / 365))) for x in cf]
OverflowError: complex exponentiation
[30/Nov/2018 21:28:36] "GET /inv/19/ HTTP/1.1" 500 299065
However I know that correct answer is -38.912..% I got this result by Excel. What is wrong here? With other data I get the same result as Excel IRR function... Should I use other function to find parameter?
P.S.: here is list of iteration's parameters and results (OMG complex numbers in finance %-) ):
rate= 0.0 result= -46148.94
rate= 0.0001 result= -46151.281226688276
rate= -1.9711435988300456 result= (-54972.27265283515-4141.40178622848j)
rate= (8.450859228811169-3.967580022971747j) result= (-51089.96465099011+0.07614432462298902j)
rate= (46.548868303534285-96.82120737804672j) result= (-51089.93999938349-9.638432563347345e-07j)
rate= (63880696.05472335+4880892.707757121j) result= (-51089.94-1.7420169038104924e-34j)
rate= (-1.3534185437764045e+18-2.52038641964956e+18j) result= (-51089.94-5.721141417411886e-85j)
rate= (-7.391799165398238e+56+3.969311207511089e+56j) result= (-51089.94+9.185514002355334e-269j)
rate= (3.5446051170119047e+145+6.600895665730368e+145j) result= ERROR!!!
(1 + rate) ** time
becomes complex, which is the error you get. In fact, at rate = -1 you have a discontinuity. You can use a diferent optimization method which allows the constraintrate > -100%
(orrate >= 0
), or use a bounded method (e.g.brentq
orsecant
) in a known interval, or usenewton
starting at an x0 which you know is below the IRR value (e.g.x0 = -0.95
)