Skip to content

Instantly share code, notes, and snippets.

@urish
Created May 25, 2013 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urish/5647324 to your computer and use it in GitHub Desktop.
Save urish/5647324 to your computer and use it in GitHub Desktop.
Mortgage calculation related functions
def spitzer(k,r,p):
return k-p*(r/12.0)
def months(k,r,p):
total = 0
while k > 0:
k -= spitzer(k,r,p)
total += 1
return total
def binomial(a, n):
if n < 0:
return 1.0 / binomial(a, -n)
sum = 1.
pow = 1. * n
term = 1.
cof = 1.
for i in range(1, 10):
cof *= pow / i
pow -= 1.
term *= a
sum += cof * term
return sum
def monthlyRefund(interest, months, sum):
interest /= 100.
return sum * interest / 12. / (1.0 - binomial(interest / 12., -months))
def waste(interest, months, sum):
return monthlyRefund(interest, months, sum)*months-sum
def gracewaste(interest, months, grace, sum):
return waste(interest,months-grace,sum)+sum*(interest/100./12.)*grace
def split(interest, low_interest, months, grace, sum, low_sum):
splitWaste = waste(low_interest,grace,low_sum)+gracewaste(interest,months,grace,sum)
return monthlyRefund(low_interest,grace,low_sum)+sum*interest/100./12.,monthlyRefund(interest,months-grace,sum),splitWaste,waste(interest,months,sum+low_sum)-splitWaste
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment