forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresent_value.py
37 lines (27 loc) · 1.26 KB
/
present_value.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
# Reference: https://www.investopedia.com/terms/p/presentvalue.asp
# Algorithm that calculates the present value of a stream of yearly cash flows given...
# 1. The discount rate (as a decimal, not a percent)
# 2. An array of cash flows, with the index of the cash flow being the associated year
# Note: This algorithm assumes that cash flows are paid at the end of the specified year
def present_value(discount_rate: float, cash_flows: list[float]) -> float:
"""
>>> round(present_value(0.13, [10, 20.70, -293, 297]), 2)
4.69
>>> round(present_value(0.07, [-109129.39, 30923.23, 15098.93, 29734,39]), 2)
-42739.63
>>> round(present_value(0.07, [109129.39, 30923.23, 15098.93, 29734,39]), 2)
175519.15
>>> present_value(-1, [109129.39, 30923.23, 15098.93, 29734,39])
Traceback (most recent call last):
...
ValueError: Invalid discount rate, please choose a rate other than -1
"""
present_value = 0.0
if discount_rate == -1:
raise ValueError("Invalid discount rate, please choose a rate other than -1")
for idx, cash_flow in enumerate(cash_flows):
present_value += cash_flow / ((1 + discount_rate) ** idx)
return present_value
if __name__ == "__main__":
import doctest
doctest.testmod()