# Probability of X heads before Y consecutive tails in N biased coin tosses

I have another coin toss question:

Assume I am tossing a biased coin n times with probability p of coming up heads. What is the probability that x heads come up, before y consecutive tails?

A code example would be preferable.

-
Is this a homework question? If so, you should tag it as homework and make this clear. –  madprob Jan 8 '14 at 17:17
Just to be clear about what you mean, is HTHTT a case where $x=2$ heads come up before $y=2$ consecutive tails? Or did you mean to say "$x$ consecutive heads"? –  Barry Cipra Jan 8 '14 at 17:42
@BarryCipra, Yes, the heads do not need to be consecutive. –  user27 Jan 8 '14 at 17:46
@madprob, This is not a homework or interview question. Reading my own post, I realize it might come across as such. I've searched this site for a while and have found the answer to both individual cases (either x heads or y consecutive tails), but not how to combine them. –  user27 Jan 8 '14 at 17:49

Consider the following events:

$A_{x,y}$: You observe $x$ heads before $y$ consecutive tails.

$B_{x,y}$: You observe $y$ consecutive tails before $x$ heads.

Let $X_{i}$ follow a Geometric(p). This means that $X_{i}$ denotes the number of tail counts until you observe the first head with a biased coin. Observe that, in particular, $P(A_{1,y}) = P(X_{1} < y)$.

In order to solve for the general case, consider $X_{1},\ldots,X_{x}$ i.i.d. Geometric(p). I claim that:

\begin{align*} P(A_{x,y}) = P(X_{1} < y, \ldots,X_{x} < y) = P(X_{1} < y)^{x} \end{align*}

In order to understand the claim, you can think of $X_{i}$ as the number of observed tails it takes until you observe a head after having already observed $i-1$ heads in the past. Hence, we only need to find $P(X_{1} < y)$. This is well known and equals $1-(1-p)^{y}$.

Hence, in general $P(A_{x,y}) = (1-(1-p)^{y})^{x}$

-
Thank you! Though this does not answer my question exactly, since I was asking for a specific maximum number of tosses. On second thought, I don't actually need to know this, so I marked your answer as accepted. –  user27 Jan 9 '14 at 16:09