Tutorials › Stochastic Processes › Checking Markov Chain Simulations Against Exact Probabilities

Simulation of Stochastic Processes · Tutorial 980 of 994

Checking Markov Chain Simulations Against Exact Probabilities

Use transition matrices to calculate exact benchmarks, then assess what simulated one-step and multi-step frequencies do—and do not—tell you.

Intermediate 11 min read

What You'll Learn

  • Calculate exact one-step transition probabilities and multi-step state distributions from a row-stochastic matrix.
  • Build empirical transition counts from a simulated path and compare them with the model’s transition probabilities.
  • Prove why transition-count residuals have mean zero, even though departures from a state are random.
  • Interpret lagged frequencies while accounting for initial distributions and dependence within a path.
  • Recognize how row-sum checks and matrix multiplication can expose simulation or calculation errors.

Exact Probabilities as Simulation Benchmarks

A simulated Markov chain produces one random path, not a list of probabilities. Its observed transitions will generally differ from the probabilities used to generate them. To decide whether a simulation is behaving as intended, compare its frequencies with exact quantities computed from the transition matrix, and ask whether the observed discrepancy is plausible as sampling variation. This is a diagnostic, not a requirement that simulated counts match their expected values exactly.

Let \((X_n)_{n\ge0}\) be a time-homogeneous Markov chain on a finite state space \(S\), defined on a probability space \((\Omega,\mathcal{F},\mathbb{P})\). Its transition matrix is \(P=(p_{ij})\), where \(p_{ij}=\mathbb{P}(X_{n+1}=j\mid X_n=i)\). The Markov property says that this conditional probability is unchanged when the earlier history is also given. Each entry is nonnegative and every row sums to 1. Write \(\mu\) for the initial distribution as a row vector, so the distribution at time \(n\) is \(\mu P^n\).

For a simulated path \(X_0,\ldots,X_N\), define the number of departures from state \(i\), and the number of observed transitions from \(i\) to \(j\), by

$$ C_i=\sum_{r=1}^{N}\mathbf{1}_{\{X_{r-1}=i\}},\qquad C_{ij}=\sum_{r=1}^{N}\mathbf{1}_{\{X_{r-1}=i,\ X_r=j\}}. $$

When \(C_i>0\), the empirical one-step frequency from \(i\) to \(j\) is \(C_{ij}/C_i\). It can be compared with \(p_{ij}\). If \(C_i=0\), that empirical frequency is undefined: the path did not visit \(i\) at a time when a transition could be observed. The denominator is therefore part of the diagnosis, not a detail to omit.

Theorem: For any finite-state Markov chain, any state \(i\), any state \(j\), and any fixed \(N\ge1\), the transition-count residual \(D_{ij}=C_{ij}-p_{ij}C_i\) satisfies \(\mathbb{E}[D_{ij}]=0\) and \(\operatorname{Var}(D_{ij})=p_{ij}(1-p_{ij})\mathbb{E}[C_i]\). In particular, \(\operatorname{Var}(D_{ij})\le Np_{ij}(1-p_{ij})\). These identities do not say that \(C_{ij}/C_i\) is unbiased or that the transitions in a path are independent.

Proof. Use the filtration \(\mathcal{F}_n=\sigma(X_0,\ldots,X_n)\), which records the path through time \(n\). For \(1\le r\le N\), put \(Y_r=\mathbf{1}_{\{X_{r-1}=i\}}\bigl(\mathbf{1}_{\{X_r=j\}}-p_{ij}\bigr)\). Then \(D_{ij}=\sum_{r=1}^N Y_r\). By the Markov property, conditional on \(\mathcal{F}_{r-1}\), the probability that \(X_r=j\) is \(p_{ij}\) if \(X_{r-1}=i\). If \(X_{r-1}\ne i\), the indicator multiplying the parenthesis is zero. Thus \(\mathbb{E}[Y_r\mid\mathcal{F}_{r-1}]=0\), and taking expectations gives \(\mathbb{E}[D_{ij}]=0\).

For \(r<s\), \(Y_r\) is determined by \(\mathcal{F}_{s-1}\). Therefore \(\mathbb{E}[Y_rY_s]=\mathbb{E}\bigl[Y_r\mathbb{E}[Y_s\mid\mathcal{F}_{s-1}]\bigr]=0\). The cross terms in the variance of the sum vanish. Conditional on \(\mathcal{F}_{r-1}\), the squared term has expectation \(\mathbf{1}_{\{X_{r-1}=i\}}p_{ij}(1-p_{ij})\), since an indicator with success probability \(p_{ij}\) has variance \(p_{ij}(1-p_{ij})\). Summing these expectations gives \(\operatorname{Var}(D_{ij})=p_{ij}(1-p_{ij})\mathbb{E}[C_i]\). Finally, \(0\le C_i\le N\), so \(\mathbb{E}[C_i]\le N\). This proves the stated bound. \(\square\)

The result explains one useful feature of a one-step diagnostic: after accounting for the number of times the chain departed from \(i\), the observed count has the correct transition probability as its reference value. But the theorem concerns the residual \(C_{ij}-p_{ij}C_i\), not the ratio \(C_{ij}/C_i\). Also, the variance depends on the expected number of departures, which can be small when the chain rarely visits \(i\). A large discrepancy from a rarely visited state deserves a different interpretation from the same discrepancy after many departures.

Worked One-Step Comparisons

Worked Example: Checking Counts from a Single Path

Consider the chain on \(S=\{1,2\}\) with transition matrix \(P=\begin{bmatrix}\tfrac34&\tfrac14\\[2pt]\tfrac12&\tfrac12\end{bmatrix}\). Both rows sum to 1: \(\tfrac34+\tfrac14=1\) and \(\tfrac12+\tfrac12=1\). Suppose a simulated path is \(1,1,2,2,1,1,1,2,1,2,2,1,1\), giving \(N=12\) transitions.

Reading each consecutive pair in order gives \(1\to1,\ 1\to2,\ 2\to2,\ 2\to1,\ 1\to1,\ 1\to1,\ 1\to2,\ 2\to1,\ 1\to2,\ 2\to2,\ 2\to1,\ 1\to1\). Thus state 1 has \(C_1=7\) departures: four to state 1 and three to state 2. State 2 has \(C_2=5\) departures: three to state 1 and two to state 2.

For departures from state 1, the model probabilities \((\tfrac34,\tfrac14)\) give reference counts \((7\cdot\tfrac34,\ 7\cdot\tfrac14)=(\tfrac{21}4,\tfrac74)\). The observed counts are \((4,3)\), so the residuals are \(4-\tfrac{21}4=-\tfrac54\) and \(3-\tfrac74=\tfrac54\). For state 2, the reference counts are \((5\cdot\tfrac12,\ 5\cdot\tfrac12)=(\tfrac52,\tfrac52)\). The observed counts \((3,2)\) give residuals \(\tfrac12\) and \(-\tfrac12\).

Check: The residuals in each row add to zero: \(-\tfrac54+\tfrac54=0\) and \(\tfrac12-\tfrac12=0\), because observed and reference counts both sum to the number of departures. The empirical frequencies are \((\tfrac47,\tfrac37)\) from state 1 and \((\tfrac35,\tfrac25)\) from state 2. They differ from the exact rows, but this path alone does not establish that the simulation is wrong.

One-step frequencies are a direct diagnostic of how transitions are generated. Multi-step frequencies check a different feature: whether the simulated chain moves through time according to repeated application of the same transition rule. The exact benchmark follows by matrix multiplication, not by treating successive transitions as independent.

Theorem (multi-step distribution): If a time-homogeneous Markov chain has initial row distribution \(\mu\) and transition matrix \(P\), then the distribution of \(X_m\) is \(\mu P^m\) for every integer \(m\ge0\). In particular, starting at state \(i\), the probability of being at \(j\) after \(m\) steps is the \((i,j)\) entry of \(P^m\).

Proof. At \(m=0\), the distribution is \(\mu=\mu P^0\), since \(P^0\) is the identity matrix. Suppose the distribution at time \(m\) is \(\mu P^m\). If its \(i\)th entry is the probability of state \(i\) at time \(m\), then the probability of state \(j\) at time \(m+1\) is \(\sum_i(\mu P^m)_i p_{ij}\), by conditioning on \(X_m=i\). This sum is the \(j\)th entry of \((\mu P^m)P=\mu P^{m+1}\). Induction proves the result for every \(m\). For a chain started at \(i\), take \(\mu\) to put probability 1 on \(i\); the \(j\)th entry of \(\mu P^m\) is then \((P^m)_{ij}\). \(\square\)

Worked Multi-Step Comparisons

Worked Example: A Two-Step Benchmark

Use the matrix \(P=\begin{bmatrix}\tfrac34&\tfrac14\\[2pt]\tfrac12&\tfrac12\end{bmatrix}\), whose rows sum to 1 as checked above. Starting from state 1, calculate the exact two-step probabilities. Matrix multiplication gives

$$ P^2= \begin{bmatrix} \tfrac34\cdot\tfrac34+\tfrac14\cdot\tfrac12& \tfrac34\cdot\tfrac14+\tfrac14\cdot\tfrac12\\[3pt] \tfrac12\cdot\tfrac34+\tfrac12\cdot\tfrac12& \tfrac12\cdot\tfrac14+\tfrac12\cdot\tfrac12 \end{bmatrix} = \begin{bmatrix} \tfrac{11}{16}&\tfrac5{16}\\[2pt] \tfrac58&\tfrac38 \end{bmatrix}. $$

For example, the two ways to go from state 1 to state 1 in two steps are \(1\to1\to1\) and \(1\to2\to1\); their probabilities add to \(\tfrac34\cdot\tfrac34+\tfrac14\cdot\tfrac12=\tfrac{11}{16}\). Suppose 16 independent simulated chains all start at state 1, and their states after two steps are recorded. If 11 endpoints are state 1 and 5 are state 2, the empirical proportions are \((\tfrac{11}{16},\tfrac5{16})\), matching the exact row of \(P^2\).

Check: The first row of \(P^2\) sums to \(\tfrac{11}{16}+\tfrac5{16}=1\). The second sums to \(\tfrac58+\tfrac38=1\). The endpoint counts also total \(11+5=16\). Matching the exact proportions is possible, but not required: a different outcome count from a finite set of simulations would not by itself imply a coding error.

For independent chains run from a specified initial distribution, the distribution of each endpoint at a fixed lag is exactly the matrix-based benchmark. For one long simulated path, however, endpoints at different times—or overlapping windows of length \(m\)—are generally dependent. A collection of lagged observations from one path must not be treated as if it were automatically a collection of independent simulations. The exact probability remains a valid reference, but dependence affects how much variation to expect in the collected frequencies.

Worked Example: Checking a Nontrivial Initial Distribution

Use the same matrix, with initial row distribution \(\mu=(\tfrac13,\tfrac23)\). Its entries sum to 1, and both rows of \(P\) sum to 1. From the preceding multiplication, \(P^2=\begin{bmatrix}\tfrac{11}{16}&\tfrac5{16}\\[2pt]\tfrac58&\tfrac38\end{bmatrix}\). Multiplying once more gives

$$ P^3= \begin{bmatrix} \tfrac{11}{16}\cdot\tfrac34+\tfrac5{16}\cdot\tfrac12& \tfrac{11}{16}\cdot\tfrac14+\tfrac5{16}\cdot\tfrac12\\[3pt] \tfrac58\cdot\tfrac34+\tfrac38\cdot\tfrac12& \tfrac58\cdot\tfrac14+\tfrac38\cdot\tfrac12 \end{bmatrix} = \begin{bmatrix} \tfrac{43}{64}&\tfrac{21}{64}\\[2pt] \tfrac{21}{32}&\tfrac{11}{32} \end{bmatrix}. $$

Therefore the exact distribution at time 3 is

$$ \mu P^3= \left( \tfrac13\cdot\tfrac{43}{64}+\tfrac23\cdot\tfrac{21}{32},\ \tfrac13\cdot\tfrac{21}{64}+\tfrac23\cdot\tfrac{11}{32} \right) = \left(\tfrac{127}{192},\tfrac{65}{192}\right). $$

Suppose 32 independent simulations are initialized from \(\mu\), and at time 3 there are 21 endpoints in state 1 and 11 in state 2. Their observed proportions are \((\tfrac{21}{32},\tfrac{11}{32})=(\tfrac{126}{192},\tfrac{66}{192})\). The differences from the exact probabilities are \(-\tfrac1{192}\) and \(\tfrac1{192}\), respectively.

Check: The exact probabilities sum to \(\tfrac{127}{192}+\tfrac{65}{192}=1\), and the observed counts sum to 32. The difference between the first empirical proportion and its benchmark is \(\tfrac{126}{192}-\tfrac{127}{192}=-\tfrac1{192}\); the second difference is \(\tfrac{66}{192}-\tfrac{65}{192}=\tfrac1{192}\). The opposite signs are necessary because both pairs of proportions sum to 1.

What Discrepancies Can Tell You

A comparison is most useful when the simulation and the benchmark describe the same quantity. For a one-step check, count transitions out of a specified state and compare the row frequencies with that state’s row of \(P\). For a lag-\(m\) check, use \(P^m\) and specify the starting distribution. If the simulation begins from a fixed state \(i\), use row \(i\); if its initial states follow \(\mu\), use \(\mu P^m\). Ignoring the starting distribution can make a correct simulation appear incorrect.

Several patterns are informative. A transition frequency that differs slightly from its benchmark may reflect ordinary randomness, particularly when there were few departures from that state. A row of empirical frequencies that does not sum to 1 suggests a counting or denominator error. A persistent discrepancy in a frequently visited row, repeated across independent runs, is more concerning than one isolated difference. A mismatch in lagged frequencies, even when one-step frequencies look reasonable, can point to a problem in how the code advances the chain or records its state.

Common mistake: squaring entries instead of multiplying matrices. For the first row of \(P\), squaring entries individually gives \((\tfrac{9}{16},\tfrac1{16})\), which sums to \(\tfrac{10}{16}\), not 1. These are not the two-step probabilities. The correct first entry is \(\tfrac34\cdot\tfrac34+\tfrac14\cdot\tfrac12=\tfrac{11}{16}\), because both possible intermediate states must be included. The other entry is \(\tfrac34\cdot\tfrac14+\tfrac14\cdot\tfrac12=\tfrac5{16}\); together the correct probabilities sum to 1.

Before interpreting any discrepancy, verify that the transition matrix is the intended one and that every row sums to 1. Then verify the arithmetic for the benchmark, including the multiplication order and initial distribution. Finally, check how many relevant transitions or endpoints were observed and whether the observations come from independent runs or from one dependent path. These checks separate basic implementation mistakes from the ordinary variation that simulation is designed to produce.

Check Your Understanding

For each question, state the exact benchmark before interpreting any empirical frequency.

  1. For a transition row \((\tfrac23,\tfrac13)\), what are the reference counts after 12 departures from that state?
  2. Why does the transition-count theorem concern \(C_{ij}-p_{ij}C_i\), rather than asserting that \(C_{ij}/C_i\) is unbiased?
  3. Starting from state \(i\), which matrix quantity gives the probability of state \(j\) after three steps?
  4. If no simulated transition departs from state \(i\), can its empirical row of transition frequencies be computed? Explain.
  5. Why is squaring each entry of \(P\) not the same as calculating the two-step transition matrix?