Solved – Expected number of tosses till first head comes up

bernoulli-distributionexpected valuegeometric-distributionprobabilityself-study

Suppose that a fair coin is tossed repeatedly until a head is obtained for the first time.

  • What is the expected number of tosses that will be required?
  • What is the expected number of tails that will be obtained before the first head is obtained?

Best Answer

This can be answered using the geometric distribution as follows:

The number of failures k - 1 before the first success (heads) with a probability of success p ("heads") is given by:

$$p(X=k)=(1−p)^{k−1}p$$

with k being the total number of tosses including the first 'heads' that terminates the experiment.

And the expected value of X for a given p is $1/p=2$.

The derivation of the expected value can be found here. The last steps left implicit should be as follows:

$\frac{d}{dr} \frac{1}{1-r} = \frac{1}{(1-r)^2}$ to be plugged into the expression:

$E(X) = \frac{p}{1-p} \sum\limits_{x = 1}^{\infty}x\ r^x = \frac{p}{1-p}\ r\ (\frac{d}{dr} \frac{1}{1-r})= \frac{p}{1-p}\ r\ \frac{1}{(1-r)^2}$. With $r = 1 - p$, it simplifies to

$E(X) = \frac{1}{p}$, justifying its use above.]

Alternatively, we could use the negative binomial distribution interpreted as the number of failures before the first success. The probability mass function is given as the p(number of failures, n, before attaining r successes | given a certain probability, p, of success in each Bernoulli trial):

$$p(n;r,p) ={n+r-1\choose r-1} p^r (1-p)^n$$

The expectation for number of trials, n + r is given by the general formula:

$$\frac{r}{(1-p)}$$

Given our known parameters: r = 1 and p = 0.5,

$$E(n + r; 1,0.5) =\frac{r}{1-p} = \frac{1}{1-0.5} = 2$$

Hence we can expect to make two tosses before getting the first head with the the expected number of tails being $E(n+r) - r = 1$.

We can run a Monte Carlo simulation to prove it:

   set.seed(1)

p <- 1/2

reps <- 10000                         # Total number of simulations.

tosses_to_HEAD <- 0                   # Setting up an empty vector to add output to.

for (i in 1:reps) {
  head <- 0                           # Set the variable 'head' to 0 with every loop.
  counter <- 0                        # Same forlocal variable 'counter'.
  while (head == 0) {
    head <- head + rbinom(1, 1, p)    # Toss a coin and add to 'head'
    counter <- counter + 1            # Add 1 to 'counter'
  }
  tosses_to_HEAD[i] <- counter        # Append number in counter after getting heads.
}

mean(tosses_to_HEAD)
[1] 2.0097
Related Question