What is the expected number of times that you need to throw a biased coin to get the first tail?

This is an interesting question. Let’s assume the probability of getting a tail is p, how many times do we need to toss the coin to get the tail? Let’s solve it using a couple of different methods.

**Solution 1: Using the definition of expectation**

This is the hardest solution actually. Simply because with this method you will arrive at a geometric series that is not really easy to solve. Regardless let’s use the definition of expectation. By definition the expected value is as follows:

E[# tosses until a tail] = 1.(prob of getting a tail at one toss) + 2.(prob of getting a head, then a tail) + 3(prob of getting two heads then a tail) + … + n.(prob of getting n-1 heads then one tail)

This value can be written as

This can be simplified to the following

The sum would be equal to (you can either use Wolfram alpha or use this theorem) therefore

**Solution 2: Using recurrence**

There is a little trick that makes your life very easy. You might notice that the process at any point is independent of what has happened previously. Meaning that if you toss the coin and get a head then the expected number of tosses to get one tail is still E[X]. Therefore:

if we solve for E[X] we will have:

This way you can avoid going through solving the series of that we had to do in the previous solution.

**Solution 3: Using geometric random variables **

This is probably the easiest solution. The number of tosses until one tail follows a geometric distribution and the mean for geometric distribution is 1/p. (Also see page 40 of Ross’s Introduction to Probability Models)

**Solution 4: Using simulation **

This is easy to solve using simulation. The following is the R code for calculating this expected value.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# sims <- 1000000 p <- .5 E <- rep(NA, sims) for (i in 1:sims) { coin <- F count <- 0 while (!coin){ coin <- runif(1)>p count <- count + 1 } E[i] <- count } cat("Expected number of coin tosses = ", mean(E)) |

And the output will be

1 |
Expected number of coin tosses = 2.00475 |

If you prefer Python over R then the following is the most efficient implementation that I can come up with

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# # Simulating first tail expectation import random sims = 1000000 def tossUntilTail(p=0.5): count = 0 coin = False while not coin: coin = (random.random() > p ) count += 1 return count tossesUntilTail = map(tossUntilTail, [None]*sims) print "Expected number of coin tosses = ", float(sum(tossesUntilTail))/len(tossesUntilTail) |

Now can you solve this problem: “find the expected number of coin tosses to get two consecutive tails”