Solution: Bayesian Analysis¶

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 )

1.¶

In [ ]:
from fractions import Fraction
import pandas as pd
In [ ]:
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
Out[ ]:
prior
fair 1/2
trick 1/2

For our likelihoods we have to consider probability of our data (seeing heads) given each hypothesis:

  • If we chose our fair coin, then the probability of getting heads would be 1/2
  • if we chose our trick coin, then the probability of getting heads would be 1
In [ ]:
coinTable["likelihood"] = Fraction(1, 2), 1
coinTable
Out[ ]:
prior likelihood
fair 1/2 1/2
trick 1/2 1
In [ ]:
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
In [ ]:
update(coinTable)
coinTable
Out[ ]:
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

2¶

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)

Solution¶

Our prior is the same

In [ ]:
MHtable = pd.DataFrame(index=['Door 1', 'Door 2', 'Door 3'])
MHtable['prior'] = Fraction(1, 3)
MHtable
Out[ ]:
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:

First scenario: You choose door 1, monty opens door 2¶

  • If the car is behind door 1, then monty chooses door 2 (hecause he always does if he can), then probability of monty choosing door 2 is 1
  • if the car is behind door 2, then monty would not open door 2, thus the probability of him choosing door 2 is 0
  • if the car is behind door 3, then monty would open door 2, so again the probability is 1
In [ ]:
MHtable["likelihood"] = 1,0,1
In [ ]:
update(MHtable)
Out[ ]:
Fraction(2, 3)
In [ ]:
MHtable
Out[ ]:
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

Second scenario: You choose door 1, monty opens door 3¶

  • 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

In [ ]:
MHtable = pd.DataFrame(index=['Door 1', 'Door 2', 'Door 3'])
MHtable['prior'] = Fraction(1, 3)
MHtable['likelihood'] = 0,1,0
update(MHtable)
Out[ ]:
Fraction(1, 3)
In [ ]:
MHtable
Out[ ]:
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

3 Decision Analysis: 9.8 (Regression and Other Stories Exercise 9.8, p.129)¶

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?

In [ ]:
import numpy as np
In [ ]:
nsim = 10000
salesPerMin = np.random.normal(500000,200000, nsim)

netGain = salesPerMin - 300000 
In [ ]:
netGain.mean()
Out[ ]:
201621.25464952042

mean netGain of ~ 200 000

In [ ]:
netGain[netGain<0].size/nsim
Out[ ]:
0.1578

There is an approximately 15% chance that you get a net loss

In [ ]: