Sub topic 6
Calculating the monthly payment
This chapter assumes you know basic percentages, how compound interest works, and how to read short Python functions. You will see where the standard mortgage payment formula comes from, compute it by hand and in code, and learn which inputs move the result most.
What the payment has to do
A fixed-rate mortgage has one level payment, M, due every month. That single number must do two jobs:
- Cover the interest that accrued on the remaining balance during the month.
- Pay down enough principal that the balance reaches exactly zero after the last payment.
The first payment is mostly interest and the last is mostly principal, but the total stays the same. The formula finds the one M that makes the balance end at zero.
This chapter covers principal and interest only. Taxes, insurance, and mortgage insurance come later in the PITI chapter.
The inputs
| Symbol | Meaning | Example |
|---|---|---|
| `P` | Amount borrowed (principal) | 300,000 |
| `r` | Monthly interest rate, the annual rate divided by 12 | 0.06 / 12 = 0.005 |
| `n` | Number of monthly payments, the years times 12 | 30 × 12 = 360 |
| `M` | Level monthly payment | what we solve for |
Lenders quote a nominal annual rate and divide it by 12. They do not convert it to an effective annual rate. A 6% quoted rate therefore compounds at 0.5% per month, and the effective annual rate is slightly higher, about 6.17%.
Where the formula comes from
Let B_k be the balance after k payments. Each month the balance grows by interest, then the payment is subtracted:
B_k = B_(k-1) × (1 + r) − M
Starting from B_0 = P and unrolling this k times gives:
B_k = P(1 + r)^k − M × ((1 + r)^k − 1) / r
The loan is paid off when B_n = 0. Setting that equation to zero and solving for M gives:
M = P × r / (1 − (1 + r)^−n)
You can read this formula in two ways. The denominator 1 − (1 + r)^−n is the present value of one dollar paid each month for n months, per unit of r. So the formula says that the payment M is the amount whose stream of n payments has a present value equal to the loan. The same idea underlies car loans, student loans, and annuities.
The formula in code
def monthly_payment(principal, annual_rate, years):
n = years * 12 # number of monthly payments
r = annual_rate / 12 # monthly interest rate
if r == 0:
return principal / n
return principal * r / (1 - (1 + r) ** -n)
print(round(monthly_payment(300_000, 0.06, 30), 2)) # 1798.65The r == 0 branch matters. At zero interest the formula divides zero by zero, so you need the special case. With no interest the payment is just the principal split evenly: 300,000 / 360 = 833.33.
Checking it by hand
For the $300,000, 6%, 30-year loan:
r = 0.005andn = 360.(1.005)^360is about 6.0226, so(1.005)^−360is about 0.16604.- The denominator is
1 − 0.16604 = 0.83396. - The numerator is
300,000 × 0.005 = 1,500. M = 1,500 / 0.83396, which is about 1,798.65.
You can sanity-check the first payment too. Month-one interest is 300,000 × 0.005 = 1,500. That leaves 1,798.65 − 1,500 = 298.65 going to principal. Only about 17% of the first payment reduces the debt.
The total paid over the life of the loan is 1,798.65 × 360 = 647,514. That means about $347,500 of interest on a $300,000 loan, more than the amount borrowed.
How the inputs move the payment
Not every input has the same effect. Compare how the payment changes with rate and with term, holding the other inputs at $300,000 and 6% over 30 years.
def monthly_payment(principal, annual_rate, years):
n = years * 12
r = annual_rate / 12
if r == 0:
return principal / n
return principal * r / (1 - (1 + r) ** -n)
print("By rate (30 years):")
for rate in (0.05, 0.06, 0.07):
print(f" {rate:.0%}: {monthly_payment(300_000, rate, 30):,.2f}")
print("By term (6%):")
for years in (15, 20, 30):
payment = monthly_payment(300_000, 0.06, years)
total_interest = payment * years * 12 - 300_000
print(f" {years} years: payment {payment:,.2f}, interest {total_interest:,.2f}")The payments from this run are:
| Change | Monthly payment (P&I) |
|---|---|
| 5% rate, 30 years | 1,610.46 |
| 6% rate, 30 years | 1,798.65 |
| 7% rate, 30 years | 1,995.91 |
| 6% rate, 20 years | 2,149.29 |
| 6% rate, 15 years | 2,531.57 |
Two patterns stand out.
- Rate. A one-point change in the rate moves the payment by roughly $190 to $200 here. Each point costs about 11% more per month at this rate level.
- Term. Shortening the term raises the payment, but by less than the term shrinks. Going from 30 to 15 years halves the number of payments, but the payment rises only about 41%. Most of each early payment is interest anyway, so the extra money goes mostly to principal and the interest total drops sharply. Interest for the 15-year loan is about $155,600, compared with about $347,500 for the 30-year loan.
Edge cases and common errors
Most wrong answers come from a handful of mistakes.
- Using the annual rate directly. Plugging
0.06in asrwhilen = 360gives a nonsense answer. Both must be monthly. - Entering the rate as a percent.
6instead of0.06makes the rate 600%. Decide whether your function takes decimals or percents and validate it. - Mixing units for term. If your function takes years, convert to months inside it, as
monthly_paymentdoes. Do not make callers do it. - Forgetting the zero-rate case. Shown above. Zero-rate loans exist, for example some employer or family loans, and floating-point rates very close to zero can also lose precision.
- Treating the result as exact. Real lenders round the payment to the cent. Rounding means the last payment differs by a few cents. The amortization chapter handles this by capping the final payment at the remaining balance plus interest.
- Assuming the number is the full housing payment. It covers only principal and interest. Property taxes, homeowners insurance, and any mortgage insurance are added on top.
Rounding in practice
Floating-point arithmetic is fine for estimates. For software that handles real money, round the payment to cents once, and then carry the balance in integer cents or in decimal.Decimal. Rounding at every step in floating point can cause the balance to drift by a few cents over 360 months.
Why the formula matters
Knowing the formula lets you answer questions that calculators hide:
- How much more can I borrow if the rate drops by one point and I keep the same budget? Solve for
Pinstead:P = M × (1 − (1 + r)^−n) / r. - Which matters more for my budget, a lower rate or a longer term?
- How sensitive is my payment to a rate change before I lock?
Exercise
Using M = P·r / (1 − (1 + r)^−n), find the payment for a $200,000, 30-year loan at 0% interest. Then find the maximum loan a buyer can afford at 6% for 30 years if they can pay $1,500 per month toward principal and interest. Use that the 6%, 30-year payment is $5.996 per $1,000 borrowed.
Show answer
At 0% the formula's r = 0 case applies: 200,000 / 360 = $555.56 per month. For the second part, scale from the per-$1,000 payment: 1,500 / 5.996 × 1,000 ≈ $250,200, so the buyer can borrow about $250,000. This follows because the payment is proportional to the loan amount.
Exercise
A loan officer offers two options on a $300,000 loan: 6% for 30 years (payment $1,798.65) or 6% for 15 years (payment $2,531.57). Without computing a full schedule, why does the 15-year loan cost far less in total interest even though the monthly payment is only about 41% higher?
Show answer
Interest is charged each month on the remaining balance. The larger 15-year payment sends more money to principal early on, so the balance falls faster and every later month's interest is smaller. The loan also lasts half as long, so interest accrues over 180 months instead of 360. Total interest is about $155,600 for 15 years versus about $347,500 for 30 years.