Three chains with different long-run behaviour, simulated and estimated, plus a dice game solved exactly by first-step analysis.
A Markov chain is a process that moves between a finite set of states, where the next state depends only on the current one. All of its behaviour is carried by a transition matrix $P$, whose entry $p_{ij}$ is the probability of moving from state $i$ to state $j$. The first computer lab for MASC13 takes three such matrices, chosen so that each behaves completely differently in the long run, and asks the same questions of each.
$$ P_1 = \begin{pmatrix} 1/3 & 1/3 & 1/3 \cr 1/2 & 0 & 1/2 \cr 1/2 & 1/2 & 0 \end{pmatrix} \qquad P_2 = \begin{pmatrix} 1 & 0 & 0 \cr 1/6 & 1/3 & 1/2 \cr 1/3 & 1/3 & 1/3 \end{pmatrix} \qquad P_3 = \begin{pmatrix} 0 & 1 & 0 & 0 \cr 0 & 0 & 1/2 & 1/2 \cr 1 & 0 & 0 & 0 \cr 1 & 0 & 0 & 0 \end{pmatrix} $$
$P_1$ can reach any state from any other and has a positive diagonal entry, so it should settle down. $P_2$ has $p_{11} = 1$: state 1 is a trap, and once the chain arrives it never leaves. $P_3$ has a zero diagonal and a rigid structure, $1 \to 2 \to \{3, 4\} \to 1$, with the only genuine choice being which of states 3 and 4 to visit.
For $P_1$, about three to five steps. After that the realizations are indistinguishable and the distribution has settled at $\pi = (3/7,\ 2/7,\ 2/7)$. Two hundred simulated steps spent fractions $(0.42,\ 0.285,\ 0.295)$ of the time in the three states, which is the same thing measured a different way: $\pi_i = 1 / E(T_i)$, the reciprocal of the mean return time to state $i$.
For $P_2$, until absorption and no longer. Starting from state 2 the chain reaches the trap after 4.2 steps on average, and from state 3 after 3.6; a thousand simulations gave 4.40 and 3.80. After that every realization is identical, because they are all sitting in state 1.
For $P_3$, forever. Starting in state 1 the chain is in state 1 at steps $0, 3, 6, \dots$, and starting in state 2 it is there at steps $2, 5, 8, \dots$ The two never come into phase. The distribution cycles,
$$(1,0,0,0) \to (0,1,0,0) \to (0,0,\tfrac12,\tfrac12) \to (1,0,0,0) \to \cdots$$
and so never converges, even though $\pi = (1/3,\ 1/3,\ 1/6,\ 1/6)$ is stationary: start the chain at $\pi$ and it stays there, start it anywhere else and it never arrives.
The Perron-Frobenius theorem says that for a finite irreducible chain of period $d$, exactly $d$ eigenvalues of $P$ have modulus 1. That turns a question about paths through the chain into one a computer answers immediately.
The eigenvalues of $P_1$ are $1,\ -0.5,\ -0.167$, so one lies on the unit circle and the chain is aperiodic. Those of $P_3$ are $1,\ -0.5 \pm 0.866i,\ 0$, three of modulus 1, giving period 3, which matches the cycle seen in the simulation. $P_2$ is the instructive exception: it is not irreducible, so the theorem does not apply at all, and the period has to be argued from the positive diagonal instead.
Given one realization, the transition probabilities are estimated by relative frequencies, $\hat p_{ij} = n_{ij} / n_i$, with confidence intervals from the normal approximation to the binomial. That approximation needs $n_i \hat p_{ij} (1 - \hat p_{ij}) \ge 10$, and whether it holds is the real question.
For $P_1$ it does. From 200 steps, $\hat p_{12} = 0.284$ over $n_1 = 88$ steps out of state 1 gives the interval $[0.190,\ 0.378]$, which contains the true $1/3$; the same holds for $p_{23}$ and $p_{32}$.
For $P_2$ it fails completely. The chain is absorbed after a couple of steps, so the counts out of states 2 and 3 are 1 and 1. One row rests on a single observed transition and another on none at all. No amount of care with the arithmetic fixes this, because the data is not there. The remedy is structural: run many independent realizations, or restart the chain after each absorption, and pool the counts.
For $P_3$ the situation inverts. Rows 1, 3 and 4 are forced moves, estimated exactly and needing no interval, and only row 2 is random. There $n_2 = 66$ with $p = 1/2$ gives $npq = 16.5$, comfortably above the threshold.
Roll a fair die until two consecutive rolls differ by exactly 1. For $3, 4, 1, 6, 5, 4, 2$ the game ends on the fifth roll. How many rolls does it take on average?
Modelled as a chain whose state is the last roll, plus an absorbing state for the finished game, symmetry reduces six unknowns to three and first-step analysis gives $m_1 = 70/17$, $m_2 = 58/17$, $m_3 = 60/17$. Since the first roll is uniform,
$$E(N) = 1 + \tfrac16 \sum_{k=1}^{6} m_k = \tfrac{239}{51} \approx 4.69.$$
Ten thousand simulated games gave 4.69.
Linear algebra for the structural questions, left eigenvectors and stationary distributions and first-step analysis, with simulation as the check on each answer rather than as a substitute for it. Written in NumPy from the transition matrices up: simulating realizations, estimating matrices from counts, iterating distributions forward, and solving $\pi P = \pi$.
One detail worth recording, since it corrupts results silently. The supplied
estimation helper divides with a where= mask but no out= argument, so rows
for states the chain never left are filled with whatever happened to be in that
memory rather than with zeros or NaN. The numbers look entirely plausible and
mean nothing, which is the kind of failure that survives a glance at the
output.
Back to all engineering work, or the lecture notes.