Skip to content

Added an algorithm to calculate the present value of cash flows #8700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 30, 2023
Merged
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
33 changes: 33 additions & 0 deletions financial/present_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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

from typing import list, tuple


def present_value(discount_rate: float, cash_flows: list[float]) -> tuple[float, str]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file financial/present_value.py, please provide doctest for the function present_value

present_value = 0.0

if discount_rate == -1:
return (0, "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,
"The present value of the given yearly cash flows at a "
+ str(round(discount_rate * 100, 2))
+ "% discount rate is $"
+ str(round(present_value, 2)),
)


if __name__ == "__main__":
import doctest

doctest.testmod()