# python二分法求NPV=0时的内部收益率

Python 专栏收录该内容
4 篇文章 0 订阅

def NPV_numpy( r ):
return np.sum(-19900. / (1+r)**np.arange(1, 16)) + \
np.sum(40000. / (1+r)**np.arange(19, 23)) + \
100000. / (1 + r)**30 + \
np.sum(50000. / (1 + r)**np.arange(65, 86)) + \
900000. / (1 + r)**85


def IRR(r_low=0, r_up=0.1, epsilon=1e-8):
n = 0
while True:
r_mid = (r_low + r_up) / 2
n += 1
#print(r_low, r_mid, r_up)
#print(NPV(r_low), NPV(r_mid), NPV(r_up))
if r_mid - r_low <= epsilon or r_up - r_mid <= epsilon:
return r_mid, n #设置好终止条件及返回结果
elif NPV(r_mid) * NPV(r_low) >= 0:
r_low = r_mid #中点和左端点函数值同号，说明零点在右端，将中点赋值给左端点
else:
r_up = r_mid #中点和左端点函数值异号，说明零点在左端，将中点赋值给右端点


irr, n = IRR()
print('IRR = {0:.6%}, iterative: {1:d} times.'.format(irr, n))
print('NPV = {0:.4f}'.format(NPV(irr)))


def NPV(r):
s = 0
for i in range(1, 16):
s += -19900./(1 + r)**i
for i in range(19, 23):
s += 40000./(1 + r)**i
s += 100000./(1+r)**30
for i in range(65,86):
s += 50000./(1 + r)**i
s += 900000./(1 + r)**85
return s

• 0
点赞
• 3
评论
• 1
收藏
• 一键三连
• 扫一扫，分享海报

02-25

04-16 1772
07-22 3892
03-23 1224
02-10 4134
02-25
11-15
12-10
07-17 4848
10-12 3900
08-30 1万+