## The Monty Hall problem

To solve the Monty Hall problem, I’ll define a new class:

class Monty(Pmf): def __init__(self, hypos): Pmf.__init__(self) for hypo in hypos: self.Set(hypo, 1) self.Normalize()

So far `Monty`

and
`Cookie`

are exactly the
same. And the code that creates the Pmf is the same, too, except for the
names of the hypotheses:

hypos = 'ABC' pmf = Monty(hypos)

Calling `Update`

is
pretty much the same:

data = 'B' pmf.Update(data)

And the implementation of `Update`

is exactly the same:

def Update(self, data): for hypo in self.Values(): like = self.Likelihood(data, hypo) self.Mult(hypo, like) self.Normalize()

The only part that requires some work is `Likelihood`

:

def Likelihood(self, data, hypo): if hypo == data: return 0 elif hypo == 'A': return 0.5 else: return 1

Finally, printing the results is the same:

for hypo, prob in pmf.Items(): print hypo, prob

And the answer is

A 0.333333333333 B 0.0 C 0.666666666667

In this example, writing `Likelihood`

is a little complicated, but the
framework of the Bayesian update is simple. The code in this section is
available from http://thinkbayes.com/monty.py.
For more information see Working with the code.

Get *Think Bayes* now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.