Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do amortization calculation

Tags:

python

I am trying to do an amortization calculation in python. I use the PyCharm IDE and it has been consistently getting the wrong answer. MIR and MT are the mortgage interest rate and mortgage term(years) and are inputs much higher in the script.

I'm not sure what the problem is, any help?

monthly_rate = MIR / 100
loan_term_months = MT * 12
balance = Mortgage_Balance

# Top of the equation
math1 = monthly_rate * ((1 + monthly_rate) ** MT)    # r(1+r)^n
# Bottom of the equation
math2 = ((1 + monthly_rate) ** MT) - 1   # (1+r)^n -1

# Total Equation
annual_payment = (Mortgage_Balance * math1) / math2    # P (r(1+r)^n /(1+r)^n -1)

final_monthly_payment = round((annual_payment / 12), 2)
final_annual_payment = round(annual_payment, 2)

Expected result(when MIR=4.375 and MT=30): 617.87

Given result(when MIR=4.375 and MT=30): 623.82

like image 987
Gabriel Schneider Avatar asked Jul 26 '26 15:07

Gabriel Schneider


1 Answers

The difference can be seen when getting monthly payments using scipy financial functions.

The given result of 623.82 is derived by dividing the annual payment by 12 to get a monthly payment. This is an error. You really want to calculate the monthly payment not the annual payment divided by 12.

Here are the results of the payment function from scipy.

>>> pmt(.04375, 30, 123750)
-7485.858293501044
>>> -7485.8582935010447/12
-623.8215244584204
>>> pmt(.04375/12, 360, 123750)
-617.8655064472862

You can see the monthly payment uses the interest rate divided by 12 (to get the monthly interest rate) and the periods = 360, (30 years times 12 months).

So, in your calculation,

monthly_rate = MIR / 100

"should be"

monthly_rate = MIR / 100/12

loan_term_months = MT * 12

Here you calculated the periods correctly, but didn't use them in your formula.

math1 = monthly_rate * ((1 + monthly_rate) ** loan_term_months)

math2 = ((1 + monthly_rate) ** loan_term_months) - 1
like image 171
Chris Charley Avatar answered Jul 29 '26 06:07

Chris Charley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!