# Interview Question: Fair and Unfair Coins and Bayes’ Theorem

Interview Question: Fair and Unfair Coins and Bayes’ TheoremDiogo RibeiroBlockedUnblockFollowFollowingMay 2Short post for today, based on the first probability question I was asked during an onsite interview.

The question went as follows:You are given a bag of 100 coins, with 99 fair ones flipping heads and tails with 0.

5probability each, and one unfair coin which flips heads with 1.

0 probability.

You pick a coin randomly and flip it 10 times, getting heads every single time.

What is the probability that you picked the unfair coin?The question can be answered quite easily using Bayes’ Theorem from probability theory.

The extended formula from the theorem is:Here we let A be the even of having picked the unfair coin, and B be the event of having flipped 10 heads in a row.

So we have P(A) = 0.

01P, P(B|A) = 1.

0,andPutting it all together we getwhich is approximately 0.

9118.

So there is a 91.

18% chance we picked the unfair coin.

Let’s test this out empirically in Python, using the Bernoulli process generator.

I ran the experiment 100,000 times and printed the ratio.

First the code for this experiment.

(Note that I cheat and use the gi_frame attribute, which in general, is not a good idea since it’s CPython specific.

I do it here because this is not production code and just an experiment.

)def pick_random_coin(fair_coin_probablity): if random.

random() < fair_coin_probablity: return bernoulli_process(0.

5) else: return bernoulli_process(1.

0)a = 0 # number of times we get 10 heads in a row AND coin was unfairb = 0 # total number of times we get 10 heads in a rowfor __ in range(100000): coin = pick_random_coin(0.

99) if True not in [coin.

next() for __ in range(10)]: b += 1 # check to see if the coin is unfair, or we # just got really lucky if coin.

gi_frame.

f_locals['p'] > 0.

6: a += 1print (float(a) / b)The output from my first run was 0.

9163, and the second run 0.

9070 which is convincing.

Of course, the true test would be to start from the basics and prove we did everything right.

The empirical test was more of a curiosity.

.