MATH 4810Computational Modelling

Module 05 · Unit 5.1 · Lesson 3 of 3

Simulation of Stochastic State Transitions

Theory predicts the chain's behaviour. Simulation observes it. When the two agree, the model and its implementation are both confirmed.

Duration ≈ 3 hours Prerequisite Lessons 1 & 2 Feeds Lab 9 · Assignment 5
  1. Lesson 1
  2. Lesson 2
  3. Lesson 3

1 Modelling context

The analysis in Lessons 1 and 2 is exact: given the transition matrix, the state distributions at all times can be computed by matrix multiplication, and the stationary distribution can be solved for exactly. This raises a fair question: why simulate?

Simulation serves two purposes. First, it verifies the implementation: an exact calculation and a simulated estimate should agree, and a discrepancy indicates an error in one or the other. Second, it generalises: many real systems are too complex for exact analysis (inhomogeneous chains, chains with very large state spaces, chains coupled to other processes), and simulation is the only available method. The techniques developed here carry over to those more complex settings.

Exact theory is a verification tool

When a simple chain has an exact solution, use it to verify the simulation. When a complex chain does not, the simulation is the primary tool, and the verification must be done by other means.

2 Learning objectives

By the end of this lesson you will be able to

  • Simulate a Markov chain using inverse transform for the state transitions.
  • Estimate the empirical state frequencies from a simulated path.
  • Compare empirical frequencies against the theoretical stationary distribution.
  • Estimate the n-step state distribution from simulation and compare against P^n.
  • Use replication with independent seeds to quantify the sampling error of the estimates.
  • Recognise when simulation is preferable to exact analysis.

3 Prerequisite review

Inverse transform
The method for generating samples from a discrete distribution: draw U ~ Uniform(0, 1), then find the smallest index i such that the cumulative probability exceeds U.
Pseudorandom generation
The generation of uniform values, covered in Module 4. Each simulation run should record its seed.
Sampling error
The random discrepancy between a simulated estimate and the true value. Decreases as 1/√N.
Ergodic theorem
For an ergodic Markov chain, the empirical frequency of visits to a state converges to the stationary probability as the number of steps grows.

4 Mathematical formulation

Simulation algorithm

To simulate a Markov chain with transition matrix P from an initial distribution π^(0) for N steps:

  1. Choose the initial state. Draw U ~ Uniform(0, 1) and use inverse transform with π^(0) to select the starting state.
  2. For each step t = 1, …, N:
    • Draw U ~ Uniform(0, 1).
    • Use inverse transform with the current state's row of P to determine the next state: find the smallest j such that Σ_{k≤j} P_{i,k} ≥ U, where i is the current state.
    • Record the next state.

The result is a path (X_0, X_1, X_2, …, X_N).

Estimating the stationary distribution

By the ergodic theorem, the empirical frequency of each state converges to its stationary probability:

π*_j ≈ (1/N) Σ_{t=1}^N 1{ X_t = j }

For finite N, the estimate differs from π*_j by sampling error of order 1/√N. Replication with independent seeds quantifies the error.

Estimating the n-step distribution

To estimate the distribution at step n, run many independent replications, each starting from the same initial state, and record the state at step n:

π̂_j^(n) = (1/R) Σ_{r=1}^R 1{ X_n^(r) = j }

where R is the number of replications. By the law of large numbers, this converges to π_j^(n) as R grows. Compare against the exact P^n as a verification.

Choosing between simulation and exact analysis

Exact analysis is preferred when it is available: it is exact, deterministic, and requires no seed management. Simulation is preferred when:

  • The state space is very large (e.g. thousands of states) and the matrix power P^n is expensive to compute.
  • The transition probabilities depend on time or on external covariates, so no single matrix P describes the chain.
  • The chain is coupled to other processes (e.g. queueing, agent-based models) that complicate the analysis.
  • The quantity of interest is a path functional (e.g. total time spent above a threshold) that is easier to estimate by simulation than analytically.
A simulated path of a three-state Markov chain A step function showing the state of the chain over 30 steps. The chain alternates between three levels, with vertical segments showing transitions and horizontal segments showing sojourns in each state. t A B C
Figure 5.3. A simulated path of a three-state Markov chain. The path moves between states A, B, and C according to the transition probabilities. Estimating the frequency of each state over a long path approximates the stationary distribution.

5 Worked example

Simulating the weather model

Simulate the weather chain from Lesson 1 with the transition matrix

       S    C    R
S   [ 0.7  0.2  0.1 ]
C   [ 0.3  0.4  0.3 ]
R   [ 0.2  0.3  0.5 ]

starting from state S.

Step 1 — Simulate a single step.

Draw U = 0.55. The cumulative probabilities for state S are: 0.7 for S, 0.9 for C, 1.0 for R. Since 0.55 ≤ 0.7, the next state is S. Record X_1 = S.

Step 2 — Simulate 1,000 steps.

Repeat the procedure for 1,000 steps, recording the state at each. The empirical frequencies are:

Empirical frequencies after 1,000 steps
State Count Frequency Theoretical π*
S4610.4610.4565
C2800.2800.2826
R2590.2590.2609

The empirical frequencies agree with the theoretical stationary distribution to within about 0.005. This is consistent with sampling error for N = 1,000: the standard error is roughly √(p(1−p)/N) ≈ 0.016, so a discrepancy of 0.005 is well within one standard error.

Step 3 — Estimate the 5-step distribution.

Run 2,000 independent replications, each starting from S, and record the state at step 5. The empirical distribution is:

Estimated 5-step distribution from S
State Simulated π̂^(5) Exact P^5 row S
S0.4540.4572
C0.2810.2825
R0.2650.2603

The simulated and exact distributions agree to within about 0.005, consistent with the sampling error of the estimates.

Step 4 — Note the convergence.

The exact 5-step distribution from S is already very close to the stationary distribution, because the chain converges quickly. For a chain with a slower convergence rate (larger second eigenvalue), the difference between P^5 and π* would be more visible.

6 Verification check

Checks to perform

  • Confirm the initial state. The first state of the simulated path should match the intended starting state.
  • Compare empirical frequencies against the theoretical stationary distribution. For a long path (N ≥ 10,000), the empirical frequencies should be close to π*.
  • Compare the n-step distribution against P^n. For a specific n, the empirical distribution from many replications should match the corresponding row of P^n.
  • Use independent seeds across replications. Replications with the same seed are not independent and do not provide valid standard errors.
  • Check that the simulated transition matrix matches P. Estimate the one-step transition probabilities from the simulated path and compare against the specified matrix.

7 Interpretation

Simulation of a Markov chain is a form of Monte Carlo estimation (Module 4) applied to a stochastic process. The path is the sample; the state frequencies are the estimator; the stationary distribution is the target.

Three interpretive points:

  • Comparison with the theoretical distribution is a verification. When the exact P^n is available, agreement between simulation and theory verifies both the theory and its implementation in code.
  • The empirical frequency is a Monte Carlo estimate. It carries the same sampling error as any other Monte Carlo estimator: order 1/√N.
  • Simulation scales to problems that theory cannot handle. Once the algorithm is verified against a simple case, it can be applied to problems with inhomogeneous transitions, large state spaces, or coupling to other processes.

Simulation is not always cheaper

For a small, time-homogeneous chain, exact computation of P^n or of the stationary distribution is faster and more accurate than simulation. Use simulation when the problem is too complex for exact analysis, not as a replacement for a simple matrix calculation.

8 Uncertainty and limitations

The uncertainty in a simulated estimate of the stationary distribution comes from two sources.

Sources of uncertainty in simulated Markov-chain estimates
Source Effect
Finite path length Empirical frequencies differ from the stationary distribution
Autocorrelation in the path Successive states are correlated, so the effective sample size is smaller than N
Initial transient If the chain does not start in the stationary distribution, the early steps are not representative
Uncertainty in P The stationary distribution is estimated from the estimated matrix

Autocorrelation reduces effective sample size

Successive states in a Markov-chain path are correlated. This means the effective sample size is smaller than the number of steps, and the standard error of the estimates is larger than the naive √(p(1−p)/N). For slowly-mixing chains, the effect can be substantial.

9 Common implementation errors

Watch for these

  • Using the wrong row of P for the next-state decision. The row corresponds to the current state, not to a fixed state.
  • Not resetting the cumulative sum at each step. The cumulative probabilities must be computed fresh from the current state's row.
  • Using the same seed for all replications. This makes the replications identical, defeating the purpose. Use different seeds (or one seed plus a replication index) for independent runs.
  • Discarding early steps without justification. If the chain does not start in its stationary distribution, early steps are transient. Discard them (this is called "burn-in") and document how many steps were discarded.
  • Confusing the path length with the effective sample size. Because of autocorrelation, the effective sample size is smaller than the number of steps.
  • Not comparing against theory when theory is available. For a simple chain, the exact P^n and stationary distribution are available and provide a verification.

10 Independent modelling task

Your turn

  1. Use the chain from Lesson 2. Simulate 10,000 steps starting from a chosen state. Record the seed.
  2. Compute the empirical state frequencies over the last 5,000 steps (discarding the first 5,000 as burn-in). Compare against the theoretical stationary distribution.
  3. Repeat the simulation with 100 independent seeds, each for 1,000 steps, starting from the same state. Record the state at step 1,000 in each replication.
  4. Compute the empirical distribution of the states at step 1,000 across replications. Compare against the exact P^{1000} row.
  5. Compute the standard error of the estimated distribution across replications.
  6. Repeat the exercise with a burn-in period and compare the results.

This exercise completes Laboratory 9 and the simulation component of Assignment 5.

11 Reflection prompt

Reflect before continuing

For a simple, time-homogeneous Markov chain, exact analysis is faster and more accurate than simulation. Under what circumstances would you still simulate?

Two or three sentences. Consider large state spaces, time-inhomogeneous transitions, path functionals, and coupling to other processes.

12 Lesson summary

Key takeaways

  • Simulating a Markov chain uses inverse transform at each step. The cumulative probabilities are computed from the current state's row of P.
  • The empirical frequency of a state estimates its stationary probability. By the ergodic theorem, the estimate converges as the path length grows.
  • The n-step distribution can be estimated by replication. Compare against the exact P^n to verify the implementation.
  • Autocorrelation reduces the effective sample size. Standard errors from simulated paths are larger than the naive 1/√N formula suggests.
  • Simulation is the tool of choice for large, inhomogeneous, or coupled systems. For simple chains, exact analysis is preferred.

Unit 5.1 complete

You have specified Markov chains, computed their multi-step and steady-state behaviour, and simulated their transitions. Unit 5.2 turns to discrete-event simulation — a different paradigm for stochastic systems, based on events and a simulated clock.

§ Self-quiz, practice, and homework

Self-quiz

Q1

Simulating a Markov chain requires which method to select the next state?

Show answer

Inverse transform.

Q2

The empirical frequency of a state in a long simulated path estimates:

Show answer

The stationary probability of that state. By the ergodic theorem.

Q3

To estimate the n-step distribution from an initial state, you should:

Show answer

Run many independent replications of length n. The empirical distribution of the state at step n estimates π^(n).

Q4

Autocorrelation in the simulated path causes:

Show answer

The effective sample size to be smaller than the path length. This inflates the standard error relative to the naive formula.

Q5

Simulation is preferable to exact analysis when:

Show answer

The chain is large, time-inhomogeneous, or coupled to other processes. These cases are where exact analysis becomes impractical.

Practice exercises

  1. For the two-state chain with P_{on→off} = 0.1 and P_{off→on} = 0.3, simulate 1,000 steps by hand (or in a spreadsheet) starting from the on state. Compute the empirical frequencies and compare against the theoretical stationary distribution.
  2. For the weather model, run 500 independent replications of length 3 from state S. Compute the empirical distribution of the state at step 3 and compare against the exact row of P^3.
  3. Explain in two sentences why two independent replications should use different seeds.
  4. For a slowly-mixing chain (second eigenvalue close to 1), how many steps of burn-in would you recommend? Justify.

Homework problems

  1. Simulation verification. Implement a simulation of your chain from Lesson 2's homework. Run 100,000 steps and compute the empirical state frequencies. Compare against the exact stationary distribution and compute the standard error of each frequency estimate. Write a 500-word report.
  2. Burn-in study. Repeat the simulation, computing the empirical frequencies using only the last 50,000, 20,000, 10,000, and 5,000 steps. How do the estimates change? At what point do they stabilise?
  3. Autocorrelation. Estimate the autocorrelation function of the indicator sequence 1{X_t = i} for a chosen state i. Estimate the integrated autocorrelation time and the effective sample size. Compare against the number of steps.

Mini project

Choose a Markov-chain model with 3–4 states. Simulate it, estimate the stationary distribution from the empirical frequencies, estimate the n-step distribution from replications, and compare both against exact analysis. Include a burn-in study and an autocorrelation analysis. Submit the spreadsheet and a two-page technical report. This completes Laboratory 9 and feeds Assignment 5.

👤