"""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 class Cookie(Pmf): 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(hypo, data) self.Mult(hypo, like) self.Normalize() mixes = { 'Bowl 1':dict(vanilla=0.75, chocolate=0.25), 'Bowl 2':dict(vanilla=0.5, chocolate=0.5), } def Likelihood(self, hypo, data): mix = self.mixes[hypo] like = mix[data] return like def main(): hypos = ['Bowl 1', 'Bowl 2'] pmf = Cookie(hypos) pmf.Update('vanilla') for hypo, prob in pmf.Items(): print hypo, prob if __name__ == '__main__': main()