Debug School

ashley
ashley

Posted on

Fixed-Rate Mortgage Amortization in Code: Principal vs Interest Breakdown and Edge Cases

Fixed-rate mortgage schedules (the French amortization system) maintain a constant monthly payment $P$. However, the internal distribution between interest payment $J_k$ and principal reduction $A_k$ shifts continuously over the term.

Formula for Fixed Monthly Payment

Given principal loan amount $PV$, monthly interest rate $i$, and total number of months $n$:

P = PV * (i * ((1 + i) ** n)) / (((1 + i) ** n) - 1)

For each month $k$:

  1. Interest: $J_k = S_{k-1} \cdot i$
  2. Principal Amortization: $A_k = P - J_k$
  3. Remaining Balance: $S_k = S_{k-1} - A_k$

Python Implementation

def generate_amortization_schedule(pv: float, annual_rate: float, years: int):
    n = years * 12
    i = annual_rate / 12.0
    factor = (1.0 + i) ** n
    payment = pv * (i * factor) / (factor - 1.0)

    balance = pv
    schedule = []

    for month in range(1, n + 1):
        interest = balance * i
        amortization = payment - interest
        balance -= amortization

        if month == n:
            amortization += balance
            balance = 0.0

        schedule.append({
            "month": month,
            "payment": round(payment, 2),
            "interest": round(interest, 2),
            "amortization": round(amortization, 2),
            "balance": round(max(0.0, balance), 2)
        })

    return schedule
Enter fullscreen mode Exit fullscreen mode

Verification and Schedule Inspection

To generate a complete month-by-month payment schedule and visualize principal vs interest decay, check the amortization calculator at Miryori.


Disclosure: The author leads content at Miryori, the free online calculator suite linked above. Educational arithmetic only, not financial advice.

Top comments (0)