Let's say I have word constructed from random letters, A Band C with $\mathbf{P}(A) = \mathbf{P}(B) = \mathbf{P}(C) = \frac{1}{3}$. I am going to do a random trial and record the letters I got. The experiment stops the first time I spell out the word ABC. Let $N$ be the number of trials until I make the word ABC out of letters.
Here are some trial words:
BBBBACCCCBABAABBBBCBCCBBBCACBCAACBABC
BBACCCCACABABC
CBBCCCABBABC
BABBBCAAAABC
CBBBCCBCCABABC
CCBCBBABC
ACCACCCCBCBBBCBACCBBAABBABBACCCBCBAABC
ABAAABBBABC
ABABC
BBCACAACCACCAABAAABBCABBBBACABACBACBAABACCCBCBCCCBCCCBAAAABC
I am asking for the expected length of this word. And the variance.
- $\mathbb{E}[N]$ expectation
- $\mathbb{E}[N^2] - \mathbb{E}[N]^2$ variance
Sounding more like a textbook:
Our random variable is $X \in \{ A,B,C\}$ where each letter appears with equal probability. Let's examine the sequence $(X_1, X_2, X_3, \dots , X_n)$ where $X_i$ are iid random variables with probability the same as $X$. Our process stops at time $t = N$ when $(X_{N-2}, X_{N-1}, X_N) = (A,B,C)$. What is the expected value of $N$ ?
Once you spot that this is a Markov chain (as correctly tagged) the problem is easy to solve with first step analysis. States: $0, A, AB, ABC$, meaning that the ending of the word you already have is not helpful (equivalent to the empty word), ends with $A$, ends with $AB$ and ends with $ABC$, respectively. The state $ABC$ is the only absorbing state. Transitions:
$0\rightarrow 0$ if you get $B$ or $C$, so the transition probability is $2/3$.
$0\rightarrow A$ if you get $A$, so the transition probability is $1/3$.
$A\rightarrow 0$ if you get $C$, so the transition probability is $1/3$.
$A\rightarrow A$ if you get $A$, so the transition probability is $1/3$.
$A\rightarrow AB$ if you get $B$, so the transition probability is $1/3$.
$AB\rightarrow 0$ if you get $B$, so the transition probability is $1/3$.
$AB\rightarrow A$ if you get $A$, so the transition probability is $1/3$.
$AB\rightarrow ABC$ if you get $C$, so the transition probability is $1/3$.
So the transition matrix is
$\begin{pmatrix} 2/3 & 1/3 & 0 & 0\\ 1/3 & 1/3 & 1/3 & 0\\ 1/3 & 1/3 & 0 & 1/3\\ 0 & 0 & 0 & 1\\ \end{pmatrix}$
Can you finish from here? There is a formula that computes the expected number of steps from each transient state given the transition matrix.