Example #1
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 #2
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 #3
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 #4
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 #5
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 #6
0
#===========#

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'))


# more general tooling for the cookie problem:
class Cookie(Pmf):
    def __init__(self, hypos):
        Pmf.__init__(self)
        for hypo in hypos:
            self.Set(hypo, 1)
        self.Normalize()
Example #7
0
'''
学习笔记:
贝叶斯
事情A发生的情况下,推测可能导致事件A发生的各条件的概率。

单次贝叶斯的计算逻辑:
   各条件下,事件A发生的概率,进行归一化后,则为A发生的情况下,各条件可能导致的概率。
'''

from thinkbayes import Pmf

pmf = Pmf()
#曲奇饼问题
#1.设置先验概率
pmf.Set('Bowl1', 0.5)
pmf.Set('Bowl2', 0.5)
print('***************')
print('Bowl1:' + str(pmf.Prob('Bowl1')))
print('Bowl2:' + str(pmf.Prob('Bowl2')))
#2.设置似然度
pmf.Mult('Bowl1', 0.75)  #香菜来自1碗的概率。碗1中香草曲奇饼的出现概率75%;选中1碗且是香草曲奇饼的概率0.375。
pmf.Mult('Bowl2', 0.5)  #香草饼干来自2碗的概率。碗2中香草曲奇饼的出现概率25%;选中2碗且是香草曲奇饼的概率0.25。
print('***************')
print('Bowl1:' + str(pmf.Prob('Bowl1')))
print('Bowl2:' + str(pmf.Prob('Bowl2')))
#3.初始化
pmf.Normalize()

print('***************')
print('Bowl1:' + str(pmf.Prob('Bowl1')))  #拿出来了一个香草曲奇饼,来自碗1的概率0.375/0.625
print('Bowl2:' + str(pmf.Prob('Bowl2')))
Example #8
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)