Skip to content

Commit a354342

Browse files
sahilg13pre-commit-ci[bot]cclauss
authored andcommitted
Added an algorithm to calculate the present value of cash flows (TheAlgorithms#8700)
* Added an algorithm to calculate the present value of cash flows * added doctest and reference * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Resolving deprecation issues with typing module * Fixing argument type checks and adding doctest case * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixing failing doctest case by requiring less precision due to floating point inprecision * Updating return type * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added test cases for more coverage * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Make improvements based on Rohan's suggestions * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update financial/present_value.py Committed first suggestion Co-authored-by: Christian Clauss <cclauss@me.com> * Update financial/present_value.py Committed second suggestion Co-authored-by: Christian Clauss <cclauss@me.com> * Update financial/present_value.py Committed third suggestion Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 0fe7cf1 commit a354342

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Diff for: financial/present_value.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Reference: https://www.investopedia.com/terms/p/presentvalue.asp
3+
4+
An algorithm that calculates the present value of a stream of yearly cash flows given...
5+
1. The discount rate (as a decimal, not a percent)
6+
2. An array of cash flows, with the index of the cash flow being the associated year
7+
8+
Note: This algorithm assumes that cash flows are paid at the end of the specified year
9+
10+
11+
def present_value(discount_rate: float, cash_flows: list[float]) -> float:
12+
"""
13+
>>> present_value(0.13, [10, 20.70, -293, 297])
14+
4.69
15+
>>> present_value(0.07, [-109129.39, 30923.23, 15098.93, 29734,39])
16+
-42739.63
17+
>>> present_value(0.07, [109129.39, 30923.23, 15098.93, 29734,39])
18+
175519.15
19+
>>> present_value(-1, [109129.39, 30923.23, 15098.93, 29734,39])
20+
Traceback (most recent call last):
21+
...
22+
ValueError: Discount rate cannot be negative
23+
>>> present_value(0.03, [])
24+
Traceback (most recent call last):
25+
...
26+
ValueError: Cash flows list cannot be empty
27+
"""
28+
if discount_rate < 0:
29+
raise ValueError("Discount rate cannot be negative")
30+
if not cash_flows:
31+
raise ValueError("Cash flows list cannot be empty")
32+
present_value = sum(
33+
cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(cash_flows)
34+
)
35+
return round(present_value, ndigits=2)
36+
37+
38+
if __name__ == "__main__":
39+
import doctest
40+
41+
doctest.testmod()

0 commit comments

Comments
 (0)