-
Notifications
You must be signed in to change notification settings - Fork 0
/
irr.py
77 lines (62 loc) · 1.69 KB
/
irr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from functools import partial
def month_ratio(year_rate, month):
return (1 + year_rate / 12) ** month
def binary_search(l, r, check):
while l + 1e-8 < r:
mid = (l + r) / 2
if check(mid):
l = mid
else:
r = mid
return l
def judge(
r,
principal,
rebate,
inflow_list,
info=False,
):
inflow_total_present = 0
for idx, inflow in enumerate(inflow_list):
t = idx + 1
inflow_term_present = inflow / month_ratio(r, t)
if info:
print("第{}期\t{}".format(t, inflow_term_present))
inflow_total_present += inflow_term_present
cost_total_present = principal - rebate
diff = inflow_total_present - cost_total_present
if info:
print("流入现值总和\t{}".format(inflow_total_present))
print("流出现值总和\t{}".format(cost_total_present))
return diff > 0
def irr(principal, rebate, inflow_list, info):
"""
principal 本金
rebate 回扣
inflow_list 每期现金流
"""
check = partial(
judge,
principal=principal,
rebate=rebate,
inflow_list=inflow_list,
info=info,
)
check_with_info = partial(
judge,
principal=principal,
rebate=rebate,
inflow_list=inflow_list,
info=True,
)
r = binary_search(0.0, 10.0, check)
check_with_info(r)
print("\n-------------\n年化利率: {:.2f}%\n-------------\n".format(r * 100))
inflow_list = [9696 for _ in range(143)]
inflow_list.append(76305.06)
irr(
principal=1.2e6,
rebate=0,
inflow_list=inflow_list,
info=False,
)