Python: a smarter way to calculate loan payments

How to calculate the monthly loan fee?

Given:

  • a: loan amount.
  • b: lending period (number of months).
  • c: interest rate pa (interest is calculated and added every month, 1/12 percent is added. So if interest is 12%, 1% is added every month).
  • d: amount of money due after the end of the period.

This problem is slightly different from the usual one, because the goal is not to repay the loan after the lonely period has ended, but should still provide the amount. I could find an algorithm to solve this problem if I wanted to pay the full amount, but this will be the reason that this work will not work when the goal is to end because of a certain amount, and not because anything.

I managed to solve this problem, starting with an assumption, and continue to improve this conjecture until it gets close enough. I thought, however, if there is a better way to just figure it out, and not just guess.

Edit: this is how I do it now.

def find_payment(start, end, months, interest): difference = start guess = int(start / months * interest) while True: total = start for month in range(1, months + 1): ascribe = total * interest / 12 total = total + ascribe - guess difference = total - end # See if the guess was good enough. if abs(difference) > start * 0.001: if difference < 0: if abs(difference) < guess: print "payment is %s" % guess return evolution(start, guess, interest, months) else: mod = int(abs(difference) / start * guess) if mod == 0: mod = 1 guess -= mod else: mod = int(difference / start * guess) if mod == 0: mod = 1 guess += mod else: print "payment is %s" % guess return evolution(start, guess, interest, months) 

evolution is just a function that shows how a loan will look like payment for payment and interest for interest, summing up the total amount of interest paid, etc.

For example, if I wanted to know the monthly payments for a loan starting at $ 100 thousand and ending at $ 50 thousand with a percentage of 8% and a duration of 70 months, calling

 >>> find_payment(100000, 50000, 70, 0.08) payment is 1363 

In the above case, I would finish for 49935, and I went through the loop 5 times. The number of times it takes to go through a cycle depends on how close I'm used to getting to the amount, and it changes a bit.

+6
python math algorithm finance
source share
7 answers

This is mainly a calculation of the repayment of the mortgage .

Assuming the beginning is greater than the end, and this interest is between 0 and 1 (i.e. 0.1 for 10% interest)

First, consider the part of the payment that you want to pay off.

 Principal = start - end 

Monthly payment is provided:

 pay_a = (interest / 12) / (1 - (1+interest/12) ^ (-months))) * Principal 

Then you need to consider extra interest. Coefficient equal to other main times, monthly percentage

 pay_b = interest / 12 * end 

Thus, the total payment

 payment = (interest / 12) * (1 / (1 - (1+interest/12) ^ (-months))) * Principal + end) 

In the example you specified from

 Start: 100000 End: 50000 Months: 70 Interest: 8% pay_a = 896.20 pay_b = 333.33 Payment = 1229.54 

When I tested these values ​​in Excel, after 70 payments, the refinancing loan was 50,000. This assumes that you pay interest on a conditional value before the payment is made every month.

+9
source share

Perhaps the easiest way to think about this is to split the loan into two parts, one part that must be fully repaid, and the other part where you will not pay back anything. You have already calculated the monthly fee for the first part.

+4
source share

You can continue to pay interest for each month; then, you will always be obliged by the same type.

 Owe_1 = a Int_2 = Owe_1*(InterestRate/12) Pay_2 = Int_2 Owe_2 = Owe_1 + Int_2 - Pay_2 # ==> Owe_1 + Int_2 - Int_2 = Owe_1 Int_3 = Owe_2*(InterestRate/12) Pay_3 = Int_3 Owe_3 = Owe_2 + Int_3 - Pay_3 # ==> Owe_2 + Int_3 - Int_3 = Owe_2 = Owe_1 
0
source share

python code to calculate emi

 class EMI_CALCULATOR(object): # Data attributes # Helps to calculate EMI Loan_amount = None # assigning none values Month_Payment = None # assigning none values Interest_rate = None #assigning none values Payment_period = None #assigning none values def get_loan_amount(self): #get the value of loan amount self.Loan_amount = input("Enter The Loan amount(in rupees) :") pass def get_interest_rate(self): # get the value of interest rate self.Interest_rate = input("Enter The Interest rate(in percentage(%)) : ") pass def get_payment_period(self): # get the payment period" self.Payment_period = input("Enter The Payment period (in month): ") pass def calc_interest_rate(self): # To calculate the interest rate" self.get_interest_rate() if self.Interest_rate > 1: self.Interest_rate = (self.Interest_rate /100.0) else: print "You have not entered The interest rate correctly ,please try again " pass def calc_emi(self): # To calculate the EMI" try: self.get_loan_amount() #input loan amount self.get_payment_period() #input payment period self.calc_interest_rate() #input interest rate and calculate the interest rate except NameError: print "You have not entered Loan amount (OR) payment period (OR) interest rate correctly,Please enter and try again. " try: self.Month_Payment = (self.Loan_amount*pow((self.Interest_rate/12)+1, (self.Payment_period))*self.Interest_rate/12)/(pow(self.Interest_rate/12+1, (self.Payment_period)) - 1) except ZeroDivisionError: print "ERROR!! ZERO DIVISION ERROR , Please enter The Interest rate correctly and Try again." else: print "Monthly Payment is : %r"%self.Month_Payment pass if __name__ == '__main__':# main method Init = EMI_CALCULATOR() # creating instances Init.calc_emi() #to calculate EMI 

for more information: https://emilgeorgejames.wordpress.com/2015/07/29/python-emi-equated-monthly-installment-calculator/

0
source share

This is a fairly detailed way, but will also give the whole payment.

 # Mortgage Loan that gives the balance and total payment per year # Function that gives the monthly payment def f1 (principle,annual_interest_rate,duration): r = annual_interest_rate/1200 n = duration*12 a=principle*r*((1+r)**n) b= (((1+r)**n)- 1) if r > 0 : MonthlyPayment = (a/b) else : MonthlyPayment = principle/n return MonthlyPayment # Function that gives the balance def f2 (principle,annual_interest_rate,duration,number_of_payments): r = annual_interest_rate/1200 n = duration*12 a= ((1+r)**n) b= ((1+r)**number_of_payments) c= (((1+r)**n)-1) if r > 0 : RemainingLoanBalance = principle*((ab)/c) else : RemainingLoanBalance = principle*(1-(number_of_payments/n)) return RemainingLoanBalance # Entering the required values principle=float(input("Enter loan amount: ")) annual_interest_rate=float(input("Enter annual interest rate (percent): ")) duration=int(input("Enter loan duration in years: ")) # Output that returns all useful data needed print ("LOAN AMOUNT:",principle,"INTEREST RATE (PERCENT):",annual_interest_rate) print ("DURATION (YEARS):",duration,"MONTHLY PAYMENT:",int(f1(principle,annual_interest_rate,duration))) k=duration+1 BALANCE=principle total=0 for i in range (1,k): TOTALPAYMENT= f1(BALANCE,annual_interest_rate,ki)*12 total+= TOTALPAYMENT BALANCE= f2(principle,annual_interest_rate,duration,12*i) print("YEAR:",i,"BALANCE:",int(BALANCE),"TOTAL PAYMENT",int(total)) 
0
source share

How about this?

 def EMI_calc(principle, rate, time, frequency): return (principle / ((1-((1+(rate/frequency))**(-1*(time*frequency))))/(rate/frequency))) print(""" ----- Welcome to EMI programe for Python ----- """) print("\n You have chosen to know the EMI for Loan.\n") input('\nTo Continue Press ENTER --- to ABORT Press ctrl+c > \n') print("\nPlease Enter amount of Loan to be taken: >\n") principle = int(input()) print("\nEnter rate of interst (%): >\n") rate = float(input())/100 print("\nEnter Term (Years): >\n") time = float(input()) print("\nPlease enter the frequency of installments) : >\n") frequency = int(input()) EMI = round(EMI_calc(principle, rate, time, frequency),0) print(""" --------------------------------------------------------------------- """) print(f""" The EMI for Loan of Rs.{principle}; at interest rate of {rate*100} % for {time} years; would be: Rs.""", EMI) print(""" --------------------------------------------------------------------- """) 
0
source share

Here is a code snippet using numpy functions. This shows you the payment, principal, interest, installment plan and total_amount every month. Run it and see the output. You can also check the syntax for the Excel functions "IPMT ()" and "PPMT ()" for a more detailed explanation of the arguments. https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.pmt.html#numpy.pmt

 import math import numpy as np rate = 0.08 start_amount = 100000.0 end_amount = 50000.0 diff_amount = start_amount - end_amount # nr_years = 4 payment_frequency = int (12) nr_months = 70 # = nr_years * payment_frequency per_np = np.arange (nr_months) + 1 # +1 because index starts with 1 here pay_b = rate / payment_frequency * end_amount ipmt_np = np.ipmt (rate / payment_frequency, per_np, nr_months, diff_amount) - pay_b ppmt_np = np.ppmt (rate / payment_frequency, per_np, nr_months, diff_amount) for payment in per_np: idx = payment - 1 principal = math.fabs (ppmt_np [idx]) start_amount = start_amount - principal interest = math.fabs (ipmt_np [idx]) instalment = principal + interest print payment, "\t", principal, "\t", interest, "\t\t", instalment, "\t\t", start_amount print np.sum (ipmt_np) 
0
source share

All Articles