def TestGte(): """Tests the GapTimeEstimator.""" random.seed(17) xs = [60, 120, 240] gap_times = [60, 60, 60, 60, 60, 120, 120, 120, 240, 240] # distribution of gap time (z) pdf_z = thinkbayes.EstimatedPdf(gap_times) pmf_z = pdf_z.MakePmf(xs, name="z") wtc = WaitTimeCalculator(pmf_z, inverse=False) lam = 0.0333 n = 100 passenger_data = wtc.GenerateSamplePassengers(lam, n) pcounts = [0, 0, 0] ite = GapTimeEstimator(xs, pcounts, passenger_data) thinkplot.Clf() # thinkplot.Cdf(wtc.pmf_z.MakeCdf(name="actual z")) thinkplot.Cdf(wtc.pmf_zb.MakeCdf(name="actual zb")) ite.MakePlot()
def RunSimpleProcess(gap_times, lam=0.0333, num_passengers=15, plot=True): """Runs the basic analysis and generates figures. gap_times: sequence of float lam: arrival rate in passengers per second num_passengers: int number of passengers on the platform plot: boolean, whether to generate plots Returns: WaitTimeCalculator, ElapsedTimeEstimator """ global UPPER_BOUND UPPER_BOUND = 1200 cdf_z = thinkbayes.MakeCdfFromList(gap_times).Scale(1.0 / 60) print('CI z', cdf_z.CredibleInterval(90)) xs = MakeRange(low=10) pdf_z = thinkbayes.EstimatedPdf(gap_times) pmf_z = pdf_z.MakePmf(xs, name="z") wtc = WaitTimeCalculator(pmf_z, inverse=False) if plot: wtc.PlotPmfs() wtc.MakePlot() ete = ElapsedTimeEstimator(wtc, lam, num_passengers) if plot: ete.MakePlot() return wtc, ete
def RunLoop(gap_times, nums, lam=0.0333): """Runs the basic analysis for a range of num_passengers. gap_times: sequence of float nums: sequence of values for num_passengers lam: arrival rate in passengers per second Returns: WaitMixtureEstimator """ global UPPER_BOUND UPPER_BOUND = 4000 thinkplot.Clf() RandomSeed(18) # resample gap_times n = 220 cdf_z = thinkbayes.MakeCdfFromList(gap_times) sample_z = cdf_z.Sample(n) pmf_z = thinkbayes.MakePmfFromList(sample_z) # compute the biased pmf and add some long delays cdf_zp = BiasPmf(pmf_z).MakeCdf() sample_zb = cdf_zp.Sample(n) + [1800, 2400, 3000] # smooth the distribution of zb pdf_zb = thinkbayes.EstimatedPdf(sample_zb) xs = MakeRange(low=60) pmf_zb = pdf_zb.MakePmf(xs) # unbias the distribution of zb and make wtc pmf_z = UnbiasPmf(pmf_zb) wtc = WaitTimeCalculator(pmf_z) probs = [] for num_passengers in nums: ete = ElapsedTimeEstimator(wtc, lam, num_passengers) # compute the posterior prob of waiting more than 15 minutes cdf_y = ete.pmf_y.MakeCdf() prob = 1 - cdf_y.Prob(900) probs.append(prob) # thinkplot.Cdf(ete.pmf_y.MakeCdf(name=str(num_passengers))) thinkplot.Plot(nums, probs) thinkplot.Save( root='redline5', xlabel='Num passengers', ylabel='P(y > 15 min)', formats=FORMATS, )
def __init__(self, prices, bids, diffs): """Construct the Player. prices: sequence of prices bids: sequence of bids diffs: sequence of underness (negative means over) """ self.pdf_price = thinkbayes.EstimatedPdf(prices) self.cdf_diff = thinkbayes.MakeCdfFromList(diffs) mu = 0 sigma = numpy.std(diffs) self.pdf_error = thinkbayes.GaussianPdf(mu, sigma)
def GenerateSampleData(gap_times, lam=0.0333, n=10): """Generates passenger data based on actual gap times. gap_times: sequence of float lam: arrival rate in passengers per second n: number of simulated observations """ xs = MakeRange(low=10) pdf_z = thinkbayes.EstimatedPdf(gap_times) pmf_z = pdf_z.MakePmf(xs, name="z") wtc = WaitTimeCalculator(pmf_z, inverse=False) passenger_data = wtc.GenerateSamplePassengers(lam, n) return wtc, passenger_data
def CH6_2(price1, price2): """ 两组展览品的价格分布 """ thinkplot.Clf() thinkplot.PrePlot(num=2) # 因为price变量值没有重复的, 所以PMF绘图是看不出什么的. # price1_pmf = thinkbayes.MakePmfFromList(price1, name='showcase1') # price2_pmf = thinkbayes.MakePmfFromList(price2, name='showcase2') price1_max = max(price1) price2_max = max(price2) price_max = max(price1_max, price2_max) xs = numpy.linspace(0, price_max + 100, num=150) price1_pdf = thinkbayes.EstimatedPdf(price1) price2_pdf = thinkbayes.EstimatedPdf(price2) price1_pmf = price1_pdf.MakePmf(xs, name='showcase1') price2_pmf = price2_pdf.MakePmf(xs, name='showcase2') thinkplot.Pmfs([price1_pmf, price2_pmf]) thinkplot.Show(xlabel='price $', ylabel='PMF')
def main(): data = ReadData() cols = zip(*data) price1, price2, bid1, bid2, diff1, diff2 = cols pdf = thinkbayes.EstimatedPdf(price1) # print(type(pdf)) low, high = 0, 75000 n = 101 xs = numpy.linspace(low, high, n) # print(pdf.Density(25000)) pmf = pdf.MakePmf(xs) thinkplot.PrePlot(1) thinkplot.Pmf(pmf) thinkplot.Save(root='price_self2', xlabel='', ylabel='Probability_density', formats=['pdf'])
import numpy import thinkbayes def ReadData(filename='showcases.2011.csv'): fp = open(filename) reader = csv.reader(fp) res = [] for t in reader: _heading = t[0] data = t[1:] try: data = [int(x) for x in data] # print heading, data[0], len(data) res.append(data) except ValueError: pass fp.close() return zip(*res) prices = ReadData() pdf = thinkbayes.EstimatedPdf(prices) '''low,high = 0,75000 n=101 xs=numpy.linspace(low,high,n) pmf=pdf.MakePmf(xs) for x,y in pmf.Items(): print x,y'''