0% found this document useful (0 votes)
98 views

Introduction To Computation and Programming Using Python, Revised - Guttag, John v..166

This document describes three subclasses of the Mortgage class - Fixed, FixedWithPts, and TwoRate. Fixed adds a legend attribute to identify the interest rate. FixedWithPts adds a points attribute and calculates an upfront payment. TwoRate has a teaser interest rate for a set number of months, then switches to a higher regular rate, updating the payment amount accordingly. The document also describes functions for plotting and comparing different types of mortgages by simulating payments over time.

Uploaded by

ZhichaoWang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Introduction To Computation and Programming Using Python, Revised - Guttag, John v..166

This document describes three subclasses of the Mortgage class - Fixed, FixedWithPts, and TwoRate. Fixed adds a legend attribute to identify the interest rate. FixedWithPts adds a points attribute and calculates an upfront payment. TwoRate has a teaser interest rate for a set number of months, then switches to a higher regular rate, updating the payment amount accordingly. The document also describes functions for plotting and comparing different types of mortgages by simulating payments over time.

Uploaded by

ZhichaoWang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Chapter 11.

Plotting and More About Classes

class Fixed(Mortgage):
def __init__(self, loan, r, months):
Mortgage.__init__(self, loan, r, months)
self.legend = 'Fixed, ' + str(r*100) + '%'
class FixedWithPts(Mortgage):
def __init__(self, loan, r, months, pts):
Mortgage.__init__(self, loan, r, months)
self.pts = pts
self.paid = [loan*(pts/100.0)]
self.legend = 'Fixed, ' + str(r*100) + '%, '\
+ str(pts) + ' points'
class TwoRate(Mortgage):
def __init__(self, loan, r, months, teaserRate, teaserMonths):
Mortgage.__init__(self, loan, teaserRate, months)
self.teaserMonths = teaserMonths
self.teaserRate = teaserRate
self.nextRate = r/12.0
self.legend = str(teaserRate*100)\
+ '% for ' + str(self.teaserMonths)\
+ ' months, then ' + str(r*100) + '%'
def makePayment(self):
if len(self.paid) == self.teaserMonths + 1:
self.rate = self.nextRate
self.payment = findPayment(self.owed[-1], self.rate,
self.months - self.teaserMonths)
Mortgage.makePayment(self)

Figure 11.2 Subclasses of Mortgage


Figure 11.3 contain functions that can be used to generate plots intended to
provide insight about the different kinds of mortgages.
The function plotMortgages generates appropriate titles and axis labels for each
plot, and then uses the methods in MortgagePlots to produce the actual plots. It
uses calls to pylab.figure to ensure that the appropriate plots appear in a given
figure. It uses the index i to select elements from the lists morts and styles in a
way that ensures that different kinds of mortgages are represented in a consistent
way across figures. For example, since the third element in morts is a variablerate mortgage and the third element in styles is 'b:', the variable-rate mortgage
is always plotted using a blue dotted line.
The function compareMortgages generates a list of different mortgages, and
simulates making a series of payments on each, as it did in Chapter 8. It then
calls plotMortgages to produce the plots.

149

You might also like