Is This a Fair Coin? Bayesian Coin Flip Simulation

Amir Harjo
6 min readJun 4, 2023

Bayesian might be straight forward; however, it is not as easy as it sound. The objective of Bayesian approach is to update our believe when we are shown with few facts.

For example in the case of coin flip. Let’s say, we have a coin with Head(H) and Tail(T) on both of its surface. We flip the coin five times and we get HHTTH. The most common questions when we faced this challenge and want to solve it using Bayesian is, what is the probability of getting head? And the solution for this trivia questions is answering this questions P(H/X), the probability of getting head if we have condition X ~ HHTHH. This is a solved questions and you can find the answer scattered in the internet.

Now, consider a scenario. Two guys have too many spare time and do not know what to do. So, they betting their little money over a trivia challenge, the coin flip. One of the man sloshing his hand to his pocket, and from his pocket, he showed a coin that they want to use. Lets call the first man as “Strategic Guy”. The second guy make a rookie mistake, he didn’t check the the coin and they start playing. Lets call the second guy as “Rookie Guy”.

Mind you, that they are not best friend, so they do not trust each other that much. Strategic Guy championing that the coin will mostly showed up head (H). Rookie Guy, have no other options but tail(T).

After five tosses, the coin come out as HHHHH. Now, if you are the Rookie Guy, how do you feel? Do you feel cheated? What is the probability that Strategic Guy is cheating? The answer of this questions is not to estimate the probability of getting Head (H). This is totally different question. Updating the posterior using Bayesian is a bit tricky. Until I found a blog post written by Tim Erickson and I was enlighten.

Why do I write this blog post? Well, even though the blog answered my curiosity, I think there are still some stone left unturned. That is where my contribution and this blog will served as my note for future reference.

I will explain two scenarios. Scenario one is my understanding of the blog plus few variations of prior information. Scenario two is solution when the problem is deviate far from the blog post.

Scenario 1 — The Coin is 100% Unfair

The coin is 100% unfair, so we could expect that after the toss, it will always come out as H. Suppose that Rookie Guy still have heart and his morale compass said that he will only give 50% of doubt that Strategic Guy is cheating.

First Toss

They toss the coin once …. and H is the output. How does Rookie Guy update his believe. Does he still 50% believe that Strategic Guy is fair or his trust diminish? The questions is, how to update his believe? The blog post explain well using graph.

Source: Inspired by Tim Erickson

We start with the same measure of probability of fair coin and not fair. Because Rookie Guy prior believe is that the coin is 50% fair, so P(F) = 0.5.

After the first toss, Rookie Guy update his prior believe using the Bayesian approach:

Bayesian Formulation

The result is (0.5)*(0.5)/0.75 = 1/3. Rookie Guy now believe 33% that Strategic Guy is not cheating. This is become the new prior.

Second Toss — Method 1

The game continue. The toss the coin second time and as you can expect, H come out as the winner. Rookie Guy shocked, but he still want to update his believe first. Here is the illustration using graph.

Source: Inspired by Tim Erickson

How much does Rookie Guy believe that the coin is fair? P(F/H) = (0.5*1/3)/(5/6) = 1/5.

Second Toss — Method 2

Instead of updating the prior, we could also calculate the believe in the second toss using initial believe, but now the conditional statement is HH. So, we would like to calculate P(F/HH), the probability that this is fair coin if the output is HH. And it can easily explained using graph.

Source: Inspired by Tim Erickson

And update the prior

Bayesian Formulation

We get P(F/HH) = 0.25*0.5/(5/8) = 1/5.

And continue …

The third flip will come out as H, as for the fourth, fifth and to infinite flip.

My questions to you will be, how long does the Rookie Guy believe that Strategic Guy is 99% cheating?

Scenario 2 — The Coin is X% Unfair

Above scenario seems easy because we use very trivial case where the coin is 100% fake. Now, what if Strategic Guy is cheating but he want to do it very subtle? So, instead of 100% always H, the coin will come out 75% H?

To explain this, I would like to simulate three coin flip where the output is HTH and calculate the posterior after each toss with initial Rookie Guy prior at 50%. The calculation is a bit tricky for second and third toss and graph will not work well.

First Toss

Source: Personal Documentation

The prior is then update to P(F/H) = (0.5*0.5)/(5/8) = 2/5 or 40%.

Second Toss

The output for the second toss is HT. Creating graph to illustrate this scenario is a bit tricky since the output will not 100% HH. To aid our calculation, we will use Binomial distribution because this scenario exactly follow the Binomial process.

Source: Personal Documentation

And using the formulation we can calculate the probability in questions mark:

And calculate P(H=1,T=1) = P(H=1,T=1/not F) * P(not F) + P(H=1,T=1/F)*P(F) = 0.1875 + 0.25 = 0.4375.

Final calculation, P(F/H=1,T=1) = 0.5*0.5/0.4375 = 0.57.

Great news! Strategic Guy can dodge the suspicion from Rookie Guy. Why? Because Rookie Guy in the beginning believe that 50% the coin is fair. After first toss, his believe is drop to 40%. Now, his believe is going up again to 57%.

Strategic Guy is smiling. In the long run, he will eventually win. On the other hand, Rookie Guy suspicion is eroded. Today will be a payday for Strategic Guy.

Third Toss

How much Rookie Guy believe will change after third toss? When the output is HTH? Here is the calculation Binomial process.

P(F/H=2,T=1)=0.375*0.5/0.3975 = 0.47. Rookie Guy drop his believe that Strategic Guy is honest guy from 57% to 47%.

Strategic Guy still think that it is not bad, because Rookie Guy believe is till hover close to 50%. Strategic Guy continue smiling. In the long run, he will eventually win, but he need to think strategically before Rookie Guy realize that Strategic Guy is cheating. “Maybe…” Strategic Guy thought,” I can drop off from the game when Rookie Guy believe drop to less 35%”.

Today is payday for Strategic Guy.

BECOME a WRITER at MLearning.ai // AI Factory // Super Cheap AI.

--

--