Skip to content

Instantly share code, notes, and snippets.

@dylanjm
Created December 2, 2020 02:54
Show Gist options
  • Save dylanjm/df46857941e9738914c19abf609d7ce1 to your computer and use it in GitHub Desktop.
Save dylanjm/df46857941e9738914c19abf609d7ce1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def amort(principal, rate, n):
return principal * (rate * ((1 + rate) ** n)) / ((1 + rate) ** n - 1)
def compute_schedule(bal, pmt, rate):
while bal > 0:
new_bal = bal - (pmt - (bal * rate))
period_interest = (bal * rate)
period_principal = pmt - (bal * rate)
if new_bal > 0:
yield (new_bal, period_interest, period_principal)
bal = new_bal
def amort_table(pmt, rate):
asking = 349900.00
negotiated = 0.03
closing = negotiated * asking
down_pmt = 12200.00
purchase = asking + closing
loan_amt = purchase - down_pmt
rate = 0.025 / 12
n = 30 * 12
df = pd.DataFrame(
columns=['Payment', 'Amount', 'Interest', 'Principal', 'Balance'],
index = np.arange(1, 361, 1)
)
for x, (b, i, p) in enumerate(compute_schedule(loan_amt, amort(loan_amt, rate, n), rate)):
df.loc[x + 1] = pd.Series(
and{
'Payment': x+1,
'Amount': round(amort(loan_amt, rate, n), 2),
'Interest': round(i, 2),
'Principal': round(p, 2),
'Balance': round(b, 2)
}
)
print(df)
def main():
asking_price = 349_900.00
closing_costs_percent = 0.03
closing_costs = closing_costs_percent * asking_price
down_pmt = 12_200.00
purchase = asking_price + closing_costs
loan_amt = purchase - down_pmt
rate = 0.025 / 12
n = 30 * 12
amort_table(amort(loan_amt, rate, n), rate)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment