-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Conversation
@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]) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 and4.15
in the new.
The formula produces4.15
, i have no idea why it was4.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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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]
?
There was a problem hiding this comment.
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 todef present_value(discount_rates: float | list[tuple]
?
Yes that's better, changing it rn
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
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.