def uniformMixPMF_expWaitTimes(lams=[.2, .1], max_wait=20, verbose=True): '''MAKE DOCSTRING BETTER BY OVERWRITING WHAT'S BELOW Attempt to sketch out the scenario in the hint, assuming that bus arrival times are Exp(5) or Exp(10), with either exponential distribution having an equal likelihood of happening. lams: list of lambdas for the exponential distributions being considered. Note that in order to produce wait time estimates in minutes, these lambdas are fractional (number of buses arriving per minute) max_wait: what is the maximum verbose: Want to call Suite.Print() on each PMF and the metaPMF? TODO: Reparameterize to (0, N-1) to match conventions with expWaitTimes ''' metaPmf = thinkbayes.Pmf() for lam in lams: expPmf = thinkbayes.MakeExponentialPmf(lam=lam, high=max_wait - 1, n=max_wait) if verbose: print 'Results for wait time {}'.format(int(1 / lam)) expPmf.Print() metaPmf.Set(expPmf, 1) metaPmf.Normalize() exp_bounds = '_'.join([str(int(1 / v)) for v in lams]) mix_name = 'unifMix-expWaits{}'.format(exp_bounds) expMix = thinkbayes.MakeMixture(metapmf=metaPmf, name=mix_name) if verbose: print('Results for {}'.format(expMix.name)) expMix.Print() return expMix
def _MixPmf(suite): high = 10 metapmf = thinkbayes.Pmf() for lam, prob in suite.Items(): pmf = thinkbayes.MakePoissonPmf(lam, high, step=1) metapmf.Set(pmf, y=prob) return thinkbayes.MakeMixture(metapmf, name='mix')
def uniformMixPMF_uniformWaitTimes(wait_times=[5, 10], verbose=False): '''Attempt to sketch out the scenario in the hint, assuming that bus arrival times are either Unif(1,5) and Unif(1,10), with either uniform distribution having an equal likelihood of happening. wait_times: list of upper bounds for Unif(1, T) distributions verbose: Want to call Suite.Print() on each PMF and the metaPMF? TODO: Reparameterize to (0, N-1) to match conventions with expWaitTimes ''' metaPmf = thinkbayes.Pmf() for wait in wait_times: unifPmf = thinkbayes.MakeUniformPmf(low=0, high=wait - 1, n=wait) if verbose: print('Results for wait time {}'.format(wait)) unifPmf.Print() metaPmf.Set(unifPmf, 1) metaPmf.Normalize() unif_bounds = '_'.join([str(w) for w in wait_times]) mix_name = 'unifMix-unifWaits{}'.format(unif_bounds) unifMix = thinkbayes.MakeMixture(metapmf=metaPmf, name=mix_name) if verbose: print('Results for {}'.format(unifMix.name)) unifMix.Print() return unifMix
def MakeRawScoreDist(self, efficacies): """Makes the distribution of raw scores for given difficulty. efficacies: Pmf of efficacy """ pmfs = thinkbayes.Pmf() for efficacy, prob in efficacies.Items(): scores = self.PmfCorrect(efficacy) pmfs.Set(scores, prob) mix = thinkbayes.MakeMixture(pmfs) return mix
def PmfOfWaitTime(pmf_zb): """Distribution of wait time. pmf_zb: dist of gap time as seen by a random observer Returns: dist of wait time (also dist of elapsed time) """ metapmf = thinkbayes.Pmf() for gap, prob in pmf_zb.Items(): uniform = MakeUniformPmf(0, gap) metapmf.Set(uniform, prob) pmf_y = thinkbayes.MakeMixture(metapmf, name='y') return pmf_y
def MakeGoalTimePmf(suite): """Makes the distribution of time til first goal. suite: distribution of goal-scoring rate returns: Pmf of goals per game """ metapmf = thinkbayes.Pmf() for lam, prob in suite.Items(): pmf = thinkbayes.MakeExponentialPmf(lam, high=2, n=2001) metapmf.Set(pmf, prob) mix = thinkbayes.MakeMixture(metapmf, name=suite.name) return mix
def MakeGoalPmf(suite, high=10): """Makes the distribution of goals scored, given distribution of lam. suite: distribution of goal-scoring rate high: upper bound returns: Pmf of goals per game """ metapmf = thinkbayes.Pmf() for lam, prob in suite.Items(): pmf = thinkbayes.MakePoissonPmf(lam, high) metapmf.Set(pmf, prob) mix = thinkbayes.MakeMixture(metapmf, name=suite.name) return mix
def __init__(self, wtc, are, num_passengers=15): """Constructor. wtc: WaitTimeCalculator are: ArrivalTimeEstimator num_passengers: number of passengers seen on the platform """ self.metapmf = thinkbayes.Pmf() for lam, prob in sorted(are.post_lam.Items()): ete = ElapsedTimeEstimator(wtc, lam, num_passengers) self.metapmf.Set(ete.pmf_y, prob) self.mixture = thinkbayes.MakeMixture(self.metapmf) lam = are.post_lam.Mean() ete = ElapsedTimeEstimator(wtc, lam, num_passengers) self.point = ete.pmf_y
def CH5_6(): """ 混合分布, 汇总多个分布的贡献 骰子个数 骰子面数 5 4-sides 4 6-sides 3 8-sides 2 12-sides 1 20-sides """ thinkplot.PrePlot(num=2) # (权重, 骰子) dices = [(5, Die(4)), (4, Die(6)), (3, Die(8)), (2, Die(12)), (1, Die(20))] mix = thinkbayes.Pmf() for w, die in dices: for v, p in die.Items(): mix.Incr(v, w * p) mix.Normalize() mix.name = 'mix-1' thinkplot.Pmf(mix) # 方法2 pmf_dices = thinkbayes.Pmf() pmf_dices.Set(Die(4), y=5) pmf_dices.Set(Die(6), y=4) pmf_dices.Set(Die(8), y=3) pmf_dices.Set(Die(12), y=2) pmf_dices.Set(Die(20), y=1) pmf_dices.Normalize() mix = thinkbayes.MakeMixture(pmf_dices, name='mix-2') mix.name = 'mix-2' thinkplot.Pmf(mix) thinkplot.Show()
def DistOfN(self, name=''): """Returns the PMF of n.""" return thinkbayes.MakeMixture(self, name=name)
def main(): pmf_dice = thinkbayes.Pmf() pmf_dice.Set(Die(4), 5) pmf_dice.Set(Die(6), 4) pmf_dice.Set(Die(8), 3) pmf_dice.Set(Die(12), 2) pmf_dice.Set(Die(20), 1) pmf_dice.Normalize() mix = thinkbayes.Pmf() for die, weight in pmf_dice.Items(): for outcome, prob in die.Items(): mix.Incr(outcome, weight * prob) mix = thinkbayes.MakeMixture(pmf_dice) colors = thinkplot.Brewer.Colors() thinkplot.Hist(mix, width=0.9, color=colors[4]) thinkplot.Save(root='dungeons3', xlabel='Outcome', ylabel='Probability', formats=FORMATS) random.seed(17) d6 = Die(6, 'd6') dice = [d6] * 3 three = thinkbayes.SampleSum(dice, 1000) three.name = 'sample' three.Print() three_exact = d6 + d6 + d6 three_exact.name = 'exact' three_exact.Print() thinkplot.PrePlot(num=2) thinkplot.Pmf(three) thinkplot.Pmf(three_exact, linestyle='dashed') thinkplot.Save(root='dungeons1', xlabel='Sum of three d6', ylabel='Probability', axis=[2, 19, 0, 0.15], formats=FORMATS) thinkplot.Clf() thinkplot.PrePlot(num=1) # compute the distribution of the best attribute the hard way # best_attr2 = PmfMax(three_exact, three_exact) # best_attr4 = PmfMax(best_attr2, best_attr2) # best_attr6 = PmfMax(best_attr4, best_attr2) # thinkplot.Pmf(best_attr6) # and the easy way best_attr_cdf = three_exact.Max(6) best_attr_cdf.name = '' best_attr_pmf = thinkbayes.MakePmfFromCdf(best_attr_cdf) best_attr_pmf.Print() thinkplot.Pmf(best_attr_pmf) thinkplot.Save(root='dungeons2', xlabel='Sum of three d6', ylabel='Probability', axis=[2, 19, 0, 0.23], formats=FORMATS)