Tutorials › Stochastic Processes › Simulating Markov Chain Occupation Times and Rewards

Simulation of Stochastic Processes · Tutorial 979 of 994

Simulating Markov Chain Occupation Times and Rewards

Convert simulated Markov chain paths into occupation and reward estimates, and understand when those estimates represent a long-run average.

Intermediate 10 min read

What You'll Learn

  • Define state-occupation counts and empirical occupation proportions on a finite trajectory.
  • Simulate transitions by selecting from each row of a transition matrix.
  • Accumulate state-dependent rewards and compute their time average.
  • Derive expected finite-horizon occupation and reward totals.
  • Identify the assumptions behind long-run occupation and reward averages.
  • Recognize why reducible chains may not have a single long-run average.

From a Simulated Path to Occupation and Reward

A simulated Markov chain path contains more information than its final state. It records how often the chain visited each state, and therefore how much time it spent in different parts of the state space. If each state has a cost, benefit, or other numerical value attached to it, the same path also gives a cumulative reward. These summaries turn a sequence of simulated transitions into estimates of long-run behavior.

Let \((X_n)_{n\ge0}\) be a Markov chain on a finite state space \(S\), defined on a probability space \((\Omega,\mathcal{F},\mathbb{P})\), with transition matrix \(P=(p_{ij})\). The entries are nonnegative and each row sums to \(1\). For simulation, one can generate the next state from the current row of \(P\) using a fresh uniform random variable. For a trajectory with \(N\ge1\) observed states, we use \(X_0,\ldots,X_{N-1}\): this convention counts the initial state and gives \(N\) observations.

Definition: The occupation count of state \(i\in S\) over \(N\) observations is \(C_i(N)=\sum_{k=0}^{N-1}\mathbf{1}_{\{X_k=i\}}\), where \(\mathbf{1}_{\{X_k=i\}}\) is \(1\) when \(X_k=i\) and \(0\) otherwise. The empirical occupation proportion is \(\widehat{\pi}_i(N)=C_i(N)/N\). Given a state-dependent reward function \(r:S\to\mathbb{R}\), the accumulated reward is \(R_N=\sum_{k=0}^{N-1}r(X_k)\), and the average reward per observed state is \(\overline{r}_N=R_N/N\).

Every observed state contributes exactly once, so \(\sum_{i\in S}C_i(N)=N\) and \(\sum_{i\in S}\widehat{\pi}_i(N)=1\). Also, the accumulated reward can be computed either along the path or from the occupation counts: \(R_N=\sum_{i\in S}r(i)C_i(N)\). Thus \(\overline{r}_N=\sum_{i\in S}r(i)\widehat{\pi}_i(N)\). The empirical occupation proportions and the average reward are two summaries of the same finite path.

Simulating and Summarizing a Path

To simulate a transition from state \(i\), list the destinations in a fixed order and form cumulative row probabilities. A uniform random variable \(U\) on \([0,1)\) selects the first destination whose cumulative probability is strictly greater than \(U\). Equivalently, intervals can be taken half-open: a destination with cumulative interval \([a,b)\) is selected when \(a\le U<b\). This convention assigns each possible \(U\) to one destination, including values at interval boundaries.

1
Initialize.
Choose the initial state \(X_0\), the number \(N\) of observed states, and the reward function \(r\). Set all counts and the reward total to zero.
2
Record the current state.
For each \(k=0,\ldots,N-1\), add \(1\) to \(C_{X_k}(N)\) and add \(r(X_k)\) to the reward total.
3
Generate the next state if needed.
When \(k<N-1\), draw a fresh uniform random variable and select \(X_{k+1}\) from row \(X_k\) of \(P\). No transition after the final observation is needed.
4
Normalize and check.
Divide each count and the reward total by \(N\). Check that the counts sum to \(N\), the occupation proportions sum to \(1\), and the reward total equals \(\sum_i r(i)C_i(N)\).

For a formal description of the simulation, let \(U_1,U_2,\ldots\) be independent uniform random variables on \([0,1)\), independent of \(X_0\). The state \(X_{k+1}\) is selected from row \(X_k\) using \(U_{k+1}\). With the filtration \(\mathcal{F}_k=\sigma(X_0,U_1,\ldots,U_k)\), this construction makes the conditional transition probabilities satisfy \(\mathbb{P}(X_{k+1}=j\mid\mathcal{F}_k)=p_{X_kj}\). The counts and rewards are then computed from the generated path, not from additional assumptions about the future.

Worked Example: Occupation Counts in a Two-State Chain

Let \(S=\{0,1\}\), with rows and columns in the order \(0,1\), and transition matrix \(P=\begin{bmatrix}\tfrac34&\tfrac14\\[2pt]\tfrac12&\tfrac12\end{bmatrix}\). Its row sums are \(\tfrac34+\tfrac14=1\) and \(\tfrac12+\tfrac12=1\), so it is a valid transition matrix. Start at \(X_0=0\), and use the uniforms \(U_1=\tfrac45\), \(U_2=\tfrac15\), \(U_3=\tfrac7{10}\), and \(U_4=\tfrac1{10}\) to generate four transitions.

From state \(0\), the intervals are \([0,\tfrac34)\) for state \(0\) and \([\tfrac34,1)\) for state \(1\). Thus \(U_1=\tfrac45\) sends the chain to \(1\). From state \(1\), the intervals are \([0,\tfrac12)\) for state \(0\) and \([\tfrac12,1)\) for state \(1\), so \(U_2=\tfrac15\) sends it to \(0\). In row \(0\), \(U_3=\tfrac7{10}<\tfrac34\) sends it to \(0\), and \(U_4=\tfrac1{10}<\tfrac34\) also sends it to \(0\). The five observed states are therefore \(0,1,0,0,0\).

For \(N=5\), the counts are \(C_0(5)=4\) and \(C_1(5)=1\), giving occupation proportions \(\widehat{\pi}_0(5)=\tfrac45\) and \(\widehat{\pi}_1(5)=\tfrac15\). They sum to \(\tfrac45+\tfrac15=1\), and the counts sum to \(4+1=5\). If \(r(0)=2\) and \(r(1)=-1\), then \(R_5=4(2)+1(-1)=7\), so \(\overline{r}_5=\tfrac75\). Computing from the occupation proportions gives \(\tfrac45(2)+\tfrac15(-1)=\tfrac75\), as required.

Check: The probability of this particular four-transition path, conditional on starting at \(0\), is \(p_{01}p_{10}p_{00}p_{00}=\tfrac14\cdot\tfrac12\cdot\tfrac34\cdot\tfrac34=\tfrac9{128}\). This agrees with multiplying the transition probabilities along \(0\to1\to0\to0\to0\). The simulated average \(\tfrac75\) is a finite-path estimate, not a claim that every length-five path has this average.

What the Finite-Trajectory Averages Estimate

Let the initial distribution be the row vector \(\mu_0\), so that \(\mu_k=\mu_0P^k\) is the distribution of \(X_k\). The expected occupation count and expected accumulated reward have exact finite-horizon expressions. They describe averages over repeated simulations, while an individual trajectory gives one realized estimate.

Proposition: For a finite-state Markov chain with initial distribution \(\mu_0\), for every \(N\ge1\) and every function \(r:S\to\mathbb{R}\), \(\mathbb{E}[C_i(N)]=\sum_{k=0}^{N-1}\mathbb{P}(X_k=i)=\sum_{k=0}^{N-1}(\mu_0P^k)_i\), and \(\mathbb{E}[R_N]=\sum_{k=0}^{N-1}\sum_{i\in S}(\mu_0P^k)_i r(i)\).

Proof. By definition, \(C_i(N)=\sum_{k=0}^{N-1}\mathbf{1}_{\{X_k=i\}}\). Taking expectations and using linearity for this finite sum gives \(\mathbb{E}[C_i(N)]=\sum_{k=0}^{N-1}\mathbb{E}[\mathbf{1}_{\{X_k=i\}}]=\sum_{k=0}^{N-1}\mathbb{P}(X_k=i)\). The distribution of \(X_k\) is \(\mu_0P^k\), so the first identity follows. Also \(R_N=\sum_{k=0}^{N-1}r(X_k)=\sum_{k=0}^{N-1}\sum_{i\in S}r(i)\mathbf{1}_{\{X_k=i\}}\), since exactly one indicator is \(1\) at each time. Taking expectations and applying the first identity state by state gives the stated reward formula. \(\square\)

If \(X_0\) has a stationary distribution \(\pi\), meaning \(\pi P=\pi\), then \(\mu_k=\pi\) at every \(k\). The proposition implies \(\mathbb{E}[\widehat{\pi}_i(N)]=\pi_i\) and \(\mathbb{E}[\overline{r}_N]=\sum_i\pi_i r(i)\). For a nonstationary initial distribution, these equalities need not hold at a finite \(N\). In particular, dividing an expected total by \(N\) does not make every finite-trajectory estimate equal to its long-run target.

Worked Example: A State-Dependent Reward

Take \(S=\{A,B,C\}\), in that order, and \(P=\begin{bmatrix}\tfrac12&\tfrac12&0\\0&\tfrac12&\tfrac12\\\tfrac14&0&\tfrac34\end{bmatrix}\). The row sums are \(\tfrac12+\tfrac12+0=1\), \(0+\tfrac12+\tfrac12=1\), and \(\tfrac14+0+\tfrac34=1\). Start at \(A\), and use \(U_1=\tfrac13\), \(U_2=\tfrac34\), \(U_3=\tfrac18\), and \(U_4=\tfrac14\).

In row \(A\), the intervals for \(A,B,C\) are \([0,\tfrac12)\), \([\tfrac12,1)\), and the empty interval. Since \(\tfrac13<\tfrac12\), the first transition is to \(A\), not \(B\). To make the path visit all three states, use \(U_1=\tfrac23\) instead; it selects \(B\). In row \(B\), \(U_2=\tfrac34\) selects \(C\), because the intervals are \([0,\tfrac12)\) for \(B\) and \([\tfrac12,1)\) for \(C\). In row \(C\), \(U_3=\tfrac18<\tfrac14\) selects \(A\). Finally \(U_4=\tfrac14<\tfrac12\) selects \(A\) from row \(A\). Thus the five observations are \(A,B,C,A,A\).

With \(r(A)=0\), \(r(B)=4\), and \(r(C)=-2\), the occupation counts are \(C_A(5)=3\), \(C_B(5)=1\), and \(C_C(5)=1\). The proportions are \(\tfrac35,\tfrac15,\tfrac15\), whose sum is \(\tfrac35+\tfrac15+\tfrac15=1\). The pathwise reward is \(R_5=0+4-2+0+0=2\), so \(\overline{r}_5=\tfrac25\). The occupation calculation gives \(\tfrac35(0)+\tfrac15(4)+\tfrac15(-2)=\tfrac45-\tfrac25=\tfrac25\).

This chain is finite and irreducible: each state can be reached from each other state along transitions with positive probability. Its stationary distribution is \(\pi=(\tfrac14,\tfrac14,\tfrac12)\). To check, its entries sum to \(1\), and \(\pi P\) has coordinates \(\tfrac14\cdot\tfrac12+\tfrac12\cdot\tfrac14=\tfrac14\), \(\tfrac14\cdot\tfrac12+\tfrac14\cdot\tfrac12=\tfrac14\), and \(\tfrac14\cdot\tfrac12+\tfrac12\cdot\tfrac34=\tfrac12\). The stationary mean reward is \(\tfrac14(0)+\tfrac14(4)+\tfrac12(-2)=0\). The realized finite-path average \(\tfrac25\) need not equal this target. As a path check, the probability of \(A\to B\to C\to A\to A\), conditional on starting at \(A\), is \(\tfrac12\cdot\tfrac12\cdot\tfrac14\cdot\tfrac12=\tfrac1{32}\), using the four corresponding matrix entries.

Long-Run Interpretation and Its Limits

For a finite irreducible Markov chain, there is a precise long-run interpretation of occupation and reward averages. The finite-state Markov chain ergodic theorem states that for any initial state, \(\widehat{\pi}_i(N)\to\pi_i\) almost surely for every state \(i\), where \(\pi\) is the unique stationary distribution. Consequently, \(\overline{r}_N\to\sum_i\pi_i r(i)\) almost surely for every real-valued state reward \(r\). This standard theorem is proved in the later course material on long-run Markov chain behavior; the conditions here are finite state space and irreducibility. Aperiodicity is not required for these time averages, although it is relevant to some other types of convergence.

The reward conclusion follows directly from the occupation conclusion: \(\overline{r}_N=\sum_i r(i)\widehat{\pi}_i(N)\), and this is a finite sum. If each \(\widehat{\pi}_i(N)\) converges to \(\pi_i\), then each term converges to \(r(i)\pi_i\), and the finite sum converges to \(\sum_i r(i)\pi_i\). This tells us what a sufficiently long trajectory estimates; it does not guarantee that a short trajectory is close to the target.

Worked Example: Why Irreducibility Matters

Consider \(S=\{1,2\}\) and \(P=\begin{bmatrix}1&0\\0&1\end{bmatrix}\). Both row sums are \(1\). Starting at state \(1\), the chain remains at \(1\) at every time. For \(N=4\), the observations are \(1,1,1,1\), so \(C_1(4)=4\), \(C_2(4)=0\), and the occupation proportions are \((1,0)\). If \(r(1)=5\) and \(r(2)=-1\), the average reward is \(5\).

This chain is reducible, so the finite irreducible-chain conclusion does not apply. Indeed, every distribution \(\pi=(a,1-a)\), with \(0\le a\le1\), is stationary because \(\pi P=\pi\). The corresponding stationary mean reward is \(5a-(1-a)=6a-1\), which varies with \(a\). The realized long-run average from state \(1\) is \(5\), while from state \(2\) it is \(-1\). There is no single long-run reward shared by all starting states. The pathwise counts are correct; what fails is the irreducibility condition needed for one common target.

Common mistake. If rewards are attached to observed states, divide their sum by the number of observed states, not automatically by the number of transitions. In the first example there are five observed states and four transitions. The reward total is \(7\), so the defined average is \(7/5\). Dividing by four gives \(7/4\), which is the wrong quantity for this convention. A different definition based on rewards attached to transitions would use a transition-indexed reward and a denominator matching the number of transitions; it should not be mixed with the state-reward definition.

When reporting a simulation summary, state the observation convention, the initial state or distribution, the transition matrix, the reward function, and the trajectory length. These specify what the counts and average mean. The finite-horizon formulas give useful exact checks on expected totals, while the ergodic theorem explains the target of long runs when the chain is finite and irreducible.

Check Your Understanding

For each question, use the convention that \(N\) observations are \(X_0,\ldots,X_{N-1}\).

  1. If a trajectory of length \(N=6\) visits state \(a\) twice, what is its empirical occupation proportion for \(a\)?
  2. For a state-reward function \(r\), express the accumulated reward using the occupation counts \(C_i(N)\).
  3. Why must the occupation proportions sum to \(1\), even if some states are never visited?
  4. State the finite-state assumptions under which the occupation proportions converge almost surely to a single stationary distribution.
  5. For the identity transition matrix on two states, explain why a single long-run reward need not apply to every starting state.