Skip to content

Instantly share code, notes, and snippets.

@datlife
Created May 20, 2021 22:52
Show Gist options
  • Save datlife/e1dc5069a80c125b485a704682dfcaf7 to your computer and use it in GitHub Desktop.
Save datlife/e1dc5069a80c125b485a704682dfcaf7 to your computer and use it in GitHub Desktop.
Mortgage Calculator
"""
Mortgage Calculator for fun
https://en.wikipedia.org/wiki/Mortgage_calculator
What I want to know:
---- What's interest that I have to pay at payment N ?
---- What's principal that I have to pay at payment N ?
---- What's loan balance before payment N ?
(P * (1 + r) ** n) - (c * (((1+r)**n - 1)) / r)
---- What's principal that I have paid so far before payment N ?
---- What's interest that I have paid so far before payment N ?
"""
# VARIABLES
HOME_PRICE = 200000
DOWN_PERCENTAGE = 0.0
INTERST_RATE = 0.03
LOAN_TERM_YEARS = 30
PAYMENT_PER_YEAR = 12
PROPERTY_TAX = 0.0130 # California
INSURANCE_RATE = 0.0022 # California
def cost_of_debt(P, r, N):
""" Calculate monthly cost to service debt P at a fixed rate r and period N
Paramters
=========
P: Loan's Principal, the amount of borrowed money
r: Annual Interest Rate
N: Number of payments over the loan terms
e.g 30year * 12 times/ year = 360 payments
Returns:
========
Monthly payment to service the loan
"""
if r == 0:
return P / N
r = r / 12 # monthly rate
return (r * P) / (1 - (1 + r)**(-N))
def cost_breakdown(P, r, n, c):
"""Understand how each components in the monthly payment
Parameters
==========
P: Loan principal
n: payment number
r: monthly interest
c: monthly payment (derived from cost of debt)
Returns:
========
principal, interest paid of Nth payment.
"""
# Amount owed at end of month N
beginning_balance = (P * (1 + r) ** n) - (c * (((1+r)**n - 1)) / r)
interest = beginning_balance * r
principal = c - interest
return beginning_balance, principal, interest
MONTHLY_PAYMENT = cost_of_debt(
P=HOME_PRICE*(1 - DOWN_PERCENTAGE),
r=INTERST_RATE,
N=LOAN_TERM_YEARS * PAYMENT_PER_YEAR)
print("Monthly Payment: ${:,.2f}\n".format(MONTHLY_PAYMENT))
COLUMNS = ["YEAR", "MONTH", "BALANCE", "PAYMENT", "PRINCIPAL", "INTEREST"]
print("_______________________________________________________________")
print("{:4} | {:4} | {:10} | {:9} | {:9} | {:9}|".format(*COLUMNS))
print("===============================================================")
for year_n in range(2):
cinter, cprinc = 0, 0
for month_n in range(PAYMENT_PER_YEAR):
balance, principal, interest = cost_breakdown(
P=HOME_PRICE * (1-DOWN_PERCENTAGE),
n=month_n * (1 + year_n),
r=INTERST_RATE/12,
c=MONTHLY_PAYMENT)
print("{: <4} | {: <4} | {:10,.2f} | {: <9,.2f} | {: <9,.2f} | {: <9,.2f}|".format(
year_n, month_n, balance, MONTHLY_PAYMENT ,principal, interest, 0
))
cinter += interest
cprinc += principal
print("===============================================================")
r = INTERST_RATE / 12
n = (1 + year_n) * PAYMENT_PER_YEAR
cumm_interest = (MONTHLY_PAYMENT * (((1+ r)**n - 1)) / r)
print("Total payment = {:,.2f}".format(MONTHLY_PAYMENT * PAYMENT_PER_YEAR))
print("Interest = {:,.2f}".format(cinter))
print("Principal = {:,.2f}\n".format(cprinc))
# print("\nInterest = {:,.2f}\n".format(cumm_interest))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment