Пример #1
0
def comparePriors():
    """Runs the analysis with two different priors and compares them."""
    dataset = [60]
    high = 1000

    thinkplot.clf()
    thinkplot.prePlot(num=2)

    constructors = [Train, Train2]
    labels = ['uniform', 'power law']

    # NOTE: the uniform prior means we assign probability 1/1000 to each hypotheses from 1 ... 1000
    # note then we normalize it and update by multiplying by likelihood then normalize again (why?)

    # NOTE: the power law prior means we assign 1/hypo to each hypothesis from 1 ... 1000
    # note: then normalize by summing total and dividing then update by likelihood and normalize again (why?)

    for constructor, label in zip(constructors, labels):
        suite = makePosterior(high, dataset, constructor)
        suite.name = label
        thinkplot.pmf(suite)

    thinkplot.save(root='train4',
                   xlabel='Number of trains',
                   ylabel='Probability')
Пример #2
0
def makePosterior(high, dataset):
    hypos = range(1, high + 1)
    suite = Train(hypos)
    suite.name = str(high)

    for data in dataset:
        suite.update(data)

    thinkplot.pmf(suite)
    return suite
Пример #3
0
def main():
    # note: METHOD 1: assume that f = 0.1 is known! ------------------------
    k = 15
    f = 0.1

    # plot Detector suites for a range of hypothetical r
    thinkplot.prePlot(num=3)
    for r in [100, 250, 400]:
        suite = Detector(r, f, step=1)
        suite.update(k)
        thinkplot.pmf(suite)
        print(suite.maximumLikelihood())

    thinkplot.save(root='jaynes1',
                   xlabel='Number of particles (n)',
                   ylabel='PMF',
                   formats=FORMATS)

    # note: METHOD 2: k is unknown. ---------------------------------------
    # plot the posterior distributions of r and n
    hypos = range(1, 501, 5)
    suite = Emitter(hypos, f=f)
    suite.update(k)

    thinkplot.prePlot(num=2)
    post_r = suite.distOfR(name='posterior r')
    post_n = suite.distOfN(name='posterior n')

    thinkplot.pmf(post_r)
    thinkplot.pmf(post_n)

    thinkplot.save(root='jaynes2',
                   xlabel='Emission rate',
                   ylabel='PMF',
                   formats=FORMATS)

    # note: BUILDING ON 2: optimization in Emitter's likelihood.
    hypos = range(1, 501, 5)
    suite = Emitter2(hypos, f=f)
    suite.update(k)

    thinkplot.prePlot(num=2)
    post_r = suite.distOfR(name='posterior r')
    post_n = suite.distOfN(name='posterior n')

    thinkplot.pmf(post_r)
    thinkplot.pmf(post_n)

    thinkplot.save(root='jaynes3',
                   xlabel='Emission rate',
                   ylabel='PMF',
                   formats=FORMATS)
Пример #4
0
def main():

    suite = version2()
    print(suite.mean())

    thinkplot.pmf(suite)
    thinkplot.show()

    suite = version3()
    print(suite.mean())

    thinkplot.pmf(suite)
    thinkplot.show()
def makePmfPlot(alpha = 10):
    """Plots Pmf of location for a range of betas."""
    locations = range(0, 31)

    betas = [10, 20, 40]
    thinkplot.prePlot(num=len(betas))

    for beta in betas:
        pmf = makeLocationPmf(alpha, beta, locations)
        pmf.name = 'PMF(alpha = 10, beta = %d)' % beta
        thinkplot.pmf(pmf)

    thinkplot.save('paintball1_pmfOfLocationForRangeOfBetas',
                   xlabel='Distance',
                   ylabel='Prob',
                   formats=FORMATS)
Пример #6
0
def main():
    # Step 1: Prior P(N) = 1/1000
    hypos = range(
        1, 1001
    )  # assume there are 1 - 1000 locomotives in the train, given we saw the number 60
    suite = Train(hypos)

    suite.update(60)
    print(suite.mean())

    thinkplot.prePlot(1)
    thinkplot.pmf(suite)
    thinkplot.save(root='train1',
                   xlabel='Number of trains',
                   ylabel='Probability',
                   formats=['pdf', 'eps'])
    thinkplot.show()
def makeConditionalPlot(suite):
    """Plots marginal CDFs for alpha conditioned on beta.

    suite: posterior joint distribution of location
    """
    betas = [10, 20, 40]
    thinkplot.prePlot(num=len(betas))

    for beta in betas:
        cond = suite.conditional(0, 1, beta) # i=0 (variable we want), j=1 (variable cond), val=beta (value that jth variable must have)
        cond.name = 'PMF(alpha | beta = %d)' % beta
        thinkplot.pmf(cond)

    # THe posterior conditional marginal distributions
    thinkplot.save('paintball3_marginalCDFS_for_alpha|beta',
                   xlabel='Distance',
                   ylabel='Prob',
                   formats=FORMATS)
Пример #8
0
    def plotPmfs(self):
        """Plot the PMFs."""
        print('Mean y', self.pmf_y.mean())
        print('Mean z', self.post_z.mean())
        print('Mean zb', self.post_zb.mean())

        thinkplot.pmf(self.pmf_y)
        thinkplot.pmf(self.post_z)
        thinkplot.pmf(self.post_zb)
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)