Chapter 08 Exercise 2
In [1]:
import thinkstats2 as ts
import thinkplot as tp
import numpy as np
import math
In [7]:
def RMSE(estimates, actual):
e2 = [(estimate-actual)**2 for estimate in estimates]
mse = np.mean(e2)
return math.sqrt(mse)
In [8]:
def MeanError(estimates, actual):
errors = [estimate-actual for estimate in estimates]
return np.mean(errors)
In [ ]:
def
In [23]:
def expo_estimate(n, m):
lam = 2
means = []
medians = []
for x in range(m):
xs = np.random.exponential(1.0/lam, n)
L = 1/np.mean(xs)
Lm = math.log(2)/ts.Median(xs)
means.append(L)
medians.append(Lm)
cdf = ts.Cdf(means)
ci = cdf.Percentile(10), cdf.Percentile(90)
print 'rmse L', round(RMSE(means, lam), 3)
print 'rmse Lm', round(RMSE(medians, lam), 3)
print 'mean error L', round(MeanError(means, lam), 3)
print 'mean error Lm', round(MeanError(medians, lam), 3)
print 'con intvs at 90%', round(ci[0], 3), "to" ,round(ci[1], 3)
In [ ]:
In [24]:
expo_estimate(10, 1000)
In [25]:
expo_estimate(30, 1000)
In [26]:
expo_estimate(80, 1000)
In [ ]: