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 CH7_2(): """ http://www.ruanyifeng.com/blog/2015/06/poisson-distribution.html 1. 一场比赛平均进球数为lam, 每场比赛进球分布: 泊松分布 (进球可以在任何时间点发生) eg. 某医院平均每小时出生3个婴儿 重点是: 次数 2. 进球间隔的分布: 指数分布 eg. 某医院婴儿出生的时间间隔(20分钟一个(0.3h)) 重点是: 间隔 泊松分布是单位时间内独立事件发生次数的概率分布 指数分布是独立事件的时间间隔的概率分布 """ # 单位时间内出生1 - 10个婴儿的泊松分布 pmf = thinkbayes.MakePoissonPmf(3, 10, step=1) thinkplot.Clf() thinkplot.Pmf(pmf) # thinkplot.Show(); # 婴儿出生时间间隔(20分钟) pmf = thinkbayes.MakeExponentialPmf(0.3, 10, n=200) thinkplot.Clf() thinkplot.Pmf(pmf) thinkplot.Show();
def CH7_4(show = 1): """ 混合分布 """ suite1, suite2 = CH7_3(0) # 均值: mu1 = suite1.Mean() mu2 = suite2.Mean() print("Mean1: ", mu1) print("Mean2: ", mu2) if show: # 使用均值, 计算泊松分布 (下一场比赛进球分布) pos1 = thinkbayes.MakePoissonPmf(mu1, 10, step=1) pos2 = thinkbayes.MakePoissonPmf(mu2, 10, step=1) thinkplot.Clf() thinkplot.PrePlot(num=2) thinkplot.Pmfs([pos1, pos2]) thinkplot.Show(title='Poisson', xlabel='Goals per game', ylabel='Probability') # 混合分布 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') mix1 = _MixPmf(suite1) mix2 = _MixPmf(suite2) if show: thinkplot.Clf() thinkplot.PrePlot(num=2) thinkplot.Pmfs([mix1, mix2]) thinkplot.Show(title='Mixture', xlabel='Goals per game', ylabel='Probability') return mix1, mix2
def __init__(self, r, f, high=500, step=5): """Initializes the suite. r: known emission rate, r f: fraction of particles registered high: maximum number of particles, n step: step size between hypothetical values of n """ pmf = thinkbayes.MakePoissonPmf(r, high, step=step) thinkbayes.Suite.__init__(self, pmf, name=r) self.r = r self.f = f
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