In: Statistics and Probability
Use python simulations to answer the following questions. You should be able to make minor modifications to previous simulations to estimate these probabilities:
1) Suppose the pocket contains one fair coin and one two-headed coin. Find the approximate probability that the coin is fair given that it came up heads on the first flip.
2) Suppose the pocket contains one fair coin and one two-headed coin. Find the approximate probability that the coin is fair given that it came up heads on the first two flips.
3) Repeat part 1 if her pocket contains two fair coins and one two-headed coin.
4) Repeat part 2 if her pocket contains two fair coins and one two-headed coin.
1) Let F be the even that the coin picked is fair, 2H be the event that the coin picked is 2 headed.
Assuming that the coins are picked at random, the probability that the coin picked is fair is P(F) =0.5 and the probability that coin picked is 2 headed is P(2H) = 0.50
Let H be the event that the coin picked came heads up on the first flip.
If the coin is fair, the probability that it comes up heads is 0.50. If it is a 2 headed coin the probability that it comes up heads is 1 (always it comes up heads).
The conditional probability that heads in the first flip given that it is a fair coin is
P(H|F) = 0.50
The conditional probability that heads in the first flip given that it is a 2H coin is
P(H|2H) = 1
The probability that the coin is fair given that it came up heads on the first flip is
First these
Python code with comments to simulate this
#get this output
this is close to the theoretical probability 0.3333
2) Let F be the even that the coin picked is fair, 2H be the event that the coin picked is 2 headed.
Assuming that the coins are picked at random, the probability that the coin picked is fair is P(F) =0.5 and the probability that coin picked is 2 headed is P(2H) = 0.50
Let HH be the event that the coin picked came heads up on the first 2 flips.
If the coin is fair, the probability that it comes up heads is 0.50. If it is a 2 headed coin the probability that it comes up heads is 1 (always it comes up heads).
The conditional probability that heads in the first 2 flips given that it is a fair coin is
P(HH|F) = 0.50*0.50=0.25
The conditional probability that heads in the first 2 flips given that it is a 2H coin is
P(HH|2H) = 1*1=1
The probability that the coin is fair given that it came up heads on the first 2 flips is
Python code is below
get this output
this is close to the theoretical probability of 0.20
3) Let F be the even that the coin picked is fair, 2H be the event that the coin picked is 2 headed.
Assuming that the coins are picked at random, the probability that the coin picked is fair is P(F) =2/3 and the probability that coin picked is 2 headed is P(2H) = 1/3
Let H be the event that the coin picked came heads up on the first flip.
If the coin is fair, the probability that it comes up heads is 0.50. If it is a 2 headed coin the probability that it comes up heads is 1 (always it comes up heads).
The conditional probability that heads in the first flip given that it is a fair coin is
P(H|F) = 0.50
The conditional probability that heads in the first flip given that it is a 2H coin is
P(H|2H) = 1
The probability that the coin is fair given that it came up heads on the first flip is
First these
Python code is below
get this output
this is close enough to the theoretical probability of 0.50
4) Let F be the even that the coin picked is fair, 2H be the event that the coin picked is 2 headed.
Assuming that the coins are picked at random, the probability that the coin picked is fair is P(F) =2/3 and the probability that coin picked is 2 headed is P(2H) = 1/3
Let HH be the event that the coin picked came heads up on the first 2 flips.
If the coin is fair, the probability that it comes up heads is 0.50. If it is a 2 headed coin the probability that it comes up heads is 1 (always it comes up heads).
The conditional probability that heads in the first 2 flips given that it is a fair coin is
P(HH|F) = 0.50*0.50=0.25
The conditional probability that heads in the first 2 flips given that it is a 2H coin is
P(HH|2H) = 1*1=1
The probability that the coin is fair given that it came up heads on the first 2 flips is
Python code is below
get this output
This is close enough to the theoretical probability of 0.3333
-------------------------
The code in text format (the tabs may not be presents)
import numpy as np
#define a function to pick fair coin with probability p
def pickCoin(p):
#get a uniform random number in the interval (0,1)
u=np.random.random()
# pick fair coin if u<0.5 else 2 headed coin
if u<p:
return('F')
else:
return('2H')
#function to toss the coin n times and observe the result
def tossCoin(Type,n):
#set the probability of getting a head
if Type=='F':
p=0.5
else:
p=1
#variable to hold the result
r=''
#toss the coin n times
for i in range(n):
#get a uniform random number in the interval (0,1)
u=np.random.random()
# heads if u<p else tails
if u<p:
r=r+'H'
else:
r=r+'T'
return(r)
#part 1) the approximate probability that the coin is fair given
that it came up heads on the first flip.
# set the seed for the random number generator
np.random.seed(123)
# set the number of simulations
N=10000
#initialize a variable to hold the toss
toss=[]
#simulate for N runs
for i in range(N):
#choose the coin, with probability of choosing a fair
coin=0.5
coin=pickCoin(0.5)
#toss it one time
r=tossCoin(coin,1)
#if it is heads
if r=='H':
#then add 1 to toss if the coin is Fair
if coin=='F':
toss.append(1)
else: #else add a 0
toss.append(0)
#Calculate the conditional probability
prob=sum(toss)/len(toss)
print("probability that the coin is fair given that it came up
heads on the first flip is %.4f "%prob)
#part 2) the approximate probability that the coin is fair given
that it came up heads on the first two flips
# set the seed for the random number generator
np.random.seed(123)
# set the number of simulations
N=10000
#initialize a variable to hold the toss
toss=[]
#simulate for N runs
for i in range(N):
#choose the coin, with probability of choosing a fair
coin=0.5
coin=pickCoin(0.5)
#toss it one time
r=tossCoin(coin,2)
#if it is 2 heads
if r=='HH':
#then add 1 to toss if the coin is Fair
if coin=='F':
toss.append(1)
else: #else add a 0
toss.append(0)
#Calculate the conditional probability
prob=sum(toss)/len(toss)
print("probability that the coin is fair given that it came up
heads on the first two flips is %.4f "%prob)
#part 3) Repeat part 1 if her pocket contains two fair coins and
one two-headed coin.
# set the seed for the random number generator
np.random.seed(123)
# set the number of simulations
N=10000
#initialize a variable to hold the toss
toss=[]
#simulate for N runs
for i in range(N):
#choose the coin, with probability of choosing a fair
coin=2/3
coin=pickCoin(2/3)
#toss it one time
r=tossCoin(coin,1)
#if it is heads
if r=='H':
#then add 1 to toss if the coin is Fair
if coin=='F':
toss.append(1)
else: #else add a 0
toss.append(0)
#Calculate the conditional probability
prob=sum(toss)/len(toss)
print("probability that the coin is fair given that it came up
heads on the first flip is %.4f "%prob)
#part 4) Repeat part 2 if her pocket contains two fair coins and
one two-headed coin.
# set the seed for the random number generator
np.random.seed(123)
# set the number of simulations
N=10000
#initialize a variable to hold the toss
toss=[]
#simulate for N runs
for i in range(N):
#choose the coin, with probability of choosing a fair
coin=2/3
coin=pickCoin(2/3)
#toss it one time
r=tossCoin(coin,2)
#if it is 2 heads
if r=='HH':
#then add 1 to toss if the coin is Fair
if coin=='F':
toss.append(1)
else: #else add a 0
toss.append(0)
#Calculate the conditional probability
prob=sum(toss)/len(toss)
print("probability that the coin is fair given that it came up
heads on the first two flips is %.4f "%prob)