# Successes or failures in a row

Let random variable $X$ be defined as the number of independent Bernoulli(p) trials required until we observe either two successes or two failures in a row. Find $P(X=n)$ for $n=2,3,4, \ldots$ Then find the expectation and variance of $X$.

Okay so since there has to be two successes or failures in a row, the trials have to alternate $SFSFSF...$ or $FSFSFS...$ until it comes upon two successes or two failures. I initially tried using the Negative Binomial Distribution here but I remembered that it doesn't take order into account so that wouldn't work. Then I tried breaking it up into if $n$ is odd vs. if $n$ is even but but I didn't really know where to go from there...

-

You can try a simple bookkeeping here like this. If $p$ is the probability of success, than, obviously, you get $$P(X=2)=p^2+(1-p)^2=1-2p+2p^2,$$ because either you have two successes or two failures, and these two events are disjoint. Next, $$P(X=3)=(1-p)p^2+p(1-p)^2=(1-p)p.$$ Two more: $$P(X=4)=p(1-p)p^2+(1-p)p(1-p)^2=(1-p)p(1-2p+2p^2).$$ $$P(X=5)=(1-p)p(1-p)p^2+p(1-p)p(1-p)^2=(1-p)^2p^2.$$ Do you see a pattern here? If not, try a couple more values for $n$. Finally, you get $$P(X=2k)=(p(1-p))^{k-1}(1-2p+2p^2),\quad k=1,2,\ldots$$ $$P(X=2k+1)=(p(1-p))^{k},\quad k=1,2,\ldots$$ The sanity check shows that $\sum_k [P(X=2k)+P(X=2k+1)]=1$ as it should be.
Using the found distribution it is possible to find $E[X]$ and Var$[X]$ if you know how to find sums of the form $$\sum_k kp^k,\quad \sum_k k^2p^k.$$
Hint: This will depend upon the probability of success, so name it $X(p)$, where $p$ is the probability of success on one trial. $X(p)=X(1-p)$ do you see why? We can either start with a success or a failure. If we let $Y(p)$ be the number of additional trials required for two successes or two failures if we have started with a success and $Z(p)$ be the number of additional trials required for two successes or two failures if we have started with a failure, then $X(p)=2(p^2+(1-p)^2)+(1-p)pY(p)+p(1-p)Z(p), Y(p)=p+(1-p)(1+Z(p)), Z(p)=(1-p)+p(1+Y(p))$. For $X$, we can either have the first two be the same, or have them different. Then for $Y$ and $Z$, we can either have the first one match what we came in with or not. Three equations in three unknowns.
@donkeykong15: you can do it that way and Artem did. My approach was to find disjoint conditions that lead to the end. Either you start with two successes or two failures, or you start with one of each and keep going. If you keep going, you care what the previous result is, because that is the one that terminates now. The equation for $Y$ comes from the fact that you either succeed and are done, or fail and now you need $Z$ trials. – Ross Millikan Apr 23 '12 at 13:34