Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..166

1
Chapter 11. Plotting and More About Classes 149 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 variable- rate 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. 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)

description

lll

Transcript of Introduction to Computation and Programming Using Python%2C Revised - Guttag%2C John v..166

  • Chapter 11. Plotting and More About Classes 149

    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 variable-rate 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.

    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)