Skip to content

Commit f6d7103

Browse files
committed
formatting fixes in financial/macaulay_duration
1 parent 53c0047 commit f6d7103

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

financial/macaulay_duration.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1+
"""
2+
Calculate the Macaulay Duration of a bond.
3+
Reference: https://www.investopedia.com/terms/m/macaulayduration.asp
4+
"""
5+
16
from __future__ import annotations
27

8+
39
def macaulay_duration(
410
face_value: float,
511
coupon_rate: float,
612
periods: int,
7-
yield_rate: float
13+
yield_rate: float,
814
) -> float:
915
"""
1016
Calculates the Macaulay Duration of a bond.
1117
12-
Reference:
13-
https://www.investopedia.com/terms/m/macaulayduration.asp
14-
15-
Args:
16-
face_value: The final payout amount of the bond.
17-
coupon_rate: The annual interest rate paid by the bond.
18-
periods: The number of years until the bond matures.
19-
yield_rate: The current market interest rate used to discount future cash flows.
20-
21-
Returns:
22-
The Macaulay Duration of the bond in years.
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.
2324
2425
>>> round(macaulay_duration(1000.0, 0.05, 8, 0.04), 2)
2526
6.83
@@ -57,7 +58,9 @@ def macaulay_duration(
5758
total_time_weighted_value: float = 0.0
5859

5960
for period in range(1, periods + 1):
60-
cash_flow: float = (face_value * coupon_rate) + (face_value if period == periods else 0)
61+
cash_flow: float = face_value * coupon_rate + (
62+
face_value if period == periods else 0
63+
)
6164

6265
time_weighted_value: float = (period * cash_flow) / pow(1 + yield_rate, period)
6366
total_time_weighted_value += time_weighted_value
@@ -67,6 +70,7 @@ def macaulay_duration(
6770

6871
return total_time_weighted_value / total_present_value
6972

73+
7074
if __name__ == "__main__":
7175
import doctest
7276

0 commit comments

Comments
 (0)