我正在尝试计算XIRR的值。数据框“ f”具有日期,现金,费率,名称列。但是我收到错误“ OverflowError:(34,'结果太大')”。请帮助我修复代码以纠正此错误。
def xnpv(rate, values, dates):
if rate <= -1.0:
return float('inf')
d0 = dates.min() # or min(dates)
return sum([ vi / (1.0 + rate)**((di - d0).days / 365.0) for vi, di in zip(values, dates)])
def xirr(values, dates):
try:
return scipy.optimize.newton(lambda r: xnpv(r, values, dates), 0.0)
except RuntimeError: # Failed to converge?
return scipy.optimize.brentq(lambda r: xnpv(r, values, dates), -1.0, 1e10)
def fun(x):
x["XNPV"] = xnpv(0.1, x["Cash"], x['Due Date'])
x["XIRR"] = xirr( x["Cash"], x['Due Date'])
return x
f2 = f.groupby('Rate').apply(fun)