In [1]:
import pandas
import os
%matplotlib inline
dt = pandas.read_csv(os.path.join(os.path.realpath('..'), 'detroit_response_time.csv'))
dt['Total_Trav'].describe()
Out[1]:
In [2]:
import pandas
%matplotlib inline
dt = pandas.read_csv(os.path.join(os.path.realpath('..'), 'arlington_response_time.csv'))
dt['Total_Trav'].hist()
Out[2]:
In [3]:
dt['Total_Trav'].describe()
Out[3]:
In [4]:
from scipy.stats import lognorm
import numpy as np
import matplotlib.pyplot as plt
# Use this to get the tuple shape, location, and scale.
samp = lognorm.fit(dt['Total_Trav'])
print samp
lognorm.rvs(*samp)
x = np.linspace(0, 14, 1000)
pdf_fitted = lognorm.pdf(x, samp[0], loc=samp[1], scale=samp[2])
#dt['Total_Trav'].hist(normed=True)
fig, ax = plt.subplots(1,1)
plt.plot(x, pdf_fitted, 'r-')
plt.hist(dt['Total_Trav'], normed=True)
Out[4]:
In [5]:
class DrawType(object):
"""
Implements a base DrawType object.
"""
def draw(self):
raise NotImplementedError
class LogNormalDraw(DrawType):
"""
Implements a log normal draw.
>>> import numpy as np
>>> np.random.seed(1234)
>>> ud = LogNormalDraw(0.3381962232249362, -1.0844073333047395, 3.1682731892016429)
>>> ud.draw()
2.631505249260421
"""
def __init__(self, shape, location, scale, multiplier=1):
self.shape = shape
self.location = location
self.scale = scale
self.multiplier = multiplier
def draw(self):
return lognorm.rvs(self.shape, self.location, self.scale) * self.multiplier
In [6]:
lnd = LogNormalDraw(*samp)
res = []
for i in range(10000):
res.append(lnd.draw())
res = np.array(res)
print res.max()
print res.mean()