📚 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 Monty(Pmf):11"""Map from string location of car to probability"""1213def __init__(self, hypos):14"""Initialize the distribution.1516hypos: sequence of hypotheses17"""18Pmf.__init__(self)19for hypo in hypos:20self.Set(hypo, 1)21self.Normalize()2223def Update(self, data):24"""Updates each hypothesis based on the data.2526data: any representation of the data27"""28for hypo in self.Values():29like = self.Likelihood(data, hypo)30self.Mult(hypo, like)31self.Normalize()3233def Likelihood(self, data, hypo):34"""Compute the likelihood of the data under the hypothesis.3536hypo: string name of the door where the prize is37data: string name of the door Monty opened38"""39if hypo == data:40return 041elif hypo == 'A':42return 0.543else:44return 1454647def main():48hypos = 'ABC'49pmf = Monty(hypos)5051data = 'B'52pmf.Update(data)5354for hypo, prob in sorted(pmf.Items()):55print hypo, prob565758if __name__ == '__main__':59main()606162