我知道我的问题在这里得到了回答..具有 xirr 和 xnpv 功能的金融 python 库?..
def xirr(transactions):
years = [(ta[0] - transactions[0][0]).days / 365.0 for ta in transactions]
residual = 1
step = 0.05
guess = 0.05
epsilon = 0.0001
limit = 10000
while abs(residual) > epsilon and limit > 0:
limit -= 1
residual = 0.0
for i, ta in enumerate(transactions):
residual += ta[1] / pow(guess, years[i])
if abs(residual) > epsilon:
if residual > 0:
guess += step
else:
guess -= step
step /= 2.0
return guess-1
当我为基金交易执行上述代码时,我在第一次迭代中得到负残差。guess=0.5-0.5=0 因此,我在下一次迭代中得到zerodivisionerror 。为什么我得到负残差..如何处理这个?/提前谢谢