-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add equated_monthly_installments.py in Financials #5775
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
33535c6
Add equated_monthly_installments.py in Financials
Rohanrbharadwaj 56b7dce
Formatting
Rohanrbharadwaj 07db4a3
More formatting, Descriptive names
Rohanrbharadwaj 458beb7
Errors with name change
Rohanrbharadwaj 1cbf10d
Formatting
Rohanrbharadwaj 556faf6
Formatting, Naming Error
Rohanrbharadwaj dae8014
dedent
Rohanrbharadwaj b5da874
Update DIRECTORY.md
Rohanrbharadwaj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
Program to calculate the amortization amount per month, given | ||
- Principal borrowed (p) | ||
- Rate of interest per annum (r) | ||
- Years to repay the loan (y) | ||
|
||
Wikipedia Reference: https://en.wikipedia.org/wiki/Equated_monthly_installment | ||
""" | ||
|
||
|
||
def equated_monthly_installments(p: float, r: float, y: int) -> float: | ||
Rohanrbharadwaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Formula for amortization amount per month: | ||
A = p * r * (1 + r)^n / ((1 + r)^n - 1) | ||
where p is the principal, r is the rate of interest per month | ||
and n is the number of payments | ||
|
||
>>> equated_monthly_installments(25000, 0.12, 3) | ||
830.3577453212793 | ||
>>> equated_monthly_installments(25000, 0.12, 10) | ||
358.67737100646826 | ||
>>> installments(0, 0.12, 3) | ||
Traceback (most recent call last): | ||
... | ||
Exception: Principal borrowed must be > 0 | ||
>>> installments(25000, -1, 3) | ||
Traceback (most recent call last): | ||
... | ||
Exception: Rate of interest must be >= 0 | ||
>>> installments(25000, 0.12, 0) | ||
Traceback (most recent call last): | ||
... | ||
Exception: Years to repay must be an integer > 0 | ||
""" | ||
if p <= 0: | ||
raise Exception("Principal borrowed must be > 0") | ||
if r < 0: | ||
raise Exception("Rate of interest must be >= 0") | ||
if y <= 0 or not isinstance(y, int): | ||
raise Exception("Years to repay must be an integer > 0") | ||
|
||
# Yearly rate is divided by 12 to get monthly rate | ||
rate_per_month = r_m = r / 12 | ||
|
||
# Years to repay is multiplied by 12 to get number of payments as payment is monthly | ||
number_of_payments = n = y * 12 | ||
|
||
return p * r_m * (1 + r_m)**n / ((1 + r_m)**n - 1) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.