def main():
    #    d6 = Die(6)
    #    d8 = Die(8)
    d6 = Pmf()
    print type(d6)
    d6.Set(Die(6), 2)
    print type(d6)
    d8 = Pmf()
    d8.Set(Die(8), 3)
    d12 = Pmf()
    d12.Set(Die(12), 1)
    d20 = Pmf()
    d20.Set(Die(20), 1)

    mix = Pmf()
    for dice in [d6, d8, d12, d20]:
        #        print type(dice)
        for die, weight in dice.Items():
            #            print type(die)
            for outcome, prob in die.Items():
                mix.Incr(outcome, weight * prob)
    mix.Normalize()

    thinkplot.PrePlot(1)
    thinkplot.Pmf(mix)
    thinkplot.Save(root='dice_Mix_self2',
                   xlabel='',
                   ylabel='Probability',
                   formats=['pdf'])
Example #2
0
def main():
    pmf_dice = Pmf()
    pmf_dice.Set(Die(6),2)
    pmf_dice.Set(Die(8),3)
    pmf_dice.Set(Die(12),1)
    pmf_dice.Set(Die(20),1)
    
    mix = Pmf()
    for die, weight in pmf_dice.Items():
        for outcome, prob in die.Items():
            mix.Incr(outcome, weight*prob)
    mix.Normalize()

    thinkplot.PrePlot(1)
    thinkplot.Pmf(mix)
    thinkplot.Save(root='dice_Mix_self3',xlabel='',ylabel='Probability',formats=['pdf'])
Example #3
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from thinkbayes 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')
Example #4
0
#p(H|D),在D的条件下H发生的概率

#先验概率PH
#似然度p(D|H)
#p(H)p(D|H)
#后验概率p(H|D)

#p(A|B) = p(A)p(B|A) / p(B)

from thinkbayes import Pmf

pmf = Pmf()

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

print(pmf)
Example #5
0
 def MakePmf(self,xs):
     pmf = Pmf()
     for x in xs:
         pmf.Set(x, self.Density(x))
     pmf.Normalize()
     return pmf
Example #6
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from thinkbayes import Pmf

pmf1 = Pmf()
for x in range(1, 6 + 1):
    pmf1.Set(x, 1 / 6.0)

pmf1.Print()

print("曲奇饼问题")
pmf2 = Pmf()
pmf2.Set('Bowl1', 0.5)
pmf2.Set('Bowl2', 0.5)

pmf2.Mult('Bowl1', 0.75)
pmf2.Mult('Bowl2', 0.5)

pmf2.Normalize()
print pmf2.Probs(pmf2.Values())
Example #7
0
## based on "Cookie" problem
## http://www.greenteapress.com/thinkbayes/

from thinkbayes import Pmf

pmf = Pmf()
pmf.Set("H1", 1 / 2.0)
pmf.Set("H2", 1 / 2.0)

pmf.Mult("H1", 30 / 40.0)
pmf.Mult("H2", 20 / 40.0)

pmf.Normalize()

for bowl in ["H1", "H2"]:
    print bowl, pmf.Prob(bowl)
Example #8
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 17 11:21:39 2020

@author: isajarspector
"""

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

print(pmf.Prob(1))
Example #9
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 thinkbayes import Pmf

pmf = Pmf()
pmf.Set('Bowl 1', 0.50)
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')
Example #10
0
from thinkbayes import Pmf

# make empty pmf
pmf = Pmf()
pmf.Set('Bowl 1', .5)  # P(Bowl 1)
pmf.Set('Bowl 2', .5)  # P(Bowl 2)
pmf.Print()

# picks vanilla cookie - update P of bowl with evidence
pmf.Mult('Bowl 1', .75)  # P(Vanilla | Bowl 1)
pmf.Mult('Bowl 2', .5)  # P(Vanilla | Bowl 2)
pmf.Print()

print pmf.Normalize()
pmf.Print()

# picks chocolate cookie - update the pmf with new evidence
pmf.Mult('Bowl 1', .25)  # P(Chocolate | Bowl 1)
pmf.Mult('Bowl 2', .5)  # P(Chocolate | Bowl 2)
pmf.Print()

print pmf.Normalize()
pmf.Print()
Example #11
0
from thinkbayes import Pmf

pmf = Pmf()
pmf.Set('b1', 0.5)
pmf.Set('b2', 0.5)
print pmf.Items()

pmf.Mult('b1', 0.75)
pmf.Mult('b2', 0.5)
print pmf.Items()

pmf.Normalize()
print pmf.Items()
print pmf.Prob('b1')


Example #12
0
def CH2_2():
    """
    Cookie Problem (曲奇饼问题)
    碗1: 30个香草味 10个巧克力味
    碗2: 20个香草味 20个巧克力味

          |     碗1       |     碗2       |
          | 香草 | 巧克力 | 香草 | 巧克力 |
   -------+---------------+---------------+
    先验  |     0.5       |     0.5       |
    ------+---------------+---------------+ 
    似然度| 0.75 | 0.25   |  0.5 | 0.5    | 
    ------+------+--------+---------------- 
    后验  |0.375 | 0.125  | 0.25 | 0.25   |
   -------+--^---+--------+----------^----+
          |  |   |        |          |    |
          |  +---+--------+----------+    |
    归一化| 0.6  |        |               |

      0.375 / (0.375 + 0.25)
    """

    # 先验
    pmf = Pmf()
    pmf.Set(x='b1', y=0.5)
    pmf.Set(x='b2', y=0.5)

    # 乘以似然度 (香草)
    pmf.Mult('b1', 0.75)
    pmf.Mult('b2', 0.5)

    # 归一化
    pmf.Normalize(fraction=1.0)
    print("b1 = %.3f" % (pmf.Prob('b1', default=0)))
    print("b2 = %.3f" % (pmf.Prob('b2', default=0)))

    class Cookie(Pmf):
        # 似然度
        mixes = {
            'bow1': {
                'vanilla': 0.75,
                'chocolate': 0.24
            },
            'bow2': {
                'vanilla': 0.5,
                'chocolate': 0.5
            }
        }

        def __init__(self, hypos):
            Pmf.__init__(self)
            # 先验概率
            for hypo in hypos:
                self.Set(hypo, 1)
            self.Normalize()

        # 计算后验概率
        def Update(self, data):
            for hypo in self.Values():
                # 在该假设的条件下, 计算似然度
                like = self.Likelihood(data, hypo)
                self.Mult(hypo, like)
            return self.Normalize()

        def Likelihood(self, data, hypo):
            mix = self.mixes[hypo]
            return mix[data]

    hypos = ["bow1", "bow2"]
    data = 'vanilla'
    cookie = Cookie(hypos)
    # D: 香草
    cookie.Update(data)
    for hypo, prob in sorted(cookie.Items()):
        print('p(%s/%s) = %.3f' % (hypo, data, prob))

    # 有放回replace, 取三块
    cookie = Cookie(hypos)
    dataset = ['vanilla', 'chocolate', 'vanilla']
    for data in dataset:
        cookie.Update(data)
    for hypo, prob in sorted(cookie.Items()):
        print('p(%s/%s) = %.3f' % (hypo, dataset, prob))
Example #13
0
"""

PMF - probability mass function

"""

from thinkbayes import Pmf
from thinkbayes import Suite


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





# re-introducing pmf.
word_list = ['atirei', 'o', 'pau', 'atirei', 'gato', 'gato']

pmf = Pmf()
for word in word_list:
    pmf.Incr(word, 1)
pmf.Normalize()

print(pmf.Prob('gato'))

Example #14
0
# p(H|D) = p(H) * p(D|H) / p(D)
#
# posterior = prior * likelihood / normalizing constant

# "Suite": a MECE set of hypotheses

#===========#
# Chapter 2 #
#===========#

from thinkbayes import Pmf  # requires print statement fixes

# build a 6-sided die:
die = Pmf()
for x in range(1, 7):
    die.Set(x, 1 / 6.)

# solve the cookie problem
cookies = Pmf()
# set our prior distribution
cookies.Set('Bowl 1', 0.5)
cookies.Set('Bowl 2', 0.5)
# update the distribution based on evidence of a vanilla cookie
cookies.Mult('Bowl 1', 0.75)
cookies.Mult('Bowl 2', 0.5)
# renormalize
cookies.Normalize()
# posterior probability
print(cookies.Prob('Bowl 1'))

Example #15
0
from thinkbayes import Pmf
'''
pmf1 =Pmf()
for x in [1,2,3,4,5,6]:
	pmf1.Set(x, 1/6.0)

#print pmf1.Normalize()
#print pmf1.Prob(1)
'''
pmf2 = Pmf()
pmf2.Set('Bowl1', 0.5)
pmf2.Set('Bowl2', 0.5)

pmf2.Mult('Bowl1', 0.75)
pmf2.Mult('Bowl2', 0.5)

pmf2.Normalize()
print pmf2.Prob('Bowl1')
print pmf2.Prob('Bowl2')
Example #16
0
# _*_ coding: utf-8 _*_

from thinkbayes import Pmf

pmf = Pmf()
pmf.Set('Bow1', 0.5)  #碗1的概率
pmf.Set('Bow2', 0.5)  #碗2的概率
pmf.Mult('Bow1', 0.75)  #碗1香草曲奇概率
pmf.Mult('Bow2', 0.5)  #碗2香草曲奇概率
pmf.Normalize()  #归一化
t = pmf.Prob('Bow1')  #后验概率
print(t)