Esempio n. 1
0
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 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
Esempio n. 3
0
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 __init__(self,
                 prior_lam=.05,
                 max_wait_min=30,
                 name='BusWaitPoissonProcess'):
        """Initializes the BusWait object.

        prior_lam: assumed rate of bus arrivals in buses per minute. From the problem, 20
        minutes between buses implies a rate of .05 arrivals per minute
        longest_wait: highest plausible wait time value to consider
        name: string
        """
        self.prior_lam = prior_lam
        self.max_min = max_wait_min
        pmf = thinkbayes.MakeExponentialPmf(lam=self.prior_lam,
                                            high=self.max_min - 1,
                                            n=self.max_min)
        thinkbayes.Suite.__init__(self, pmf, name=name)