📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""This file contains code for use with "Think Bayes",1by Allen B. Downey, available from greenteapress.com23Copyright 2012 Allen B. Downey4License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html5"""67from thinkbayes import Pmf8910class Cookie(Pmf):11"""A map from string bowl ID to probablity."""1213def __init__(self, hypos):14"""Initialize self.1516hypos: sequence of string bowl IDs17"""18Pmf.__init__(self)19for hypo in hypos:20self.Set(hypo, 1)21self.Normalize()2223def Update(self, data):24"""Updates the PMF with new data.2526data: string cookie type27"""28for hypo in self.Values():29like = self.Likelihood(data, hypo)30self.Mult(hypo, like)31self.Normalize()3233mixes = {34'Bowl 1':dict(vanilla=0.75, chocolate=0.25),35'Bowl 2':dict(vanilla=0.5, chocolate=0.5),36}3738def Likelihood(self, data, hypo):39"""The likelihood of the data under the hypothesis.4041data: string cookie type42hypo: string bowl ID43"""44mix = self.mixes[hypo]45like = mix[data]46return like474849def main():50hypos = ['Bowl 1', 'Bowl 2']5152pmf = Cookie(hypos)5354pmf.Update('vanilla')5556for hypo, prob in pmf.Items():57print hypo, prob585960if __name__ == '__main__':61main()626364