"""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 Monty(Pmf): 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 Update(self, data): """Updates each hypothesis based on the data. data: any representation of the data """ for hypo in self.Values(): like = self.Likelihood(hypo, data) self.Mult(hypo, like) self.Normalize() def Likelihood(self, hypo, data): """Compute the likelihood of the data under the hypothesis. hypo: string name of the door where the prize is data: string name of the door Monty opened """ if hypo == data: return 0 elif hypo == 'A': return 0.5 else: return 1 def main(): hypos = 'ABC' pmf = Monty(hypos) data = 'B' pmf.Update(data) for hypo, prob in pmf.Items(): print hypo, prob if __name__ == '__main__': main()