From 33535c68f5c0f27baf4c178f0b65ad63c4120de7 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 03:05:40 +0530 Subject: [PATCH 1/8] Add equated_monthly_installments.py in Financials --- financial/equated_monthly_installments.py | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 financial/equated_monthly_installments.py diff --git a/financial/equated_monthly_installments.py b/financial/equated_monthly_installments.py new file mode 100644 index 000000000000..61cad8795a0b --- /dev/null +++ b/financial/equated_monthly_installments.py @@ -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: + """ + 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() From 56b7dce1f9ec0d78093ca5ff570099fb63250499 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 03:12:40 +0530 Subject: [PATCH 2/8] Formatting --- financial/equated_monthly_installments.py | 78 +++++++++++------------ 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/financial/equated_monthly_installments.py b/financial/equated_monthly_installments.py index 61cad8795a0b..d66ca0143a68 100644 --- a/financial/equated_monthly_installments.py +++ b/financial/equated_monthly_installments.py @@ -9,46 +9,46 @@ def equated_monthly_installments(p: float, r: float, y: int) -> float: - """ - 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) + """ + 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 + >>> equated_monthly_installments(0, 0.12, 3) + Traceback (most recent call last): + ... + Exception: Principal borrowed must be > 0 + >>> equated_monthly_installments(25000, -1, 3) + Traceback (most recent call last): + ... + Exception: Rate of interest must be >= 0 + >>> equated_monthly_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 + import doctest - doctest.testmod() + doctest.testmod() From 07db4a3f739335dbe75752e97785ea93fbd4de32 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 03:18:09 +0530 Subject: [PATCH 3/8] More formatting, Descriptive names --- financial/equated_monthly_installments.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/financial/equated_monthly_installments.py b/financial/equated_monthly_installments.py index d66ca0143a68..38654cc2b50b 100644 --- a/financial/equated_monthly_installments.py +++ b/financial/equated_monthly_installments.py @@ -1,14 +1,16 @@ """ Program to calculate the amortization amount per month, given -- Principal borrowed (p) -- Rate of interest per annum (r) -- Years to repay the loan (y) +- Principal borrowed +- Rate of interest per annum +- Years to repay the loan Wikipedia Reference: https://en.wikipedia.org/wiki/Equated_monthly_installment """ -def equated_monthly_installments(p: float, r: float, y: int) -> float: +def equated_monthly_installments( + principal: float, rate_per_annum: float, years_to_repay: int +) -> float: """ Formula for amortization amount per month: A = p * r * (1 + r)^n / ((1 + r)^n - 1) @@ -40,12 +42,12 @@ def equated_monthly_installments(p: float, r: float, y: int) -> float: 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 + rate_per_month = r_m = rate_per_annum / 12 # Years to repay is multiplied by 12 to get number of payments as payment is monthly - number_of_payments = n = y * 12 + number_of_payments = n = years_to_repay * 12 - return p * r_m * (1 + r_m)**n / ((1 + r_m)**n - 1) + return p * r_m * (1 + r_m) ** n / ((1 + r_m) ** n - 1) if __name__ == "__main__": From 458beb7b40794f7ed91de9b36e7699af0b7db515 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 03:23:27 +0530 Subject: [PATCH 4/8] Errors with name change --- financial/equated_monthly_installments.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/financial/equated_monthly_installments.py b/financial/equated_monthly_installments.py index 38654cc2b50b..79377ab8ca0a 100644 --- a/financial/equated_monthly_installments.py +++ b/financial/equated_monthly_installments.py @@ -34,20 +34,21 @@ def equated_monthly_installments( ... Exception: Years to repay must be an integer > 0 """ - if p <= 0: + if principal <= 0: raise Exception("Principal borrowed must be > 0") - if r < 0: + if rate_per_annum < 0: raise Exception("Rate of interest must be >= 0") - if y <= 0 or not isinstance(y, int): + if years_to_repay <= 0 or not isinstance(years_to_repay, 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 = rate_per_annum / 12 + rate_per_month = rate_per_annum / 12 # Years to repay is multiplied by 12 to get number of payments as payment is monthly - number_of_payments = n = years_to_repay * 12 + number_of_payments = years_to_repay * 12 - return p * r_m * (1 + r_m) ** n / ((1 + r_m) ** n - 1) + return principal * rate_per_annum * (1 + rate_per_annum) ** number_of_payments / +((1 + rate_per_month) ** number_of_payments - 1) if __name__ == "__main__": From 1cbf10d3ef934f5390545268ea42e9a97ed785e4 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 03:26:01 +0530 Subject: [PATCH 5/8] Formatting --- financial/equated_monthly_installments.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/financial/equated_monthly_installments.py b/financial/equated_monthly_installments.py index 79377ab8ca0a..020f4b54cd07 100644 --- a/financial/equated_monthly_installments.py +++ b/financial/equated_monthly_installments.py @@ -47,8 +47,8 @@ def equated_monthly_installments( # Years to repay is multiplied by 12 to get number of payments as payment is monthly number_of_payments = years_to_repay * 12 - return principal * rate_per_annum * (1 + rate_per_annum) ** number_of_payments / -((1 + rate_per_month) ** number_of_payments - 1) + return (principal * rate_per_annum * (1 + rate_per_annum) ** number_of_payments / +((1 + rate_per_month) ** number_of_payments - 1)) if __name__ == "__main__": From 556faf6921291d34d9b045bd09c5d4abbf9db07c Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 03:31:08 +0530 Subject: [PATCH 6/8] Formatting, Naming Error --- financial/equated_monthly_installments.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/financial/equated_monthly_installments.py b/financial/equated_monthly_installments.py index 020f4b54cd07..3cc3afe4814b 100644 --- a/financial/equated_monthly_installments.py +++ b/financial/equated_monthly_installments.py @@ -47,8 +47,12 @@ def equated_monthly_installments( # Years to repay is multiplied by 12 to get number of payments as payment is monthly number_of_payments = years_to_repay * 12 - return (principal * rate_per_annum * (1 + rate_per_annum) ** number_of_payments / -((1 + rate_per_month) ** number_of_payments - 1)) + return ( + principal + * rate_per_month + * (1 + rate_per_month) ** number_of_payments + / ((1 + rate_per_month) ** number_of_payments - 1) + ) if __name__ == "__main__": From dae80142bfc71ee6df3a7b24252399993bc895d5 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 03:32:00 +0530 Subject: [PATCH 7/8] dedent --- financial/equated_monthly_installments.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/financial/equated_monthly_installments.py b/financial/equated_monthly_installments.py index 3cc3afe4814b..3af9224930b5 100644 --- a/financial/equated_monthly_installments.py +++ b/financial/equated_monthly_installments.py @@ -47,7 +47,7 @@ def equated_monthly_installments( # Years to repay is multiplied by 12 to get number of payments as payment is monthly number_of_payments = years_to_repay * 12 - return ( + return ( principal * rate_per_month * (1 + rate_per_month) ** number_of_payments From b5da87415df944ee3e3157e52243d650ee8ba957 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 00:00:46 +0530 Subject: [PATCH 8/8] Update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 32ca8cd3b256..f515277f403e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -298,6 +298,7 @@ ## Financial * [Interest](https://github.com/TheAlgorithms/Python/blob/master/financial/interest.py) + * [EMI Calculation](https://github.com/TheAlgorithms/Python/blob/master/financial/equated_monthly_installments.py) ## Fractals * [Julia Sets](https://github.com/TheAlgorithms/Python/blob/master/fractals/julia_sets.py)