Skip to content

Instantly share code, notes, and snippets.

@plasmon360
Created September 23, 2015 23:43
Show Gist options
  • Save plasmon360/dcdf1deb4ce3588d3beb to your computer and use it in GitHub Desktop.
Save plasmon360/dcdf1deb4ce3588d3beb to your computer and use it in GitHub Desktop.
Calculates payments and amortization table.
home_price = 300000
down_payment = 20 # in percent
principal = home_price * (1 - down_payment / 100.0)
yearly_interest_rate = 3.710 # in percent
number_of_years = 30
pay_periods_per_year = 12
def remaining_at_end(monthly_payment, principal, number_of_years, pay_periods_per_year, yearly_interest_rate):
remaining = principal
for i in xrange(int(number_of_years * pay_periods_per_year)):
interest = remaining * yearly_interest_rate / pay_periods_per_year / 100.00
remaining -= (monthly_payment - interest)
return remaining
def amortization_table(monthly_payment, principal, number_of_years, pay_periods_per_year, yearly_interest_rate):
remaining = principal
print("Period Year Monthly payment Interest part: Payment_towards_principal part Remaining")
for i in xrange(int(number_of_years * pay_periods_per_year)):
interest = remaining * yearly_interest_rate / pay_periods_per_year / 100.00
remaining -= (monthly_payment - interest)
print("%d %d %.2f %.2f %.2f %.2f" % (
i + 1, int(i / pay_periods_per_year) + 1, monthly_payment, interest, monthly_payment - interest,
remaining))
if remaining <=0 :
number_of_periods = i+1
total_mortgage_value = (i+1)*monthly_payment+remaining # number of periods for balance to go negative and any negative current balance
print ("Number of periods to pay back: %d" %(number_of_periods))
print ("Total Mortage value: %.2f" %(total_mortgage_value))
break
return (number_of_periods, total_mortgage_value)
# Calculates the monthly payment by iteration, assumes a montly payement to be atleast the interest owed for the first month
monthly_payment = principal * yearly_interest_rate / pay_periods_per_year / 100.00 # The first monthly payment should be atleast the interest on the loan amount for that period
while True:
monthly_payment += 0.1 # increase the monthly payment by a small amount. For better results keep this as small as possible
if remaining_at_end(monthly_payment, principal, number_of_years, pay_periods_per_year,
yearly_interest_rate) <= 0.0:
# if the current balance at the end is zero or negative,
print("Monthly payment %.2f" % (monthly_payment))
break
# with a given monthly payment and other paramenters, creates a amortization table
n, mv = amortization_table(monthly_payment, principal, number_of_years, pay_periods_per_year, yearly_interest_rate)
# Lets say I put an extra payement on top of minimum monthly payment
optional_extra_payment_per_period = 200
monthly_payment+= optional_extra_payment_per_period
print ('Optional extra payment, amortization table')
n_extra_payment, mv_extra_payment = amortization_table(monthly_payment, principal, number_of_years, pay_periods_per_year, yearly_interest_rate)
# total mortgage value decreases and number of pay period decreases
print("Number of pay periods saved by paying extra %.2f per period is %.2f" %(optional_extra_payment_per_period, n-n_extra_payment))
print("Amount saved by paying extra %.2f per period is %.2f" %(optional_extra_payment_per_period, mv-mv_extra_payment))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment