Skip to content

added floating discount rate support #8834

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

Closed
wants to merge 4 commits into from

Conversation

Mike-gag
Copy link

@Mike-gag Mike-gag commented Jun 23, 2023

The existing algorithm provided the ability to calculate the present value of a series of cash flows with a fixed discount rate, i enhanced it so it works with a floating discount rate.
The basic functionallity has not been altered, the algorithm can be used as it was before without any problems.
Added doctests for new cases i introduced, also added new Errors and edge case tests.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.

@Mike-gag
Copy link
Author

@cclauss i'd really appreciate some feedback

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:
def present_value(discount_rates: list[tuple], cash_flows: list[float]) -> float:
"""
>>> present_value(0.13, [10, 20.70, -293, 297])
Copy link
Member

@cclauss cclauss Jun 26, 2023

Choose a reason for hiding this comment

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

I am confused as to why line 21 delivers 4.69 in the old code and 4.15 in the new.
Will it do the same if 0.13 is changed to [0.13] on line 21?
I would like us to keep the old tests (with modifications if required) to prove that the old and new are equivelent.

Copy link
Author

@Mike-gag Mike-gag Jun 26, 2023

Choose a reason for hiding this comment

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

  • I am confused as to why line 21 delivers 4.69 in the old code and 4.15 in the new.
    The formula produces 4.15, i have no idea why it was 4.69 in the first place. You can cross check with the formula found here. Another test came up with wrong results, fixed it as well.

  • Will it do the same if 0.13 is changed to [0.13] on line 21?
    no it will produce an error. The idea is if there is a fixed rate, the user need only provide a float number of the rate, and if its a floating rate, the user provides a list of tuples.There is no instance where a variable of type [float] is provided.

Copy link
Author

Choose a reason for hiding this comment

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

@cclauss the enumerate() function starts counting from 0 where i in
cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(cash_flows) should start counting from 1, not 0, hence the computational error in the previous iteration. My implementation produces the correct result.

Copy link
Member

Choose a reason for hiding this comment

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

What does enumerate(cash_flows, 1) do?

Copy link
Member

Choose a reason for hiding this comment

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

Should
def present_value(discount_rates: list[tuple] be changed to
def present_value(discount_rates: float | list[tuple]?

Copy link
Author

Choose a reason for hiding this comment

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

Should def present_value(discount_rates: list[tuple] be changed to def present_value(discount_rates: float | list[tuple]?

Yes that's better, changing it rn

Copy link
Author

Choose a reason for hiding this comment

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

What does enumerate(cash_flows, 1) do?

It fixes the problem but i think that's beside the point. The test cases in place had different results than my tests because the previous implementation calculated wrong results. My implementation provides the correct results which is why there were differences between the tests.

Copy link
Author

Choose a reason for hiding this comment

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

@cclauss do i resolve the conversation or do i leave this up to reviewers? I'd appreciate a review at this time

Co-authored-by: Christian Clauss <cclauss@me.com>
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Jun 26, 2023
@cclauss cclauss closed this Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants