# Checking whether a coin is fair

Given a box that contains 90% fair coins and 10% loaded coins, (a loaded coin gives heads 90% of the time), what is the probability for a randomly drawn coin to give 5 heads in a row?

P(HHHHH | random coin from box of mixed coins) = ?


I used Bayes Theorem to calculate the probability I got .219 or 21.9% probability for the random coin to give 5 heads in a row!

But I am still doubting if my answer is correct !!!

Any help appreciated :)

• I like to use two nearly opposite, but still valid ways to get to the answer. Will you accept an answer that isn't symbolic? :) Oct 8, 2017 at 21:09
• @EngrStudent sure !!! if it satisfies my reasoning Oct 8, 2017 at 21:11
• Do you (a) draw one coin randomly and then flip it five times or (b) five times over, draw one coin at a time and flip it or (c) five times over, draw one coin at a time, flip it, and return it to the box?
– whuber
Oct 8, 2017 at 21:13
• @whuber a ... Oct 8, 2017 at 21:15
• It is simply 0.9*(0.9)$^5$ when you get a loaded coin and 0.1*(0.5)$^5$ when you get a fair coin. Since the event getting a fair coin is mutually exclusive from getting a loaded coin you simply add these probabilities. Oct 8, 2017 at 21:32

Here is the code I use:

# Given a box that contains 90% fair coins and 10% loaded coins,
#(a loaded coin gives heads 90% of the time), what is the probability
#for a randomly drawn coin to give 5 heads in a row?
# draw one coin randomly and then flip it five times

Number_of_repeats = 1000000  #a million samples

#store values
store_values = numeric(length = Number_of_repeats)

#draw a coin
#    90% chance fair, 10% chance unfair
#    fair means 50% binom, unfair means 90% binom
draw_coin = sample(x=c(0.5,0.9),
size = Number_of_repeats,
prob = c(0.9,0.1),
replace = TRUE)

for (i in 1:Number_of_repeats){

#flip it 5 times, count "heads"
flips = rbinom(n = 1, size = 5, prob = draw_coin[i])

if (flips==5){
store_values[i] = 1
}

}

print(summary(as.factor(store_values)))
print(mean(store_values))


The results it gives are:

> print(summary(as.factor(store_values)))
0      1
913149  86851

> print(mean(store_values))
[1] 0.086851


To me this says you should be getting in the neighborhood of 8.6%.

This runs surprisingly fast for the number of samples. Thank you, whuber, for suggestion.

The densest way, but one with slightly less clear intermediate step is this:

Number_of_repeats = 1000000

#draw a coin
#    90% chance fair, 10% chance unfair
#    fair means 50% binom, unfair means 90% binom
store_values = sample(x=c(0.5^5,0.9^5),
size = Number_of_repeats,
prob = c(0.9,0.1),
replace = TRUE)

print(summary(as.factor(store_values)))
print(mean(store_values))


It runs really (really!) fast.

So when I put in 5 million samples I get 8.709% and 8.706%, so it is one thousandths place that is moving.

I think other commenters were saying this:

(0.1*(0.9^5)) + (0.9*(0.5^5))


It evaluates to 0.087174, or 8.717%.

It is fun that this diverges from the 7.70x%. At 5 million samples we could be hitting round-off related effects. What fun!

• +1 What I like about this is that straightforward efforts to simplify the code will lead to a non-programming solution. For instance, do we really have to sample c(0.5,0.9) and only then flip the coin five times, or could we compute the chances of getting five heads and substitute those two values for 0.5 and 0.9?
– whuber
Oct 8, 2017 at 21:39
• +1 for explaining both programmatic wise and mathematical wise :) Oct 8, 2017 at 22:45