1.) Suppose you have two coins in a box. One is a normal coin with heads on one side and tails on the other, and one is a trick coin with heads on both sides. You choose a coin at random and see that one of the sides is heads. What is the probability that you chose the trick coin?
(From Think Bayes By Allen Downey )
from fractions import Fraction
import pandas as pd
coinTable = pd.DataFrame(index=['fair', 'trick'])
coinTable["prior"] = Fraction(1, 2) #prior probability is that you have 1/2 chance of choosing the trick coin
coinTable
prior | |
---|---|
fair | 1/2 |
trick | 1/2 |
For our likelihoods we have to consider probability of our data (seeing heads) given each hypothesis:
coinTable["likelihood"] = Fraction(1, 2), 1
coinTable
prior | likelihood | |
---|---|---|
fair | 1/2 | 1/2 |
trick | 1/2 | 1 |
def update(table):
"""Compute the posterior probabilities."""
table['unnorm'] = table['prior'] * table['likelihood']
prob_data = table['unnorm'].sum()
table['posterior'] = table['unnorm'] / prob_data
return prob_data
update(coinTable)
coinTable
prior | likelihood | unnorm | posterior | |
---|---|---|---|---|
fair | 1/2 | 1/2 | 1/4 | 1/3 |
trick | 1/2 | 1 | 1/2 | 2/3 |
The probability that we chose the trick coin is 2/3
There are many variations of the Monty Hall problem. For example, suppose Monty always chooses Door 2 if he can, and only chooses Door 3 if he has to (because the car is behind Door 2). If you choose Door 1 and Monty opens Door 2, what is the probability the car is behind Door 3? If you choose Door 1 and Monty opens Door 3, what is the probability the car is behind Door 2?
(From Think Bayes By Allen Downey)
Our prior is the same
MHtable = pd.DataFrame(index=['Door 1', 'Door 2', 'Door 3'])
MHtable['prior'] = Fraction(1, 3)
MHtable
prior | |
---|---|
Door 1 | 1/3 |
Door 2 | 1/3 |
Door 3 | 1/3 |
But now our likelihood is different. We go through the possible hypothesis:
MHtable["likelihood"] = 1,0,1
update(MHtable)
Fraction(2, 3)
MHtable
prior | likelihood | unnorm | posterior | |
---|---|---|---|---|
Door 1 | 1/3 | 1 | 1/3 | 1/2 |
Door 2 | 1/3 | 0 | 0 | 0 |
Door 3 | 1/3 | 1 | 1/3 | 1/2 |
So in this case, we really do have a 50/50 chance
If the car is behind door 1, then monty would only choose door 3 if he had to (the car is behind door 2), so this has 0 probability
If the car is behind door 2, then monty would choose door 3 with probability 1
If the car is behind door 3, then monty would not open door 3, so 0 probability
MHtable = pd.DataFrame(index=['Door 1', 'Door 2', 'Door 3'])
MHtable['prior'] = Fraction(1, 3)
MHtable['likelihood'] = 0,1,0
update(MHtable)
Fraction(1, 3)
MHtable
prior | likelihood | unnorm | posterior | |
---|---|---|---|---|
Door 1 | 1/3 | 0 | 0 | 0 |
Door 2 | 1/3 | 1 | 1/3 | 1 |
Door 3 | 1/3 | 0 | 0 | 0 |
In this case, we are 100 percent certain that car is behind door 2
Simulation for decision analysis: An experiment is performed to measure the efficacy of a television advertising program. The result is an estimate that each minute spent on a national advertising program will increase sales by € 500 000 (you can assume no variable costs for this product), and this estimate has a standard error of € 200 000. Assume the uncertainty in the treatment effect can be approximated by a normal distribution. Suppose ads cost € 300 000 per minute.
What is the expected net gain for purchasing 20 minutes of ads?
What is the probability that the net gain is negative?
import numpy as np
nsim = 10000
salesPerMin = np.random.normal(500000,200000, nsim)
netGain = salesPerMin - 300000
netGain.mean()
201621.25464952042
mean netGain of ~ 200 000
netGain[netGain<0].size/nsim
0.1578
There is an approximately 15% chance that you get a net loss