Gabriel Schneider
Gabriel Schneider

Reputation: 33

How to do amortization calculation

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

Upvotes: 3

Views: 158

Answers (1)

Chris Charley
Chris Charley

Reputation: 6613

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

Upvotes: 2

Related Questions