Skip to content

Commit e7d2b84

Browse files
Feat macaulay duration (#14506)
* feat: add macaulay duration algorithm * formatting fixes in financial/macaulay_duration * updating DIRECTORY.md --------- Co-authored-by: Max <183308611+max938-coder@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 89a9601 commit e7d2b84

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@
529529
* [Exponential Moving Average](financial/exponential_moving_average.py)
530530
* [Interest](financial/interest.py)
531531
* [Kelly Criterion](financial/kelly_criterion.py)
532+
* [Macaulay Duration](financial/macaulay_duration.py)
532533
* [Present Value](financial/present_value.py)
533534
* [Price Plus Tax](financial/price_plus_tax.py)
534535
* [Sharpe Ratio](financial/sharpe_ratio.py)

‎financial/macaulay_duration.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
Calculate the Macaulay Duration of a bond.
3+
Reference: https://www.investopedia.com/terms/m/macaulayduration.asp
4+
"""
5+
6+
from __future__ import annotations
7+
8+
9+
def macaulay_duration(
10+
face_value: float,
11+
coupon_rate: float,
12+
periods: int,
13+
yield_rate: float,
14+
) -> float:
15+
"""
16+
Calculates the Macaulay Duration of a bond.
17+
18+
:param face_value: The final payout amount of the bond.
19+
:param coupon_rate: The annual interest rate paid by the bond.
20+
:param periods: The number of years until the bond matures.
21+
:param yield_rate: The current market interest rate used to discount
22+
future cash flows.
23+
:return: The Macaulay Duration of the bond in years.
24+
25+
>>> round(macaulay_duration(1000.0, 0.05, 8, 0.04), 2)
26+
6.83
27+
>>> round(macaulay_duration(987435.34, 0.07, 5, 0.038), 2)
28+
4.43
29+
>>> round(macaulay_duration(3564.2, 0.023, 6, 0.071), 2)
30+
5.62
31+
>>> macaulay_duration(-1000.0, 0.05, 8, 0.04)
32+
Traceback (most recent call last):
33+
...
34+
ValueError: face_value must be > 0
35+
>>> macaulay_duration(1000.0, -0.05, 8, 0.04)
36+
Traceback (most recent call last):
37+
...
38+
ValueError: coupon_rate must be >= 0
39+
>>> macaulay_duration(1000.0, 0.05, 0, 0.04)
40+
Traceback (most recent call last):
41+
...
42+
ValueError: periods must be > 0
43+
>>> macaulay_duration(1000.0, 0.05, 8, -0.04)
44+
Traceback (most recent call last):
45+
...
46+
ValueError: yield_rate must be > 0
47+
"""
48+
if face_value <= 0:
49+
raise ValueError("face_value must be > 0")
50+
if coupon_rate < 0:
51+
raise ValueError("coupon_rate must be >= 0")
52+
if periods < 1:
53+
raise ValueError("periods must be > 0")
54+
if yield_rate <= 0:
55+
raise ValueError("yield_rate must be > 0")
56+
57+
total_present_value: float = 0.0
58+
total_time_weighted_value: float = 0.0
59+
60+
for period in range(1, periods + 1):
61+
cash_flow: float = face_value * coupon_rate + (
62+
face_value if period == periods else 0
63+
)
64+
65+
time_weighted_value: float = (period * cash_flow) / pow(1 + yield_rate, period)
66+
total_time_weighted_value += time_weighted_value
67+
68+
present_value: float = cash_flow / pow(1 + yield_rate, period)
69+
total_present_value += present_value
70+
71+
return total_time_weighted_value / total_present_value
72+
73+
74+
if __name__ == "__main__":
75+
import doctest
76+
77+
doctest.testmod()

0 commit comments

Comments
 (0)