Skip to content

Instantly share code, notes, and snippets.

@stucchio
Created March 31, 2011 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stucchio/896909 to your computer and use it in GitHub Desktop.
Save stucchio/896909 to your computer and use it in GitHub Desktop.
Employment vs investment
from pylab import *
from numpy import *
import matplotlib.cbook as cbook
from datetime import datetime
from scipy.interpolate import interp1d
def read_file(filename):
dates = []
values = []
for l in open(filename).readlines():
datestr, valstr = l.strip().split()
date = datetime.strptime(datestr, "%Y-%m-%d")
dates.append(date)
values.append(float(valstr))
dates = date2num(dates)
interpolated = interp1d(dates, array(values))
d = date2num([ datetime(y,m,1) for y in range(1948,2009) for m in [1,4,7,10]])
return (d, interpolated(d))
def year_as_num(year):
return date2num(datetime(year,1,1))
t, employment = read_file("/tmp/EMRATIO.tsv")
t, investment = read_file("/tmp/GPDI.tsv")
t, gdp = read_file("/tmp/GDP.tsv")
investment_to_gdp = 100*investment/gdp
def color(time):
if time < year_as_num(1980):
return 'g'
if time < year_as_num(1990):
return 'r'
return 'b'
scatter(employment, investment_to_gdp, c = [color(time) for time in t] )
ylabel("Investment to GDP (%)")
xlabel("Employment to Population (%)")
## a, b = polyfit(employment, investment_to_gdp,1)
## plot( employment, a*employment+b,'y', label="All years")
#Best fit line for 90's
idx = where(t >= year_as_num(1990))
a, b = polyfit(employment[idx], investment_to_gdp[idx], 1)
plot( employment[idx], a*employment[idx]+b, 'b', label="1990-2009")
#Best fit for 80's
idx = where(logical_and(t >= year_as_num(1980), t <= year_as_num(1990)))
a, b = polyfit(employment[idx], investment_to_gdp[idx], 1)
plot( employment[idx], a*employment[idx]+b, 'r', label="1980-1990")
#best fit for pre-80's
index = where(t <= year_as_num(1980))
a, b = polyfit(employment[index], investment_to_gdp[index], 1)
plot( sort(employment[index]), a*sort(employment[index])+b, 'g', label="1948-1980")
legend()
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment