def testPmfProbLess(self): pmf = thinkbayes.MakePmfFromList([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]) p = pmf.ProbLess(3) self.assertAlmostEquals(p, 0.3) p = pmf < 4 self.assertAlmostEquals(p, 0.6) p = pmf < pmf self.assertAlmostEquals(p, 0.35) p = pmf > pmf self.assertAlmostEquals(p, 0.35) p = pmf <= pmf self.assertAlmostEquals(p, 0.65) p = pmf >= pmf self.assertAlmostEquals(p, 0.65) p = pmf == pmf self.assertAlmostEquals(p, 0.3) p = pmf != pmf self.assertAlmostEquals(p, 0.7)
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, xs, pcounts, passenger_data): self.xs = xs self.pcounts = pcounts self.passenger_data = passenger_data self.wait_times = [y for _k1, y, _k2 in passenger_data] self.pmf_y = thinkbayes.MakePmfFromList(self.wait_times, name="y") dirichlet = GapDirichlet2(self.xs) dirichlet.params /= 1.0 dirichlet.Preload(self.pcounts) dirichlet.params /= 20.0 self.prior_zb = dirichlet.PredictivePmf(self.xs, name="prior zb") for k1, y, _k2 in passenger_data: dirichlet.Update((k1, y)) self.pmf_mean_zb = dirichlet.PmfMeanZb() self.post_zb = dirichlet.PredictivePmf(self.xs, name="post zb") self.post_z = UnbiasPmf(self.post_zb, name="post z")
def SampleSum(dists, n): pmf = thinkbayes.MakePmfFromList(RandomSum(dists) for i in xrange(n)) return pmf
def PmfMeanZb(self): """Makes the Pmf of mean zb. Values stored in mean_zbs. """ return thinkbayes.MakePmfFromList(self.mean_zbs)
def sampleMax(dists, n): pmf = thinkbayes.MakePmfFromList(RandomMax(dists) for i in xrange(n)) return pmf
def SampleMax(dists, n): return thinkbayes.MakePmfFromList([RandomMax(dists) for x in range(n)])
351.0, 286.0, 373.0, 232.0, 393.0, 745.0, 636.0, 758.0, ] #print(OBSERVED_GAP_TIMES) print "cumulated data number :", len(OBSERVED_GAP_TIMES) #OBSERVED_GAP_TIMES = OBSERVED_GAP_TIMES/60 # this is not working... for i in xrange(0, len(OBSERVED_GAP_TIMES)): OBSERVED_GAP_TIMES[i] = OBSERVED_GAP_TIMES[i] / 60 #print(OBSERVED_GAP_TIMES) cdf_z = thinkbayes.MakeCdfFromList(OBSERVED_GAP_TIMES) sample_z = cdf_z.Sample(220) pmf_z = thinkbayes.MakePmfFromList(sample_z) #pmf_z = scipy.stats.gaussian_kde(sample_z) thinkplot.Clf() thinkplot.preplot(2) thinkplot.Clf() thinkplot.Pmf(pmf_z) thinkplot.Save(root='chapter8_self1', xlabel='', ylabel='Probability', formats=['pdf'])
def addTeam( self, team, this_team, rest_of_league ): this_x_cdf = tb.MakeCdfFromPmf( tb.MakePmfFromList( this_team.X ) ) this_y_cdf = tb.MakeCdfFromPmf( tb.MakePmfFromList( this_team.Y ) ) other_x_cdf = tb.MakeCdfFromPmf( tb.MakePmfFromList( rest_of_league.X ) ) other_y_cdf = tb.MakeCdfFromPmf( tb.MakePmfFromList( rest_of_league.Y ) ) self.addCDFs( team, this_x_cdf, this_y_cdf, other_x_cdf, other_y_cdf )