From 3a74af44233b51130970bd29ef181b956496a7e8 Mon Sep 17 00:00:00 2001 From: Kwodwo Graham Date: Fri, 24 Nov 2023 14:50:58 +0000 Subject: [PATCH] Update present_value.py >>> present_value(0.13, [10]) == 8.85 however the output was 10 because enumerate started with the index 0 and and anything raised to power 0 is equal to 1. Additionally the assumption may not hold for the first year because the end of the associated/specified year 0 is a year from present. Tested the present value of 10 at 13% using the Present Value Calculator here at https://www.calculator.net/present-value-calculator.html?c1futurevalue=10&c1yearsv=1&c1interestratev=13&x=Calculate#future-money --- financial/present_value.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/financial/present_value.py b/financial/present_value.py index f74612b923af..d01d8aeec646 100644 --- a/financial/present_value.py +++ b/financial/present_value.py @@ -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): ... @@ -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)