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'])
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'])
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'])
#!/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 MakePmf(self,xs): pmf = Pmf() for x in xs: pmf.Set(x, self.Density(x)) pmf.Normalize() return pmf
#!/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())
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))
""" 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'))
# ========================= # # 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'))
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')