Due: 9:00am, Monday, April 26. Value: 30 pts. Submit to Sauron.
At right is a graph of home prices between 1975 and the end of 2009, based on data from the Federal Housing Finance Agency that you can download from this file. You can clearly see the recent housing bust. But given the inexorable rise upward before that, you might wonder: Why was anybody talking about an unusual rise in prices in the years preceding the bust?
The answer is that the graph is changed dramatically once you correct for inflation. Your assignment is to modify a program drawing this graph so that it draws data based on inflation-corrected values. As a measure of inflation, you can use the Consumer Price Index as published by the Bureau of Labor Statistics and downloadable from this file.
Below is the starting code, which draws the graph
above. I suggest modifying the compute_data
function by first loading the
inflation data into
a dictionary, where each year is a key mapping to the price
index for that year.
Then, you would modify the existing code that loads the
house prices
so that rather than placing the raw house price into
the list ys, you first
divide the house price by the price index for that year, as found in the
dictionary.
from graphics import *
# creates the data to be displayed, returning it as a list
def compute_data():
ys = []
data_file = open('house-prices.txt')
for line in data_file:
tokens = line.split()
house_price = float(tokens[1])
ys.append(house_price)
data_file.close()
return ys
#
# You should not modify code below this point
#
# create a window containing a graph of the data
def draw_graph(ys):
win = GraphWin('House Price Index')
xaxis = Line(Point(20, 180), Point(180, 180))
yaxis = Line(Point(20, 180), Point(20, 20))
xaxis.draw(win)
yaxis.draw(win)
ymax = max(ys)
for i in range(len(ys)):
# compute x- and y-coordinates, between 0 and 1
x = float(i) / (len(ys) - 1)
y = float(ymax - ys[i]) / ymax
# scale them for window coordinates, so they are between 20 and 180
p1 = Point(20 + 160 * x, 20 + 160 * y)
if i != 0:
seg = Line(p0, p1)
seg.setOutline('blue')
seg.draw(win)
p0 = p1
draw_graph(compute_data())