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
Beispiel #2
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
Beispiel #3
0
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
Beispiel #4
0
def pmfOfWaitTime(pmf_zb):
    """Distribution of wait time.

    pmf_zb: dist of gap time as seen by a random observer (the biased distribution)

    Returns: dist of wait time (also dist of elapsed time)
    """
    # NOTE: zb = Y + X, where Y = dist of wait time, and X = dist of elapsed time.
    # NOTE: Y = wait times = time between arrival of passenger and arrival of train.
    # NOTE: X = wait time = time between arrival of previous train and arrival of next passenger.
    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
Beispiel #5
0
    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 lmbda, prob in sorted(are.posteriorLambda.items()):
            ete = ElapsedTimeEstimator(wtc, lmbda, num_passengers)
            self.metaPmf.set(ete.pmf_y, prob)

        self.mixture = thinkbayes.makeMixture(self.metaPmf)

        lmbda = are.posteriorLambda.mean()
        ete = ElapsedTimeEstimator(wtc, lmbda, num_passengers)
        self.point = ete.pmf_y
def main():
    pmfDice = thinkbayes.PMF()
    pmfDice.set(Die(4), 5)
    pmfDice.set(Die(6), 4)
    pmfDice.set(Die(8), 3)
    pmfDice.set(Die(12), 2)
    pmfDice.set(Die(20), 1)
    pmfDice.normalize()

    #@fix: was unhashable error here:
    # http://stackoverflow.com/questions/10994229/how-to-make-an-object-properly-hashable
    # http://stackoverflow.com/questions/2909106/python-whats-a-correct-and-good-way-to-implement-hash

    mix = thinkbayes.PMF()
    for die, weight in pmfDice.items():
        for outcome, prob in die.items():
            mix.incr(outcome, weight * prob)

    mix = thinkbayes.makeMixture(pmfDice)

    colors = thinkplot.Brewer.getColors()
    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')

    # finding distribution of rolled-dice sum by SIMULATION
    dice = [d6] * 3
    three = thinkbayes.sampleSum(dice, 1000)
    three.name = 'sample'
    print("\n\nSAMPLING: ")
    three.printSuite()

    # finding distribution of rolled-dice sum by ENUMERATION
    threeExact = d6 + d6 + d6
    threeExact.name = 'exact'
    print("\n\nENUMERATION:")
    threeExact.printSuite()

    thinkplot.prePlot(num=2)
    thinkplot.pmf(three)
    thinkplot.pmf(threeExact, 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)

    # Note: pmf of max (best) attribute:
    bestAttribute2 = pmfMax(threeExact, threeExact)
    bestAttribute4 = pmfMax(bestAttribute2, bestAttribute2)
    bestAttribute6 = pmfMax(bestAttribute4, bestAttribute2)
    thinkplot.pmf(bestAttribute6)

    # Note: finding pmf max using efficient Cdf method:
    bestAttributeCdf = threeExact.max(6)  #@ Max() in class Cdf
    bestAttributeCdf.name = ''
    bestAttributePmf = thinkbayes.makePmfFromCdf(bestAttributeCdf)
    bestAttributePmf.printSuite()

    thinkplot.pmf(bestAttributePmf)
    thinkplot.save(root='dungeons2',
                   xlabel='Sum of three d6',
                   ylabel='Probability',
                   axis=[2, 19, 0, 0.23],
                   formats=FORMATS)
Beispiel #7
0
 def distOfN(self, name=''):
     """Returns the PMF of n."""
     return thinkbayes.makeMixture(self, name=name)