gpt4 book ai didi

python - 用 Python 计算 IRR

转载 作者:太空宇宙 更新时间:2023-11-04 04:25:39 24 4
gpt4 key购买 nike

我写了一些代码来计算 IRR,它运行良好...

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

...但是当我尝试这个现金流时

cf=[(datetime.datetime(2018, 1, 10), -51089.94),
(datetime.datetime(2022, 10, 6), 4941.0)]

我收到这个错误:

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

但是我知道正确答案是 -38.912..% 我用 Excel 得到了这个结果。这里有什么问题?对于其他数据,我得到与 Excel IRR 函数相同的结果...我应该使用其他函数来查找参数吗?

P.S.:这是迭代参数和结果的列表(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!!!

最佳答案

似乎 newton 无法正确处理这个问题(不确定原因)。但是,您可以使用 root这给了你预期的结果,只需要对你的代码进行非常小的修改:

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.root(f, [0])
return r

cf = [(datetime.datetime(2018, 1, 10), -51089.94),
(datetime.datetime(2022, 10, 6), 4941.0)]

print(irr(cf))

这将打印:

    fjac: array([[-1.]])
fun: array([-2.91038305e-11])
message: 'The solution converged.'
nfev: 18
qtf: array([2.37701897e-06])
r: array([396401.23327105])
status: 1
success: True
x: array([-0.38912302])

如您所见,x 包含预期的 -0.38912

关于python - 用 Python 计算 IRR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53563674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com