PyLab and Mortgage Example

It has been a long time since I made a post.  I have a lot of things going on and really only have time to post when I am not too busy.

In this post, I am working with Guttag's mortgage example for PyLab.   This comes from chapter 11 of his book. The example compares 3 different mortgages. Here are the charts I created using Pylab:





The charts are saved in he  format.  To see the the example, please see Guttag's book on Python. 

The question is- as always- is Pylab easy to work with for making graphs?  This depends.  I know a lot of people who find it easier to make graphs in R or Tableau. They also like the quality of the graphs in R and Tableau.  In Python, making graphs is not as easy as R or Tableau but the formers are both functional programs, while Python is a general programming language.


Here is some of the code for the charts:


def findPayment(loan, r, m):
    """ Assumes: loan and r are floats, m an int
        Returns the monthly payment for a mortgage of size
        loan at a monthly rate of r for m months """
    return loan*((r*(1+r)**m)/((1 + r)**m - 1))

class Mortgage(object):
    """ Abstract class for building different kinds of mortgages """
    def __init__(self, loan, annRate, months):
        self.loan = loan
        self.rate = annRate/12.0
        self.months = months
        self.paid = [0.0]
        self.outstanding = [loan]
        self.payment = findPayment(loan, self.rate, months)
        self.legend = None #description of mortgage



def plotMortgages(morts, amt):
    def labelPlot(figure, title, xLabel, yLabel):
        pylab.figure(figure)
        pylab.title(title)
        pylab.xlabel(xLabel)
        pylab.ylabel(yLabel)
        pylab.legend(loc = 'best')

    styles = ['k-', 'k-.', 'k:']

    # give names to figure numbers
    payments, cost, balance, netCost = 0,1,2,3
    for i in range(len(morts)):
        pylab.figure(payments)
        morts[i].plotPayments(styles[i])
        pylab.figure(cost)
        morts[i].plotTotPd(styles[i])
        pylab.figure(balance)
        morts[i].plotBalance(styles[i])
        pylab.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 $')
    

def compareMortgages(amt, years, fixedRate, pts, ptsRate, varRate1, varRate2, varMonths):
        totMonths = years*12
        fixed1 = Fixed(amt, fixedRate, 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)
        pylab.show()


compareMortgages(amt=200000, years = 30, fixedRate = 0.07, pts = 3.25,
                 ptsRate = 0.05, varRate1 = 0.045, varRate2 = 0.095,
                 varMonths= 48)


Comments

Popular posts from this blog

Space Wars- Python 3

Maze Game in Python using Turtle