Skip to content

Financials #5585

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
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions financials/ABOUT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Interest

* Compound Interest: "Compound interest is calculated by multiplying the initial principal amount by one plus the annual interest rate raised to the number of compound periods minus one." [Compound Interest](https://www.investopedia.com/)
* Simple Interest: "Simple interest paid or received over a certain period is a fixed percentage of the principal amount that was borrowed or lent. " [Simple Interest](https://www.investopedia.com/)
*
Empty file added financials/__init__.py
Empty file.
84 changes: 84 additions & 0 deletions financials/interest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from __future__ import annotations


def simple_interest(
principle: float, daily_interest_rate: float, number_of_days_between_payments: int
) -> float:
"""
>>> simple_interest(18000.0,0.06,3)
3240.0
Copy link
Member

@cclauss cclauss Oct 24, 2021

Choose a reason for hiding this comment

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

Add tests dealing with zero principle, negative principle, zero interest rate, negative interest rate, zero days, negative days, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cclauss done

>>> simple_interest(0.0,0.06,3)
0.0
>>> simple_interest(18000.0,0.01,10)
1800.0
>>> simple_interest(18000.0,0.0,3)
0.0
>>> simple_interest(5500.0,0.01,100)
5500.0
>>> simple_interest(10000.0,-0.06,3)
Traceback (most recent call last):
...
ValueError: daily_interest_rate must be >= 0
>>> simple_interest(-10000.0,0.06,3)
Traceback (most recent call last):
...
ValueError: principle must be >= 0
>>> simple_interest(5500.0,0.01,-5)
Traceback (most recent call last):
...
ValueError: number_of_days_between_payments must be >= 0
"""
if number_of_days_between_payments < 0:
raise ValueError("number_of_days_between_payments must be >= 0")
if daily_interest_rate < 0.0:
raise ValueError("daily_interest_rate must be >= 0")
if principle < 0.0:
raise ValueError("principle must be >= 0")

return principle * daily_interest_rate * number_of_days_between_payments


def compound_interest(
principle: float,
nominal_annual_interest_rate_percentage: float,
number_of_compounding_periods: int,
) -> float:
"""
>>> compound_interest(10000.0,0.05,3)
1576.2500000000014
>>> compound_interest(10000.0,0.05,0)
0.0
>>> compound_interest(10000.0,0.05,1)
500.00000000000045
>>> compound_interest(0.0,0.05,3)
0.0
>>> compound_interest(10000.0,0.06,-4)
Traceback (most recent call last):
...
ValueError: number_of_compounding_periods must be >= 0
>>> compound_interest(10000.0,-3.5,3.0)
Traceback (most recent call last):
...
ValueError: nominal_annual_interest_rate_percentage must be >= 0
>>> compound_interest(-5500.0,0.01,5)
Traceback (most recent call last):
...
ValueError: principle must be >= 0
"""
if number_of_compounding_periods < 0:
raise ValueError("number_of_compounding_periods must be >= 0")
if nominal_annual_interest_rate_percentage < 0.0:
raise ValueError("nominal_annual_interest_rate_percentage must be >= 0")
if principle < 0.0:
raise ValueError("principle must be >= 0")

return principle * (
(1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods
- 1
)


if __name__ == "__main__":
import doctest

doctest.testmod()