Пример #1
0
def main():
    pmf = Pmf()

    pmf.Set('Bowl 1', 0.5)
    pmf.Set('Bowl 2', 0.5)

    pmf.Mult('Bowl 1', 0.75)
    pmf.Mult('Bowl 2', 0.5)

    pmf.Normalize()

    print(pmf.Prob('Bowl 1'))
    print(pmf.Prob('Bowl 2'))
Пример #2
0
"""This file contains code for use with "Think Bayes",
by Allen B. Downey, available from greenteapress.com

Copyright 2012 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

from thinkbayes2 import Pmf

pmf = Pmf()
pmf.Set('Bowl 1', 0.5)
pmf.Set('Bowl 2', 0.5)

pmf.Mult('Bowl 1', 0.75)
pmf.Mult('Bowl 2', 0.5)

pmf.Normalize()

print(pmf.Prob('Bowl 1'))
Пример #3
0
import numpy as np
import matplotlib.pyplot as plt

from thinkbayes2 import Pmf, Suite, CredibleInterval, Beta

# PMF for 6-sided die
pmf = Pmf()
for x in [1, 2, 3, 4, 5, 6]:
    pmf.Set(x, 1 / 6)
print(pmf)

# How to build up a pmf from a list of strings
pmf2 = Pmf()
for word in ['a', 'in', 'or', 'to', 'a', 'me', 'in']:
    pmf2.Incr(word, 1)
pmf2.Normalize()
print(pmf2)
print("Probability of letter a:", pmf2.Prob(
    'a'))  # Typo p12 print pmf.Prob('the') should read print(pmf.Prob('the'))

# PMF for the Cookie problem
pmf = Pmf()
# Prior:
pmf.Set("Bowl 1", 0.5)
pmf.Set("Bowl 2", 0.5)
# Posterior:
# First multiply prior by likelihood
pmf.Mult("Bowl 1", 0.75)
pmf.Mult("Bowl 2", 0.5)
# Then normalise (we can do this because the hypotheses are mutually exclusive and collectively exhaustive,
# i.e. only one of the hypotheses can be true and there can be no other hypothesis)
Пример #4
0
from __future__ import print_function, division
import sys
sys.path.append("../lib/ThinkBayes2/code/")
from thinkbayes2 import Suite, Pmf, SampleSum, MakeMixture
import thinkplot
from simulationDD02 import Die

pmf_dice = Pmf()
pmf_dice.Set(Die(4), 2)
pmf_dice.Set(Die(6), 3)
pmf_dice.Set(Die(8), 2)
pmf_dice.Set(Die(12), 1)
pmf_dice.Set(Die(20), 1)
pmf_dice.Normalize()
print(pmf_dice)

print("#################################################")
mix = Pmf()
for die, weight in pmf_dice.Items():
    for outcome, prob in die.Items():
        mix.Incr(outcome, weight * prob)

#Shorthand for above
#mix = MakeMixture(pmf_dice)
print(mix)

thinkplot.Hist(mix)
thinkplot.Save(root='bar',
               xlabel='Mixture over a set of dice',
               ylabel='Probability',
               formats=['pdf'])
Пример #5
0
from thinkbayes2 import Pmf, Suite
import thinkplot

# Probability of each dice given that when rolled the output is 6

dice = Pmf(['4-sided', '6-sided', '8-sided', '12-sided'])

dice['4-sided'] *= 0
dice['6-sided'] *= 1 / 6
dice['8-sided'] *= 1 / 8
dice['12-sided'] *= 1 / 12
dice.Normalize()
print(dice)

suite = Suite([4, 6, 8, 12])
suite[4] *= 0
suite[6] *= 1 / 6
suite[8] *= 1 / 8
suite[12] *= 1 / 12
suite.Normalize()
print(suite)


class Dice(Suite):
    # hypo is the number if sides in the die
    # dat is the outcome

    def Likelihood(self, data, hypo):
        return 0 if data > hypo else 1 / hypo

Пример #6
0
from thinkbayes2 import Pmf
import thinkplot

d6 = Pmf()

for x in [1, 2, 3, 4, 5, 6]:
    d6[x] = 1

d6.Normalize()
print(d6)

twice = d6 + d6

# If the sum of two dice is greater than 3, then we update the dictionary
twice[2] = 0
twice[3] = 0
twice.Normalize()
print(twice)

thinkplot.Hist(twice)
thinkplot.show()
Пример #7
0
#!/usr/env python
import sys
sys.path.append("../lib/ThinkBayes2/code/")
from thinkbayes2 import Pmf

pmf = Pmf()
for x in [1, 2, 3, 4, 5, 6]:
    pmf.Set(x, 1 / 6.0)
print(pmf)

words = Pmf()
f = open('alice.txt', 'r')
for line in f:
    for word in line.split():
        words.Incr(word.strip(), 1)
#print(words)
words.Normalize()
print(words)

print("Finished")
Пример #8
0
from thinkbayes2 import Pmf

# Cookie Problem

# Prior 50%-50%
cookie = Pmf(['Bowl 1', 'Bowl 2'])

# p (Vanilla|Bowl1) = 30/40
cookie['Bowl 1'] *= 0.75
# p (Vanilla|Bowl2) = 20/40
cookie['Bowl 2'] *= 0.5

# Normalize, return values is p(D)
cookie.Normalize()

# Posteriors
print(cookie)

# Suppose we put the first cookie back, stir, choose again from the same bowl, and
# get a chocolate cookie

# p (Chocolate|Bowl1) = 10/40
cookie['Bowl 1'] *= 0.25
# p (Chocolate|Bowl2) = 20/40
cookie['Bowl 2'] *= 0.5

cookie.Normalize()
print(cookie)