- Python计算IRR公式
from scipy import optimize
# 计算现值
def xnpv(rate, cashflows):
return sum([cf / (1 + rate) ** ((t-cashflows[0][0]).days/365.0) for (t, cf) in cashflows])
# 计算xirr
def xirr(cashflows, guess=.01):
try:
return optimize.newton(lambda r: xnpv(r, cashflows), guess)
except:
# print('无法计算irr')
# return -1
try:
guess = -0.5
return optimize.newton(lambda r: xnpv(r, cashflows), guess)
except:
return 0
cashflows = [(datetime.date(2021, 2, 9), -50000), (datetime.date(2021, 2, 9), -30000)]
标签:guess,return,python,公式,IRR,optimize,xnpv,cashflows
From: https://www.cnblogs.com/feel-myself/p/17445602.html