# Think Bayes

This notebook presents example code and exercise solutions for Think Bayes.

Copyright 2018 Allen B. Downey

MIT License: https://opensource.org/licenses/MIT


```python
# Configure Jupyter so figures appear in the notebook
%matplotlib inline

# Configure Jupyter to display the assigned value after an assignment
%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'

# import classes from thinkbayes2
from thinkbayes2 import Hist, Pmf, Suite
```

**Exercise:** Let's consider [a more general version of the Monty Hall problem](https://en.wikipedia.org/wiki/Monty_Hall_problem#Other_host_behaviors) where Monty is more unpredictable.  As before, Monty never opens the door you chose (let's call it A) and never opens the door with the prize.  So if you choose the door with the prize, Monty has to decide which door to open.  Suppose he opens B with probability `p` and C with probability `1-p`.

1.  If you choose A and Monty opens B, what is the probability that the car is behind A, in terms of `p`?

2.  What if Monty opens C?

Hint: you might want to use SymPy to do the algebra for you. 


```python
from sympy import symbols
p = symbols('p')
```




    p




```python
# Solution

# Here's the solution if Monty opens B.

pmf = Pmf('ABC')
pmf['A'] *= p
pmf['B'] *= 0
pmf['C'] *= 1
pmf.Normalize()
pmf['A'].simplify()
```




    1.0*p/(p + 1)




```python
# Solution

# When p=0.5, the result is what we saw before

pmf['A'].evalf(subs={p:0.5})
```




    0.333333333333333




```python
# Solution

# When p=0.0, we know for sure that the prize is behind C

pmf['C'].evalf(subs={p:0.0})
```




    1.00000000000000




```python
# Solution

# And here's the solution if Monty opens C.

pmf = Pmf('ABC')
pmf['A'] *= 1-p
pmf['B'] *= 1
pmf['C'] *= 0
pmf.Normalize()
pmf['A'].simplify()
```




    0.333333333333333*(p - 1)/(0.333333333333333*p - 0.666666666666667)



ecac451cfa95fa909ebb15ab14137d411ce59a71
ipynb
Jupyter Notebook
examples/monty_soln.ipynb
vickiwickinger/ThinkBayes2
2259f1e83dba9a959b2cc84c7b83318b53b3ee24
MIT
2015-01-06T06:23:55.000Z
2022-03-31T21:06:21.000Z
examples/monty_soln.ipynb
vickiwickinger/ThinkBayes2
2259f1e83dba9a959b2cc84c7b83318b53b3ee24
MIT
2015-04-23T13:14:15.000Z
2022-01-04T12:55:59.000Z
examples/monty_soln.ipynb
vickiwickinger/ThinkBayes2
2259f1e83dba9a959b2cc84c7b83318b53b3ee24
MIT
2015-01-13T22:05:32.000Z
2022-03-30T09:19:53.000Z
Qwen/Qwen-72B
1. YES
2. YES