Escaping Prisoner Probability Question

To calculate $E[X]$ you presumably did $$E[X] = 0.5 E[X+2]+ 0.3 E[X+3]+0.2 E[0]$$ i.e. $$E[X] = 0.5 E[X] + 0.5 \times 2+ 0.3 E[X] +0.3 \times 3$$ and then solved to find $E[X]$.

Similarly you can do $$E[X^2] = 0.5 E[(X+2)^2]+ 0.3 E[(X+3)^2]+0.2 E[0^2]$$ i.e. $$E[X^2] = 0.5 E[X^2] +0.5 \times 4 E[X] +0.5 \times 4 + 0.3 E[X^2] +0.3 \times 6 E[X]+0.3 \times 9$$ and you already know $E[X]$. Then use $Var(X) = E[X^2] - (E[X])^2$


A general approach: Let $X$ the time when de prisoner is free. We will calculate $m_X(t)=E[e^{tX}]$. Let $Y$ the first door choosen.

Note that

$\begin{eqnarray}E[e^{tX}]&=&\sum_{y}E[e^{tX}|Y=y]P(Y=y)\\ &=&E[e^{tX}|Y=1]0.5+E[e^{tX}|Y=2]0.3+E[e^{tX}|Y=3]0.2\end{eqnarray}$

Now, given $Y=1$, $X=2+Z$ where $Z$ is the number of additional days to freedom after the prisoner returned to his cell. But once he returned, the experiment is newly the same. Then $X$ and $Z$ have the same distribution. Thus $E[e^{tX}|Y=1]=E[e^{t(2+Z)}]=e^{2t}E[e^{tZ}]=e^{2t}E[e^{tX}]$.

Analogously $E[e^{tX}|Y=2]=e^{3t}E[e^{tX}]$.

Finally, $E[e^{tX}|Y=3]=1$, cause $X=0$.

Therefore $m_X(t)=E[e^{tX}]=0.5e^{2t}m_X(t)+0.3e^{3t}m_X(t)+0.2$. Thus $m_X(t)=\dfrac{0.2}{1-0.5e^{2t}-0.3e^{3t}}$.

Now it is easy to finish:

$E[X]=m_X^\prime(0)=\left.\dfrac{-0.2(-e^{2t}-0.9e^{3t})}{(1-0.5e^{2t}-0.3e^{3t})^2}\right|_{t=0}=9.5$

$E[X^2]=m_X^{\prime\prime}(0)=\left.0.2\left(\dfrac{2(-e^{2t}-0.9e^{3t})^2}{(-0.5e^{2t}-0.3e^{3t}+1)^3}-\dfrac{-2e^{2t}-2.7e^{2t}}{(-0.5e^{2t}-0.3e^{3t}+1)^2}\right)\right|_{t=0}=204$, so $Var(X)=204-9.5^2=113.75$