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)


rmse L 0.793
rmse Lm 1.781
mean error L 0.218
mean error Lm 0.673
con intvs at 90% 1.436 to 3.157

In [25]:
expo_estimate(30, 1000)


rmse L 0.395
rmse Lm 0.619
mean error L 0.062
mean error Lm 0.19
con intvs at 90% 1.603 to 2.572

In [26]:
expo_estimate(80, 1000)


rmse L 0.229
rmse Lm 0.349
mean error L 0.029
mean error Lm 0.079
con intvs at 90% 1.757 to 2.325

In [ ]: