def MakePoissonPmf(lam, high): pmf = thinkbayes.Pmf() for k in xrange(0, high + 1): p = EvalPoissonPmf(lam, k) pmf.Set(k, p) pmf.Normalize() return pmf
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 _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_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 MakeGoalPmf(suite): metapmf = thinkbayes.Pmf() for lam, prob in suite.Items(): pmf = MakePoissonPmf(lam, 10) # MakePoissonPmf metapmf.Set(pmf, prob) mix = MakeMixture(metapmf) #MakeMixture return mix
def DivideValues(pmf, denom): """Divides the values in a PMF by denom. Returns a new PMF.""" new = thinkbayes.Pmf() for val, prob in pmf.Items(): if val >= 0: x = 1.0 * val / denom new.Incr(x, prob) return new
def MakeGaussianPmf(mu, sigma, nsigma, n=101): pmf = thinkbayes.Pmf() low = mu - sigma * nsigma high = mu + sigma * nsigma for x in numpy.linspace(low, high, n): p = EvalGaussianPdf(x, mu, sigma) # EvalGaussianPdf pmf.Set(x, p) pmf.Normalize() return pmf
def MakeGaussianPmf(mu, sigma, nsigma, n=101): pmf = thinkbayes.Pmf() low = mu - nsigma * sigma high = mu + nsigma * sigma for x in numpy.linspace(low, high, n): p = scipy.stats.norm.pdf(x, mu, sigma) pmf.Set(x, p) pmf.Normalize() return pmf
def CoefVariation(suite): """Computes the distribution of CV. suite: Pmf that maps (x, y) to z Returns: Pmf object for CV. """ pmf = thinkbayes.Pmf() for (m, s), p in suite.Items(): pmf.Incr(s / m, p) return pmf
def MakeUniformPmf(low, high): """Make a uniform Pmf. low: lowest value (inclusive) high: highest value (inclusive) """ pmf = thinkbayes.Pmf() for x in MakeRange(low=low, high=high): pmf.Set(x, 1) pmf.Normalize() return pmf
def BinaryPmf(p): """Makes a Pmf with values 1 and 0. p: probability given to 1 Returns: Pmf object """ pmf = thinkbayes.Pmf() pmf.Set(1, p) pmf.Set(0, 1 - p) return pmf
def MarginalProduct(suite): """Extracts the distribution of the product of the parameters. suite: Suite returns: Pmf """ pmf = thinkbayes.Pmf() for (q, r), prob in suite.Items(): pmf.Incr(q * r, prob) return pmf
def DivideValues(pmf, denom): """Divides the values in a Pmf by denom. Returns a new Pmf. """ new = thinkbayes.Pmf() denom = float(denom) for val, prob in pmf.Items(): x = val / denom new.Set(x, prob) return new
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 PmfMax(pmf1, pmf2): """Computes the distribution of the max of values drawn from two Pmfs. pmf1, pmf2: Pmf objects returns: new Pmf """ res = thinkbayes.Pmf() for v1, p1 in pmf1.Items(): for v2, p2 in pmf2.Items(): res.Incr(max(v1, v2), p1 * p2) return res
def MarginalDistribution(suite, index): """Extracts the marginal distribution of one parameter. suite: Suite index: which parameter returns: Pmf """ pmf = thinkbayes.Pmf() for t, prob in suite.Items(): pmf.Incr(t[index], prob) return pmf
def MakeGaussianPmf(mu, sigma, num_sigmas, n=101): pmf = thinkbayes.Pmf() low = mu - sigma * num_sigmas high = mu + sigma * num_sigmas for x in numpy.linspace(low, high, n): p = scipy.stats.norm.pdf( x, mu, sigma ) # this seems to return poisson distribution may du to iteration... # p = EvalGaussianPdf(x, mu, sigma) pmf.Set(x, p) pmf.Normalize() return pmf
def PmfCorrect(efficacy, difficulties): """Computes the distribution of correct responses. efficacy: personal ability to answer questions difficulties: list of difficulties, one for each question Returns: new Pmf object """ pmf0 = thinkbayes.Pmf([0]) ps = [ProbCorrect(efficacy, difficulty) for difficulty in difficulties] pmfs = [BinaryPmf(p) for p in ps] dist = sum(pmfs, pmf0) return dist
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 ReverseScale(pmf, scale): """Applies the reverse scale to the values of a PMF. Args: pmf: Pmf of scaled scores scale: Interpolator object Returns: Pmf of raw scores """ new = thinkbayes.Pmf() for val, prob in pmf.Items(): raw = scale.Reverse(val) new.Incr(raw, prob) return new
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 ReverseScale(self, pmf): """Applies the reverse scale to the values of a PMF. Args: pmf: Pmf object scale: Interpolator object Returns: new Pmf """ new = thinkbayes.Pmf() for val, prob in pmf.Items(): raw = self.Reverse(val) new.Incr(raw, prob) return new
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 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 MakeLocationPmf(alpha, beta, locations): """Computes the Pmf of the locations, given alpha and beta. Given that the shooter is at coordinates (alpha, beta), the probability of hitting any spot is inversely proportionate to the strafe speed. alpha: x position beta: y position locations: x locations where the pmf is evaluated Returns: Pmf object """ pmf = thinkbayes.Pmf() for x in locations: prob = 1.0 / StrafingSpeed(alpha, beta, x) pmf.Set(x, prob) pmf.Normalize() return pmf
def PmfMax(pmf1, pmf2): """Computes the distribution of the max of values drawn from two Pmfs. pmf1, pmf2: Pmf objects returns: new Pmf """ res = thinkbayes.Pmf() i = 0 j = 0 for v1, p1 in pmf1.Items(): i = i + 1 print(v1, p1) for v2, p2 in pmf2.Items(): j = j + 1 res.Incr(max(v1, v2), p1*p2) # print res.Items() print res.Items() print i print j return res
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)
def MakePoissonPmf(lam, high=10, n=101): pmf = thinkbayes.Pmf() for x in xrange(0, 1 + high): p = EvalPoissonPmf(lam, x) #EvalPoissonPmf pmf.Set(x, p) return pmf
def MakeMixture(metapmf): mix = thinkbayes.Pmf() for pmf, p1 in metapmf.Items(): for x, p2 in pmf.Items(): mix.Incr(x, p1 * p2) return mix