Exemple #1
0
 def __init__(self, hypos):
     """Initialize the distribution.
     hypos: sequence of hypotheses
     """
     Pmf.__init__(self)
     for hypo in hypos:
         self.Set(hypo, 1)
     self.Normalize()
 def __init__(self, hypos):
     """Initialize self.
     hypos: sequence of string bowl IDs
     """
     Pmf.__init__(self)
     for hypo in hypos:
         self.Set(hypo, 1)
     self.Normalize()
 def __init__(self, hypos):
     """Initialize the distribution.
     hypos: sequence of hypotheses
     """
     Pmf.__init__(self)
     for hypo in hypos:
         self.Set(hypo, 1)
     self.Normalize()
Exemple #4
0
    def __init__(self, hypos):
        """Initialize self.

        hypos: whether you play tennis or not 
        """
        Pmf.__init__(self)
        for hypo in hypos:
            self.Set(hypo, countdict[hypo])
        self.Normalize()
Exemple #5
0
    def __init__(self, hypos, alpha=1.0):
        """Initializes the hypotheses with a power law distribution.

        hypos: sequence of hypotheses
        """
        Pmf.__init__(self)
        for hypo in hypos:
            self.Set(hypo, hypo**(-alpha))
        self.Normalize()
Exemple #6
0
    def __init__(self, hypos):
        """Initialize self.

        hypos: sequence of string bowl IDs
        """
        Pmf.__init__(self)
        for hypo in hypos:
            self.Set(hypo, 1)
        self.Normalize()
Exemple #7
0
    def __init__(self, hypos):
        """Initialize self.

        hypos: whether you play tennis or not 
        """
        Pmf.__init__(self)
        for hypo in hypos:
            self.Set(hypo, countdict[hypo])
        self.Normalize()
Exemple #8
0
    def __init__(self, hypos):
        """UPDATE THIS DOCSTRING

        hypos: sequence of hypotheses representing possible company sizes
        """
        Pmf.__init__(self)
        for hypo in hypos:
            self.Set(hypo, int(max(hypos) / hypo) * hypo)
        self.Normalize()
Exemple #9
0
    def __init__(self, hypos, alpha=1.0):
        """Initializes hypotheses with a power law distribution.
			
			hypos: sequence of hypotheses
			alpha: parameter of the power law prior
			"""
        Pmf.__init__(self)
        for hypo in hypos:
            self.Set(hypo, hypo**(-alpha))
        self.Normalize()
def main():
    d6 = Die(6)
    d8 = Die(8)
    d12 = Die(12)
    d16 = Die(16)
    d20 = Die(20)

    mix = Pmf()
    for die in [d6, d8, d12, d16, d20]:
        for outcome, prob in die.Items():
            mix.Incr(outcome, prob)
    mix.Normalize()

    thinkplot.PrePlot(1)
    thinkplot.Pmf(mix)
    thinkplot.Save(root='dice_Mix_self1',
                   xlabel='sum of dice',
                   ylabel='Probability',
                   formats=['pdf'])
Exemple #11
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'])
Exemple #12
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')
Exemple #13
0
 def __init__(self, hypos):
     Pmf.__init__(self)
     for hypo_k, hypo_v in hypos.items():
         self.Set(hypo_k, 1)
     self.Normalize()
Exemple #14
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)
Exemple #15
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)
Exemple #16
0
from thinkbayes import Pmf
import Pmf
# pmf = Pmf()
#
#
#
# pmf.Set('Bowl 1', 0.5)
# pmf.Set('Bowl 2', 0.5)
# pmf.Incr(2, 0.2)
#
# pmf.Mult('Bowl 1', 0.75)
# pmf.Mult('Bowl 2', 0.5)
#
# pmf.Normalize()
# print (pmf.Prob('Bowl 1'))

hist = Pmf.MakeHistFromList([1, 2, 2, 3, 5])
print(hist.Freq(2))
print("end")

for val in sorted(hist.Values()):
    print(val, hist.Freq(val))
Exemple #17
0
 def __init__(self, hypos):
     Pmf.__init__(self)
     for hypo in hypos:
         self.Set(hypo, 1)
     self.Normalize()
 def __init__(self, hypos):
     Pmf.__init__(self)
     for hypo in hypos:
         self.Set(hypo, 1)
     self.Normalize()
Exemple #19
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))
Exemple #20
0
# body of new data, D.

# p(H | D) = [p(H) p(D | H)] / p(D)

### p(H) := is the probability of the hypothesis before we see the data,called the prior probability, or just 'prior'.
### p(H|D) := is what we want to compute, the probability of the hypothesis after we see the data, called the 'posterior'.
### p(D|H) := is the probability of the data under the hypothesis, called the 'likelihood'.
### p(D) := is the probability of the data under any hypothesis, called the 'normalizing constant'.

# Sometimes the 'prior' can be computed based on background information

# Here we will use the probability mass function on the cookie problem.

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

print('\n')
print('The Cookie Problem: ')


# The Cookie problem #
# In the following class a Cookie object is a Pmf that maps from hypotheses to their probabilities.
Exemple #21
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')


Exemple #22
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.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'))
Exemple #23
0
 def __init__(self, hypo, alpha=1.0):
     Pmf.__init__(self)
     for hypo in hypos:
         self.Set(hypo, hypo**(-alpha))
     self.Normalize()
	def __init__(self, bowls):
		Pmf.__init__(self)
		self.bowls = bowls
		for hypo in bowls.keys():
			self.Set(hypo, 1)
		self.Normalize()
Exemple #25
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()
Exemple #26
0
 def __init__(self, hypos, alpha = 1.0):
     Pmf.__init__(self)
     for hypo in hypos:
         self.Set(hypo, hypo**(-alpha))
     self.Normalize()
Exemple #27
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)
 def __init__(self, sides):
     Pmf.__init__(self)
     for side in xrange(1, sides + 1):
         self.Set(side, 1)
     self.Normalize()
Exemple #29
0
 def __init__(self):
     Pmf.__init__(self)
     self.Set('goodsinger', 0.05)
     self.Set('badsinger', 0.95)
     self.Normalize()
Exemple #30
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))
Exemple #31
0
    def __init__(self,hypos):
        Pmf.__init__(self)

        for hypo in hypos:
            self.Set(hypo,1)
Exemple #32
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'))

Exemple #33
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())
Exemple #34
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'))
Exemple #35
0
 def MakePmf(self,xs):
     pmf = Pmf()
     for x in xs:
         pmf.Set(x, self.Density(x))
     pmf.Normalize()
     return pmf
Exemple #36
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')))
Exemple #37
0
    def __init__(self, hypos):
        Pmf.__init__(self)

        for who, prob in hypos.iteritems():
            self.Set(who, prob)
        self.Normalize()
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'])
Exemple #39
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')
 def __init__(self, sides):
     Pmf.__init__(self)
     for x in xrange(1, sides+1):
         self.Set(x, 1)
     self.Normalize()