Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions financial/present_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

def present_value(discount_rate: float, cash_flows: list[float]) -> float:
"""
>>> present_value(0.13, [10])
8.85
>>> present_value(0.13, [10, 20.70, -293, 297])
4.69
4.15
>>> present_value(0.07, [-109129.39, 30923.23, 15098.93, 29734,39])
-42739.63
-39943.58
>>> present_value(0.07, [109129.39, 30923.23, 15098.93, 29734,39])
175519.15
164036.59
>>> present_value(-1, [109129.39, 30923.23, 15098.93, 29734,39])
Traceback (most recent call last):
...
Expand All @@ -31,7 +33,7 @@ def present_value(discount_rate: float, cash_flows: list[float]) -> float:
if not cash_flows:
raise ValueError("Cash flows list cannot be empty")
present_value = sum(
cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(cash_flows)
cash_flow / ((1 + discount_rate) ** (i+1)) for i, cash_flow in enumerate(cash_flows)
)
return round(present_value, ndigits=2)

Expand Down