10 in a row.

A mandatory coin flipping example.

Posted by Magnus Harrysson on April 19, 2020

This blog post is about estimation of probability so it is time for a "mandatory" coin flipping experiment :-)

I first saw this experiment some years ago at the swedish children television show called "Hjärnkontoret" and what we like to get an answer to is basically:

When flipping a coin, what is the probability of getting 10 heads or tails in a row??

Sounds like a fun TV show right! The program host "Beppe" could of course use pen and paper to draw "trees" like the one below

but kids, even back then, would demand a little more action. So, let's make "Beppe" flip a coin many times, flip 10 heads or tails i a row and get it all on tape. Now we are talking good TV! Unfortunately, I could not find the clip from this TV show but here is clip from youtube showing a similar thing (1h and 32min, enjoy!!)

Going back to the question about the probability of getting 10 heads or tails in a row. If we assume that the coin is fair i.e. there is a 50% chance of getting either heads or tails in each coin flip then the probability of getting 10 equal outcomes in a row is 0.5^10 = 1/1024. To be honest, this does not mean that much to me more than it is a small number... A much more interesting (and more "engineering") question would be:

How long time should we book the camera team that is successfully going to film 10 heads or tails in a row?

This is not that straight forward to calculate and in this blog post (and in many of the following blog posts) we are going to use a computational approach since this is, at least to me, much more easy to understand. So let's get to it

Python to the rescue

Ok, let us start with importing the different packages that we are going to use

The numpy and pandas package is to get access to some handy functions like random number generators, matplotlib is for plotting and the seaborn stuff is just imported to get nicer looking plots.

The thing we would like to do is quite simple, it is basically the following steps:

  • Generate a lot of coin flips where the number 1 means heads and the number 0 means tails.
  • Find the first occasion where 10 heads or tails in a row is found...
that is it... let's write that in code

Let's just go through the code row by row. The first row generates 10 000 flips where 1 represents heads and 0 is tails. The second row just store the same data as a pandas series so we can use the very handy function Rolling found on row three. The rolling function calculates stuff within a window with a certain size, in this case we are interested in the sum within a 10 flips long window. The fourth row is where the "magic" happens. We are looking for ten 0 or ten 1 in a row so the sum within the windows should be 0 or 10. The last row just prints the first time we found 10 heads/tails in a row.

When I ran this code the output was 862 i.e. it took 862 flips to get 10 heads or tails in a row. As you probably have figured out, since the generation of coin flips is a random process it will (most probably) give a different answer the next time you run it... let's try that

OK indeed a different answer. Now let's run the code above a lot of times, then I mean a LOT of times, like 100 000 times and stored the number of flips it took to have 10 heads or tails in a row. The reason for this is perhaps not obvious but all these samples will later be used to evaluate probabilities (we will come to this very soon).

Alright, now we have a vector with 100 000 values representing how long it took to flip 10 heads or tails in a row, let's try to visualize the results. It is not obvious how to do this but here is a plot that would be interesting to have. I think of a number of coin flips, lets say 1300, and I would like to know the probability of 10 heads or tails have happened within these coin flips. The probability might sounds difficult to evaluate but here we will use all the 100 000 samples we have stored. One way to look at probability is "the long-run expected frequency of occurrence" i.e. if we do an experiment a lot of times, we can calculate the probability as the fraction of "how often something happens" in relation to "the total number of tests". Let's write that in code!

Now, let's loop through a number of different number of coin flips and plot the result

and here is the plot!

Nice!! Now, going back to the question about how long we should book the camera team that is going to film all the coin flips until we get 10 heads or tails in a row. According to the plot above we can say that if you have made 3000 flips there is a probability of approximately 95% that it will include 10 heads or tails in a row. You can aslo see that if we would like to be really really sure (say 99% probability)that we will have 10 heads or tails in a row the number of coin flips increases dramatically.

We are almost done but just one more thing. We assumed that the coin was fair i.e. there is a 50% chance of heads or tails but what if the coin is not fair? Let's do the same thing but with a probability of getting a heads is equal to 45%! This is very easy to do, just change the probability in the binomial distribution from 0.5 to 0.45! Let's do this and make a new plot (why don't just throw in a probability of heads equal 0.4 as well)

There you have it! The black dashed line represents a 95% probability and you can see how the number of flips needed to reach this level is reduced if the coin is not completely fair.

Time to conclude

Let's make some concluding remarks. If we are to book a camera team that is going to film this I would suggest to make time for the person flipping the coin to have approximately 3000 flips, then there is a 95% probability that you will get the 10 heads/tails in a row.

Ok, that is it for now! Hope that you found this useful and interesting. Personaly I think it is realy cool that you can evaluate probabilities for some quite difficult situations by just using random samples from a distribution. We will come back to this several times in the future.

As always, do not hesitate to contact us at info@gemello.se