Solution (source code)

= Solution

Stationarity at zero gives $\pi_0=q\pi_0+q\pi_1$, hence $\pi_1=(p/q)\pi_0$. At one, $\pi_1=p\pi_0+q\pi_2$; substituting $p\pi_0=q\pi_1$ gives $q\pi_2=p\pi_1$. More generally the interior equations are $\pi_i=p\pi_{i-1}+q\pi_{i+1}$. Induction gives
$$
\pi_i=(p/q)^i\pi_0.
$$
Since $p/q<1$, normalization of this <geometric series> gives
$$
\boxed{\pi_i=(1-p/q)(p/q)^i,\qquad i=0,1,\ldots}.
$$
These probabilities satisfy <detailed balance> $\pi_ip=\pi_{i+1}q$, including the boundary balance with the holding probability. Thus they do define a <stationary distribution>. Irreducibility then gives its uniqueness.