Will cause a window to appear on your computer monitor ...

Draft. Please do not distribute.

?John Guttag, 2010

The following is an excerpt from book I am writing for 6.00. The book (alas) is well behind schedule. I can't promise that there are no errors in this material. However, I thought that it would be helpful to you.

The first section covers some aspects of using Pylab to generate plots. The second section contains an extended example intended to illustrate some aspects of using classes that you haven't seen. It also contains more examples of ways to construct plots. Don't worry about understanding the details about mortgages and discounted cash flow (unless you are about to buy a house).

I would be delighted to receive suggestions about how to improve the presentation of this material, especially any errors that you might find.

-- John Guttag

PL.2. Plotting Using Pylab

Pylab is a Python library that provides many of the facilities of MATLAB, "a high-level technical computing language and interactive environment for algorithm development, data visualization, data analysis, and numeric computation."1 Later in the book, we will look at some of the more advanced features of pylab, but in this chapter we focus on some of its facilities for plotting data. The web site contains a complete user's guide for pylab. There are also a number of web sites with excellent tutorials. We will not try to provide a user's guide or a complete tutorial here. Instead, we will merely provide a few example plots and explain the code that generated them. For more details about plotting see .

Let's start with something very simple. Executing the code

import pylab

pylab.plot([1,2,3,4], [1,2,3,4]) pylab.plot([1,4,2,3], [5,6,7,8]) pylab.show()

will cause a window to appear on your computer monitor. Its exact appearance will depend on the operating system on your machine, but it will always look something like:

1

Draft. Please do not distribute.

?John Guttag, 2010

The bar at the top contains the name of the window, in this case "Figure 1." In this example, the name is generated automatically by pylab.

The middle section of the window contains the plot generated by the two lines of code following the import statement. The bottom line was generated by the statement pylab.plot([1,2,3,4], [1,2,3,4]). Notice that the two arguments are lists of the same length. Together, they provide a sequence of four coordinate pairs, [(1,1), (2,2), (3,3), 4,4)]. These are plotted in order, and then connected by a line. (Pylab automatically chooses the line color for each plot command, but this can be overridden by an optional argument, as we shall see.) The zig zag plot at the top of the middle section is produced the same way. It zig zags because the sequence of Cartesian points being connected is [(1,5), (4,6), (2,7), (3,8)].

The final line of code, pylab.show() causes the window to appear on the screen. If that line were not there, the figure would still have been produced, but it would not have been displayed. This is not as silly as it at first sounds, since one might well choose to write the figure to a file rather than display it.2

The bar at the bottom of the window contains a number of push buttons. The rightmost button is used to write the plot to a file.3 The next button over is used to adjust the appearance of the plot in the window. This is useful when there is more than one plot per figure. The next four buttons

2 In some operating systems, pylab.show() causes the process running Python to be suspended. This is unfortunate. The usual work around is to put the command as the last line to be executed. 3 For those of you too young to know, the icon represents a "floppy disk." Floppy disks were first introduced by IBM in 1971. They were eight inches in diameter and held all of 80K bytes. Unlike later floppy disks, they were actually floppy. The original IBM PC had a single 160KB 5.5 inch floppy disk drive. For most of the 1970's and 1980's, floppy disks were the primary storage device for personal computers. The transition to rigid cases (as represented in the icon that launched this digression) started in the mid-1980's (with the Macintosh), which didn't stop people from calling them floppy disks.

Draft. Please do not distribute.

?John Guttag, 2010

are used for panning and zooming. And the button on the left is used to restore the figure to its original appearance after you are done playing with pan and zoom.

Of course, it is possible to produce more than one figure and to write them to files rather than the display. The following code

pylab.figure(1) pylab.plot([1,2,3,4], [1,2,3,4]) pylab.figure(2) pylab.plot([1,4,2,3], [5,6,7,8]) pylab.savefig('firstSaved') pylab.figure(1) pylab.plot([5,6,7,10]) pylab.savefig('secondSaved')

produces and saves to files named firstSaved.png and secondSaved.png the two plots:

Pylab has a notion of "current figure." Executing the statement pylab.figure(x) sets the current figure to the figure numbered x. Subsequently executed pylab commands implicitly refer to that figure, until another pylab.figure command is executed. This explains why the plot on the left, which corresponds to the figured numbered 2, is stored in firstSaved.png.

Observe that the last call to pylab.plot is passed only one argument. This argument supplies the Y values. The corresponding X values are range(len(Y-values)), which is why they range from 0 to 3 in this case.

Let's look at another example. The code

principal = 10000 #initial investment interestRate = 0.05 years = 20 values = [] for i in range(years + 1):

values.append(principal) principal += principal*interestRate pylab.plot(values)

produces the plot

Draft. Please do not distribute.

?John Guttag, 2010

If we look at the code, we can deduce that this is a plot showing the growth of an initial investment of $10,000 with at an annually compounded interest rate of 5%. However, this cannot be easily inferred looking only at the plot itself. That's a bad thing. All plots should have informative titles, and all axes should be labeled. If we add to the end of our the code the following lines pylab.title('5% Growth, Compounded Annually') pylab.xlabel('Years of Compounding') pylab.ylabel('Value of Principal ($)') we get the more informative plot

For every plotted curve, there is an optional argument that is a format string indicating the color and line type of the plot. The letters and symbols of the format string are from MATLAB, and are composed of a color indicator followed by line style indicator. The default format string is 'b-', which, as we have seen is a solid blue line. To plot the above with red circles, you would replace the call to pylab.plot by pylab.plot(values, 'ro'), which produces the plot:

Draft. Please do not distribute.

?John Guttag, 2010

See for a complete list of line styles and format strings

PL.2 Mortgages

In the fall of 2008 in the United States, a collapse in housing prices helped trigger a severe economic melt down. One of the contributing factors was that many home owners had taken on mortgages that ended up having unexpected consequences.4

In the beginning, mortgages were relatively simple beasts. One borrowed money from a bank and make a fixed size payment each month for the life of the mortgage, which typically ranged from fifteen to thirty years. At the end of that period, the bank had been paid back the initial loan plus interest.

Towards the end of the twentieth century, mortgages started getting a lot more complicated. People could get lower interest rates by paying "points" at the time they took on the mortgage. A point is a cash payment of 1% of the value of the loan. People could take mortgages that were "interest only" for a period of time. That is to say for some number of months at the start of the loan the borrower paid only the accrued interest and none of principal. Other loans involved multiple rates. Typically the initial rate (called a "teaser rate") was low, and then it went up over time. Many of these loans were variable rate, i.e., the rate to be paid after the initial period would vary depending upon interest rates.

In principal, giving consumers a variety of options is a good thing. However, unscrupulous loan purveyors were not always careful to fully explain the possible long-term implications of the various options, and some na?ve borrowers made choices that proved to have dire consequences. There was no need for this, since it is relatively easy to write a program (and many such programs exist) that plots the implications of various kinds of loans.

Let's build a program that plots the costs of four kinds of loans:

? A fixed rate mortgage with no points,

4 In this context, it is worth recalling that etymology of the word mortgage. The American Heritage Dictionary of the English Language traces the word back to the old French words for dead (mort) and pledge (gage). (This derivation also explains why the "t" in the middle of mortgage is silent.)

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download