Short 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 probability each, and one unfair coin which flips heads with 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 be the even of having picked the unfair coin, and be the event of having flipped 10 heads in a row. So we have , , , and . Putting it all together we get
which is approximately . 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 from the the post on iterators and generators. 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 unfair b = 0 # total number of times we get 10 heads in a row for __ in xrange(100000): coin = pick_random_coin(0.99) if True not in [coin.next() for __ in xrange(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 += 1 print float(a) / b
The output from my first run was , and the second run 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 a curiosity.