-
Notifications
You must be signed in to change notification settings - Fork 0
/
MORTGAGE.py
119 lines (96 loc) · 4.22 KB
/
MORTGAGE.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from matplotlib import pyplot as plt
from matplotlib import pylab as plb
import numpy as np
def findpayment(loan, r, m):
return loan*((r*(1+r)**m)/((1+r)**m-1))
class mortgage():
def __init__(self,loan, annual_rate, months):
self.loan=loan
self.rate=annual_rate/12.0
self.months=months
self.paid=[0.0]
self.outstanding=[loan]
self.payment=findpayment(loan, self.rate, months)
self.legend=None
def makepayment(self):
self.paid.append(self.payment)
reduction=self.payment - self.outstanding[-1]*self.rate
self.outstanding.append(self.outstanding[-1] - reduction)
def gettotalpaid(self):
return sum(self.paid)
def __str__(self):
return self.legend
def plotpayments(self,style):
plb.plot(self.paid[1:],style,label=self.legend)
def plotbalance(self,style):
plb.plot(self.outstanding,style, label=self.legend)
def plottotpaid(self,style):
totpaid=[self.paid[0]]
for i in range(1, len(self.paid)):
totpaid.append(totpaid[-1] + self.paid[i])
plb.plot(totpaid,style, label=self.legend)
def plotnet(self,style):
totpaid=[self.paid[0]]
for i in range(1,len(self.paid)):
totpaid.append(totpaid[-1]+self.paid[i])
equityacquired=plb.array([self.loan] * len(self.outstanding))
equityacquired = equityacquired - plb.array(self.outstanding)
net=plb.array(totpaid)-equityacquired
plb.plot(net,style, label=self.legend)
class fixed(mortgage):
def __init__(self,loan, r, months):
mortgage.__init__(self,loan,r,months)
self.legend = 'Fixed, ' + str(round(r*100,2)) + '%'
class fixedwithpts(mortgage):
def __init__(self,loan, r, months, pts):
mortgage.__init__(self,loan, r, months)
self.pts=pts
self.paid=[loan*(pts/100.00)]
self.legend = 'Fixed, ' + str(round(r*100,2)) + '%, ' + str(pts) + ' points'
class TwoRate(mortgage):
def __init__(self,loan,r,months,teaserrate, teasermonths):
mortgage.__init__(self, loan, teaserrate, months)
self.teaserrate=teaserrate
self.teasermonths=teasermonths
self.nextrate=r/12.0
self.legend=str(teaserrate*100)+' % for '+str(self.teasermonths)\
+' months, then ' + str(round(r*100,2)) + '%'
def makepayment(self):
if len(self.paid) == self.teasermonths +1:
self.rate=self.nextrate
self.payment=findpayment(self.outstanding[-1],self.rate,self.months-self.teasermonths)
mortgage.makepayment(self)
def comparemortgages(amt,years,fixedrate,pts,ptsrate,varrate1,varrate2,varmonths):
totmonths=years*12
fixed1=fixed(amt,ptsrate,totmonths)
fixed2=fixedwithpts(amt,ptsrate,totmonths,pts)
tworate=TwoRate(amt,varrate2,totmonths,varrate1,varmonths)
morts=[fixed1,fixed2,tworate]
for m in range(totmonths):
for mort in morts:
mort.makepayment()
plotmortgages(morts,amt)
def plotmortgages(morts, amt):
def labelplot(figure, title, xlabel,ylabel):
plb.figure(figure)
plb.title(title)
plb.xlabel(xlabel)
plb.ylabel(ylabel)
plb.legend(loc='best')
styles = ['y-','r-.','g:']
payments, cost, balance, netcost = 0,1,2,3
for i in range(len(morts)):
plb.figure(payments)
morts[i].plotpayments(styles[i])
plb.figure(cost)
morts[i].plottotpaid(styles[i])
plb.figure(balance)
morts[i].plotbalance(styles[i])
plb.figure(netcost)
morts[i].plotnet(styles[i])
labelplot(payments,' Monthly payments of $ ' + str(amt) +' Mortgages ', 'Months', ' Monthly Payments')
labelplot(cost, 'Cash Outlay of $' + str(amt) +' Mortgages ', ' Months', 'Total Payments')
labelplot(balance, 'Balance Remaining of $' + str(amt) + ' Mortgages ', ' Months', 'Remaining Loan Balance of $')
labelplot(netcost, 'Net Cost of $' + str(amt) + ' Mortgages ', ' Months', 'Payments - Equity $')
comparemortgages(amt=200000,years=30, fixedrate=0.07,pts=3.25,ptsrate=0.05,varrate1=0.045,varrate2=0.095,varmonths=48)
plb.show()