TITLE: Expected value of the number of flips until the first head
QUESTION [24 upvotes]: Suppose we flip a coin until we see a head. What is the expected value of the number of flips we will take?

I am pretty new to expected value, so I tried to evaluate it by multiplying the probability of each scenario with the number of flips it took to get there (like taking the arithmetic mean). This didn't work though, because of the infinite possibilities. I'm really confused, and if someone were to provide an answer, I would really appreciate it if they could go into detail.

REPLY [9 votes]: Let $X$ be the number of flips we make before seeing heads. Let $p$ be the probability of getting heads on any one flip. And note three things that, individually, aren't too hard to see:

Coin flipping is a memoryless process.
We must flip the coin at least once in order to get heads (or tails, or anything).
The probability of not getting heads on that first flip is $1-p$.

Using these three ideas, we can model $E[X]$, the expected number of flips we perform before seeing heads, as:
$E[X] = 1 + (1-p)E[X]$
That is, we flip the coin once (hence the $1$), and there is a ($1-p$) chance that we get tails and have to continue flipping the coin (hence the $+ (1-p)E[X]$). Because coin flipping is memoryless, the expected value of $X$ after flipping a tails is the same as before flipping that tails (that's why the $E[X]$ on the RHS of the equation is not conditioned).
Now it turns out that modeling $E[X]$ in this way is useful because we can easily solve for $E[X]$ and answer our question:
$E[X] = 1 + (1-p)E[X]$
$E[X] - (1-p)E[X] = 1$
$E[X] + (-1+p)E[X] = 1$
$E[X] - E[X] + pE[X] = 1$
$pE[X] = 1$
$E[X] = 1 / p$
Since $p = 1/2$, we get $E[X] = 2$.
I learned of this clever approach here: http://online.stanford.edu/course/algorithms-design-and-analysis-part-1