Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7be4aa

Browse files
committed
Python Rate of return
1 parent 3ce48c6 commit d7be4aa

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

‎taiyangxue/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- [python39](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/python39) : 你在享受十一长假时,Python 已悄悄地变了
1717
- [matrix](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/matrix) : Python 世界的黑客帝国
1818
- [why](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/why) : 练那么多,为啥还不会编程
19-
19+
-[rate](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/rate-of-return) : 练那么多,为啥还不会编程
2020
---
2121

2222
从小白到工程师的学习之路

‎taiyangxue/rate-of-return/XIRR.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# // Copyright (c) 2012 Sutoiku, Inc. (MIT License)
2+
3+
# // Some algorithms have been ported from Apache OpenOffice:
4+
5+
# /**************************************************************
6+
# *
7+
# * Licensed to the Apache Software Foundation (ASF) under one
8+
# * or more contributor license agreements. See the NOTICE file
9+
# * distributed with this work for additional information
10+
# * regarding copyright ownership. The ASF licenses this file
11+
# * to you under the Apache License, Version 2.0 (the
12+
# * "License"); you may not use this file except in compliance
13+
# * with the License. You may obtain a copy of the License at
14+
# *
15+
# * http://www.apache.org/licenses/LICENSE-2.0
16+
# *
17+
# * Unless required by applicable law or agreed to in writing,
18+
# * software distributed under the License is distributed on an
19+
# * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
# * KIND, either express or implied. See the License for the
21+
# * specific language governing permissions and limitations
22+
# * under the License.
23+
# *
24+
# *************************************************************/
25+
26+
27+
def years_between_dates(date1, date2):
28+
delta = date2 - date1
29+
return (delta.days / 365)
30+
31+
32+
# // Credits: algorithm inspired by Apache OpenOffice
33+
# // Calculates the resulting amount
34+
def irrResult(values, dates, rate):
35+
r = rate + 1
36+
result = values[0]
37+
for i in range(1, len(values)):
38+
result = result + values[i] / pow(r, years_between_dates(dates[0], dates[i]))
39+
i = i + 1
40+
return result
41+
42+
43+
# // Calculates the first derivation
44+
def irrResultDeriv(values, dates, rate):
45+
r = rate + 1
46+
result = 0
47+
for i in range(1, len(values)):
48+
frac = years_between_dates(dates[0], dates[i])
49+
result = result - frac * values[i] / pow(r, frac + 1)
50+
i = i + 1
51+
return result
52+
53+
54+
def xirr(values, dates):
55+
# // Check that values contains at least one positive value and one negative value
56+
positive = False
57+
negative = False
58+
for v in values:
59+
if v > 0:
60+
positive = True
61+
if v < 0:
62+
negative = True
63+
64+
# // Return error if values does not contain at least one positive value and one negative value
65+
if not (positive and negative):
66+
return 'Error'
67+
# // Initialize guess and resultRate
68+
guess = 0.1
69+
resultRate = guess
70+
71+
# // Set maximum epsilon for end of iteration
72+
epsMax = 1e-10
73+
74+
# // Set maximum number of iterations
75+
iterMax = 20
76+
77+
# // Implement Newton's method
78+
iteration = 0
79+
contLoop = True
80+
while contLoop and (iteration < iterMax):
81+
resultValue = irrResult(values, dates, resultRate)
82+
newRate = resultRate - (resultValue / irrResultDeriv(values, dates, resultRate))
83+
epsRate = abs(newRate - resultRate)
84+
resultRate = newRate
85+
if resultRate < -1:
86+
resultRate = -0.999999999
87+
contLoop = (epsRate > epsMax) and (abs(resultValue) > epsMax)
88+
iteration = iteration + 1
89+
if contLoop:
90+
return epsRate > epsMax, epsRate, 'iterMax'
91+
return resultRate

‎taiyangxue/rate-of-return/main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 年化复合回报 15%
2+
(1+0.15)**100
3+
# 1174313.4507002793
4+
5+
# 定投的收益
6+
import numpy_financial as npf
7+
npf.fv(0.1, 12, -1000, 0)
8+
# 21384.28376721003
9+
npf.pmt(0.1, 12, 0, 50000)
10+
# -2338.165755014362
11+
12+
13+
# 定期不定额的收益率
14+
pmts = [-1000, 100, -1300, -2000, 5200]
15+
npf.irr(pmts)
16+
# 0.10969579295711918
17+
18+
# 不定期不定额的收益率
19+
from XIRR import xirr
20+
import datetime
21+
22+
dates = [datetime.date(2019, 2,4),
23+
datetime.date(2019, 6, 17),
24+
datetime.date(2019,11, 18),
25+
datetime.date(2020,4, 27),
26+
datetime.date(2020,10, 19)]
27+
28+
values = [-300.3,-500.5,741.153,-600.6,1420.328547]
29+
30+
xirr(values, dates)
31+
# 输出为: 0.779790640991537

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /