5  Markov Chains Review

Core idea. A fixed policy turns a reinforcement-learning problem into an ordinary Markov chain. Markov chains therefore provide the probability, linear algebra, and asymptotic language behind value functions, policy evaluation, simulation, exploration, and long-run behavior.

5.1 Learning goals

After reading this chapter, students should be able to:

  1. define a finite discrete-time Markov chain using conditional probability;
  2. compute multi-step transition probabilities using matrix powers;
  3. distinguish transient, recurrent, absorbing, irreducible, and aperiodic behavior in finite chains;
  4. find stationary distributions and explain when the chain converges to stationarity;
  5. interpret Markov transition matrices as linear operators on distributions and functions;
  6. use absorbing-chain formulas to compute absorption probabilities and expected absorption times;
  7. connect a Markov chain induced by a fixed policy to reinforcement learning;
  8. estimate a transition matrix from simulated or observed trajectories;
  9. explain how Markov chains appear inside modern AI systems through simulation, state abstraction, and generative modeling.

This chapter is a review, but it is not a superficial review. Reinforcement learning depends on Markov chains at a structural level. Before we optimize a policy, we must understand the stochastic process generated by a policy.

5.2 2.1 Why Markov chains come before reinforcement learning

In supervised learning, a typical observation has the form

\[ (X_i,Y_i), \qquad i=1,\ldots,n, \]

and the mathematical model often begins with the idea that the observations are independent or conditionally independent. Reinforcement learning is different. The data are generated by interaction over time:

\[ S_0,A_0,R_1,S_1,A_1,R_2,S_2,\ldots . \]

The next state depends on the current state and current action:

\[ P(S_{t+1}=s' \mid S_t=s,A_t=a). \]

If a policy \(\pi\) is fixed, then actions are sampled from

\[ \pi(a\mid s)=P(A_t=a\mid S_t=s), \]

and the state process becomes a Markov chain with transition matrix

\[ P_{\pi}(s,s') = \sum_{a}\pi(a\mid s)P(s'\mid s,a). \]

This identity is one of the most important bridges in the book. It says that policy evaluation is Markov chain analysis plus rewards. Dynamic programming, temporal-difference learning, and many forms of policy gradient methods all build on this point.

For this chapter, ignore optimization for a moment. Fix the behavior rule and study the stochastic process it creates. Later chapters optimize over such behavior rules.

5.3 2.2 Discrete-time Markov chains

Let \(X_0,X_1,X_2,\ldots\) be random variables taking values in a finite state space

\[ S = \{1,2,\ldots,n\}. \]

The process is a discrete-time Markov chain if, for every \(t\geq 0\) and every sequence of states with positive conditioning probability,

\[ P(X_{t+1}=j\mid X_t=i,X_{t-1}=i_{t-1},\ldots,X_0=i_0) = P(X_{t+1}=j\mid X_t=i). \]

If this conditional probability does not depend on time \(t\), the chain is time-homogeneous. We then write

\[ P_{ij}=P(X_{t+1}=j\mid X_t=i). \]

The matrix

\[ P= \begin{pmatrix} P_{11} & P_{12} & \cdots & P_{1n} \\ P_{21} & P_{22} & \cdots & P_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ P_{n1} & P_{n2} & \cdots & P_{nn} \end{pmatrix} \]

is called the transition matrix.

Because each row gives a probability distribution, \(P\) satisfies

\[ P_{ij}\geq 0, \qquad \sum_{j=1}^n P_{ij}=1. \]

Such a matrix is called row-stochastic.

Finite Markov chain model. A time-homogeneous finite Markov chain is completely described by two objects:

\[ \mu_0(i)=P(X_0=i), \qquad P_{ij}=P(X_{t+1}=j\mid X_t=i). \]

The initial distribution \(\mu_0\) determines how the chain starts. The transition matrix \(P\) determines how it evolves.

5.3.1 Row-vector convention

In this book, distributions are usually written as row vectors. If

\[ \mu_t(j)=P(X_t=j), \]

then

\[ \mu_{t+1}=\mu_t P. \]

Therefore

\[ \mu_t=\mu_0P^t. \]

Some probability and statistics books use column vectors instead. In that convention, the evolution is \(\mu_{t+1}=P^T\mu_t\). Both conventions are valid. The important thing is to stay consistent.

5.4 2.3 Multi-step transitions and the Chapman-Kolmogorov equations

The entry \((P^t)_{ij}\) gives the probability of moving from state \(i\) to state \(j\) in exactly \(t\) steps:

\[ (P^t)_{ij}=P(X_t=j\mid X_0=i). \]

This follows from the Chapman-Kolmogorov equations:

\[ P^{m+n}_{ij} = \sum_{k=1}^n P^m_{ik}P^n_{kj}. \]

The formula says that to move from \(i\) to \(j\) in \(m+n\) steps, the chain may pass through an intermediate state \(k\) after \(m\) steps.

5.4.1 Example: a three-state chain

Consider the transition matrix

\[ P= \begin{pmatrix} 0.85 & 0.10 & 0.05 \\ 0.20 & 0.65 & 0.15 \\ 0.10 & 0.25 & 0.65 \end{pmatrix}. \]

Starting from state \(1\), the initial distribution is

\[ \mu_0=(1,0,0). \]

The distribution after \(t\) steps is \(\mu_t=\mu_0P^t\).

The plot illustrates a central phenomenon: even though the chain starts at one state, the distribution often moves toward a stable long-run distribution.

5.4.2 Python example: distribution evolution

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

P = np.array([
    [0.85, 0.10, 0.05],
    [0.20, 0.65, 0.15],
    [0.10, 0.25, 0.65]
])

mu = np.array([1.0, 0.0, 0.0])
T = 25
history = [mu.copy()]

for t in range(T):
    mu = mu @ P
    history.append(mu.copy())

history = np.array(history)
df = pd.DataFrame(history, columns=["state 1", "state 2", "state 3"])
df.head()
state 1 state 2 state 3
0 1.000000 0.000000 0.000000
1 0.850000 0.100000 0.050000
2 0.747500 0.162500 0.090000
3 0.676875 0.202875 0.120250
4 0.627944 0.229619 0.142437

Distribution evolution for a finite Markov chain.

plt.figure(figsize=(7, 4))
for j in range(3):
    plt.plot(df.index, df.iloc[:, j], marker="o", markersize=3, label=df.columns[j])
plt.xlabel("time step")
plt.ylabel("probability")
plt.title("Distribution evolution: mu_t = mu_0 P^t")
plt.legend()
plt.grid(alpha=0.25)
plt.show()

5.5 2.4 The Markov operator viewpoint

There are two equivalent linear-algebraic ways to view a Markov chain.

5.5.1 Acting on distributions

A distribution \(\mu\) is a row vector. The transition matrix sends it to the next distribution:

\[ \mu \mapsto \mu P. \]

This is the forward evolution viewpoint.

5.5.2 Acting on functions

A real-valued function on states can be represented as a column vector

\[ f= \begin{pmatrix} f(1) \\ f(2) \\ \vdots \\ f(n) \end{pmatrix}. \]

The transition matrix acts on functions by conditional expectation:

\[ (Pf)(i)=\sum_{j=1}^n P_{ij}f(j) =E[f(X_{t+1})\mid X_t=i]. \]

More generally,

\[ (P^t f)(i)=E[f(X_t)\mid X_0=i]. \]

This operator viewpoint is essential for reinforcement learning. Bellman equations are equations involving conditional expectations. For example, if a reward \(r(i)\) is received at each state, then the discounted value function satisfies

\[ V(i)=r(i)+\gamma(PV)(i). \]

Thus

\[ V=r+\gamma PV. \]

This equation will be studied carefully in Chapter 3.

Distribution side versus function side. The row vector \(\mu\) evolves by \(\mu P\). The value function \(V\) is a column vector and appears in \(PV\). Mixing these two viewpoints is a common source of transposition errors.

5.6 2.5 Communicating classes

A state \(j\) is accessible from state \(i\) if there exists \(t\geq 0\) such that

\[ (P^t)_{ij}>0. \]

We write \(i\to j\).

States \(i\) and \(j\) communicate if

\[ i\to j \quad \text{and} \quad j\to i. \]

Communication is an equivalence relation. It partitions the state space into communicating classes.

A Markov chain is irreducible if all states communicate with one another. Equivalently, for every pair \(i,j\), there exists \(t\geq 0\) such that

\[ (P^t)_{ij}>0. \]

In graph language, create a directed graph with an edge \(i\to j\) whenever \(P_{ij}>0\). The chain is irreducible if this directed graph is strongly connected.

5.6.1 Why irreducibility matters in RL

Irreducibility says that the process can eventually move between all states. In reinforcement learning, this relates to exploration. If a policy never visits part of the state space, then data generated under that policy cannot estimate values or transitions there.

However, irreducibility is not always desirable. In a task with a terminal success state, the chain may intentionally become absorbing after success. The right structure depends on the problem.

5.7 2.6 Periodicity and aperiodicity

The period of a state \(i\) is

\[ d(i)=\gcd\{t\geq 1:(P^t)_{ii}>0\}. \]

If \(d(i)=1\), the state is aperiodic. If all states in an irreducible chain are aperiodic, the chain is called aperiodic.

A simple periodic example is

\[ P= \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}. \]

Starting from state \(1\), the chain alternates deterministically:

\[ 1,2,1,2,\ldots . \]

The distribution does not converge. It oscillates forever.

A small self-loop often breaks periodicity. For example,

\[ P= \begin{pmatrix} 0.1 & 0.9 \\ 0.8 & 0.2 \end{pmatrix} \]

is aperiodic because each state has a positive probability of remaining where it is.

Finite-state convergence principle. If a finite Markov chain is irreducible and aperiodic, then it has a unique stationary distribution \(\pi\), and for every initial distribution \(\mu_0\),

\[ \mu_0P^t\to \pi \qquad \text{as } t\to\infty. \]

This theorem is one of the most useful results in finite Markov chain theory. It explains why long simulations often forget their starting point.

5.8 2.7 Stationary distributions

A probability distribution \(\pi\) is stationary if

\[ \pi=\pi P, \qquad \sum_i\pi_i=1, \qquad \pi_i\geq 0. \]

If \(X_t\sim \pi\), then

\[ X_{t+1}\sim \pi. \]

So a stationary distribution is an equilibrium distribution for the Markov chain.

5.8.1 Linear algebra formulation

The equation \(\pi=\pi P\) can be written as

\[ \pi(P-I)=0. \]

Equivalently,

\[ P^T\pi^T=\pi^T. \]

Thus \(\pi^T\) is a right eigenvector of \(P^T\) with eigenvalue \(1\).

Since \(P\) is row-stochastic,

\[ P\mathbf{1}=\mathbf{1}, \]

so \(1\) is always a right eigenvalue of \(P\) with eigenvector \(\mathbf{1}\).

5.8.2 Example: two-state chain

Let

\[ P= \begin{pmatrix} 1-a & a \\ b & 1-b \end{pmatrix}, \qquad 0<a,b<1. \]

Let \(\pi=(\pi_1,\pi_2)\). The stationary equations are

\[ \pi_1=\pi_1(1-a)+\pi_2b, \]

\[ \pi_2=\pi_1a+\pi_2(1-b), \]

and

\[ \pi_1+\pi_2=1. \]

The first equation gives

\[ \pi_1a=\pi_2b. \]

Therefore

\[ \pi_1=\frac{b}{a+b}, \qquad \pi_2=\frac{a}{a+b}. \]

This formula is worth remembering. The long-run mass on state \(1\) is proportional to the probability of returning from state \(2\) to state \(1\).

5.8.3 Python example: stationary distribution

The following function computes a stationary distribution by solving a linear system. The equation \(\pi P=\pi\) is singular, so we replace one equation by the normalization condition \(\sum_i\pi_i=1\).

import numpy as np

def stationary_distribution(P):
    """Return one stationary distribution of a finite Markov chain.

    P is assumed to be a row-stochastic matrix.
    """
    P = np.asarray(P, dtype=float)
    n = P.shape[0]

    A = P.T - np.eye(n)
    b = np.zeros(n)

    # Replace the last equation by sum_i pi_i = 1.
    A[-1, :] = 1.0
    b[-1] = 1.0

    return np.linalg.solve(A, b)

P = np.array([
    [0.85, 0.10, 0.05],
    [0.20, 0.65, 0.15],
    [0.10, 0.25, 0.65]
])

pi = stationary_distribution(P)
print(pi)
print(pi @ P)
[0.51515152 0.28787879 0.1969697 ]
[0.51515152 0.28787879 0.1969697 ]

The two printed vectors should agree up to numerical roundoff.

5.9 2.8 Recurrence, transience, and finite chains

Let

\[ T_i^+=\inf\{t\geq 1:X_t=i\} \]

be the first return time to state \(i\).

State \(i\) is recurrent if

\[ P_i(T_i^+<\infty)=1. \]

State \(i\) is transient if

\[ P_i(T_i^+<\infty)<1. \]

For a finite irreducible chain, every state is recurrent. This fact is useful because finite irreducible Markov chains cannot permanently lose probability mass to infinity.

A recurrent state \(i\) is positive recurrent if

\[ E_i[T_i^+]<\infty. \]

In finite irreducible chains, all states are positive recurrent.

Return-time formula. For a finite irreducible Markov chain with stationary distribution \(\pi\),

\[ E_i[T_i^+]=\frac{1}{\pi_i}. \]

A state with large stationary probability is visited frequently. A state with small stationary probability has a long expected return time.

In reinforcement learning, this connects to data imbalance. States with small \(\pi_i\) under the behavior policy are rarely observed, so their value estimates may have high variance.

5.10 2.9 Absorbing chains

A state \(i\) is absorbing if

\[ P_{ii}=1. \]

Once the chain enters an absorbing state, it never leaves. Many episodic reinforcement-learning tasks have absorbing terminal states such as success, failure, or timeout.

Suppose the states are ordered so that transient states come first and absorbing states come last. Then the transition matrix has canonical form

\[ P= \begin{pmatrix} Q & R \\ 0 & I \end{pmatrix}. \]

Here:

  • \(Q\) describes transitions among transient states;
  • \(R\) describes transitions from transient states to absorbing states;
  • \(I\) describes staying in absorbing states.

The fundamental matrix is

\[ N=(I-Q)^{-1}. \]

The entry \(N_{ij}\) has a probabilistic interpretation:

\[ N_{ij}=E_i\left[\text{number of visits to transient state }j\text{ before absorption}\right]. \]

The matrix of absorption probabilities is

\[ B=NR. \]

The expected time to absorption from each transient state is

\[ t=N\mathbf{1}. \]

5.10.1 Python example: absorbing-chain calculations

import numpy as np
import pandas as pd

Q = np.array([
    [0.20, 0.50, 0.00],
    [0.10, 0.30, 0.40],
    [0.00, 0.20, 0.20]
])

R = np.array([
    [0.30, 0.00],
    [0.00, 0.20],
    [0.10, 0.50]
])

I = np.eye(Q.shape[0])
N = np.linalg.inv(I - Q)
B = N @ R
t_absorb = N @ np.ones(Q.shape[0])

print("Fundamental matrix N:")
print(pd.DataFrame(N, index=["T1", "T2", "T3"], columns=["T1", "T2", "T3"]))

print("\nAbsorption probabilities B:")
print(pd.DataFrame(B, index=["T1", "T2", "T3"], columns=["Absorb A", "Absorb B"]))

print("\nExpected time to absorption:")
print(pd.Series(t_absorb, index=["T1", "T2", "T3"]))
Fundamental matrix N:
          T1        T2        T3
T1  1.395349  1.162791  0.581395
T2  0.232558  1.860465  0.930233
T3  0.058140  0.465116  1.482558

Absorption probabilities B:
    Absorb A  Absorb B
T1  0.476744  0.523256
T2  0.162791  0.837209
T3  0.165698  0.834302

Expected time to absorption:
T1    3.139535
T2    3.023256
T3    2.005814
dtype: float64

5.11 2.10 Reversibility and detailed balance

A Markov chain with stationary distribution \(\pi\) is reversible if

\[ \pi_iP_{ij}=\pi_jP_{ji} \qquad \text{for all } i,j. \]

These equations are called detailed balance equations. They say that, in equilibrium, the probability flow from \(i\) to \(j\) equals the probability flow from \(j\) to \(i\).

Detailed balance implies stationarity. Indeed,

\[ (\pi P)_j =\sum_i\pi_iP_{ij} =\sum_i\pi_jP_{ji} =\pi_j\sum_iP_{ji}. \]

In the last expression, the index \(j\) is fixed and \(i\) ranges over the second index of row \(j\). Since rows of \(P\) sum to one,

\[ \sum_iP_{ji}=1. \]

Therefore

\[ (\pi P)_j=\pi_j. \]

So \(\pi P=\pi\).

Index warning. The expression \(P_{ij}\) means ``from \(i\) to \(j\)’’ in this book. Some references use the transpose convention. When checking detailed balance, always verify the final equation \(\pi P=\pi\).

Reversibility is especially important in Markov chain Monte Carlo. Although MCMC is not the main topic of this book, it gives useful intuition: one can design a transition rule so that the long-run distribution is a desired target distribution.

5.12 2.11 Mixing, eigenvalues, and spectral gaps

For an irreducible aperiodic finite chain, \(\mu_0P^t\to\pi\). A natural quantitative question is: how fast?

One way to measure distance between distributions is total variation distance:

\[ \|\mu-\nu\|_{TV} =\frac{1}{2}\sum_i |\mu_i-\nu_i|. \]

The mixing time at accuracy \(\epsilon\) is often defined as

\[ t_{mix}(\epsilon) = \min\left\{t: \max_i \|e_iP^t-\pi\|_{TV}\leq \epsilon\right\}. \]

Here \(e_i\) is the point mass at state \(i\).

5.12.1 Spectral intuition

If \(P\) is diagonalizable and has eigenvalues

\[ 1=\lambda_1, \lambda_2,\ldots,\lambda_n, \]

then components associated with \(|\lambda_k|<1\) decay approximately like

\[ |\lambda_k|^t. \]

The largest nontrivial magnitude

\[ \lambda_* = \max_{k\geq 2}|\lambda_k| \]

controls the slowest asymptotic mode. The quantity

\[ 1-\lambda_* \]

is called a spectral gap in many settings. A larger gap generally means faster convergence.

This idea reappears later in policy evaluation. The Bellman operator for discounted problems contracts by a factor \(\gamma\), so a large \(\gamma\) creates slow convergence in much the same way that a large \(|\lambda_2|\) slows Markov-chain mixing.

5.13 2.12 Occupancy measures and discounted visitation

Let a fixed policy \(\pi\) induce a transition matrix \(P_{\pi}\). Suppose the initial state distribution is \(\mu_0\). The distribution at time \(t\) is

\[ \mu_t=\mu_0P_{\pi}^t. \]

The discounted state occupancy distribution is

\[ d_{\pi,\gamma} =(1-\gamma)\sum_{t=0}^{\infty}\gamma^t\mu_0P_{\pi}^t. \]

When \(0<\gamma<1\), the geometric series gives

\[ d_{\pi,\gamma} =(1-\gamma)\mu_0(I-\gamma P_{\pi})^{-1}. \]

This distribution tells us which states are emphasized by a discounted reinforcement-learning objective. As \(\gamma\) approaches \(1\), the process cares more about later times. Under suitable ergodicity assumptions, \(d_{\pi,\gamma}\) approaches the stationary distribution of \(P_{\pi}\).

The state-action occupancy measure is

\[ d_{\pi,\gamma}(s,a)=d_{\pi,\gamma}(s)\pi(a\mid s). \]

Policy gradient theory, off-policy evaluation, and offline reinforcement learning all depend on occupancy measures.

Discounted resolvent identity. If \(P\) is row-stochastic and \(0<\gamma<1\), then

\[ I-\gamma P \]

is invertible and

\[ (I-\gamma P)^{-1}=\sum_{t=0}^{\infty}\gamma^tP^t. \]

This is the matrix version of the geometric series.

5.13.1 Python example: discounted occupancy measure

import numpy as np

P_pi = np.array([
    [0.85, 0.10, 0.05],
    [0.20, 0.65, 0.15],
    [0.10, 0.25, 0.65]
])

mu0 = np.array([1.0, 0.0, 0.0])
gamma = 0.95

I = np.eye(P_pi.shape[0])
d = (1 - gamma) * mu0 @ np.linalg.inv(I - gamma * P_pi)

print(d)
print("sum =", d.sum())
[0.58639119 0.24828912 0.16531969]
sum = 1.0000000000000002

The vector sums to one. This normalization is the reason for the factor \((1-\gamma)\).

5.14 2.13 Estimating transition matrices from data

In many reinforcement-learning problems, the transition matrix is unknown. We observe a trajectory

\[ X_0,X_1,\ldots,X_T \]

and estimate

\[ P_{ij}=P(X_{t+1}=j\mid X_t=i). \]

The maximum likelihood estimator is the empirical transition frequency:

\[ \widehat P_{ij} =\frac{N_{ij}}{N_i}, \]

where

\[ N_{ij}=\sum_{t=0}^{T-1}1\{X_t=i,X_{t+1}=j\}, \]

and

\[ N_i=\sum_jN_{ij}. \]

If \(N_i=0\), then the data contain no transitions out of state \(i\). This is a statistical warning: the corresponding row cannot be estimated from the trajectory.

5.14.1 Smoothing

A simple Bayesian or regularized estimate uses pseudocounts:

\[ \widehat P_{ij} =\frac{N_{ij}+\alpha}{N_i+n\alpha}, \qquad \alpha>0. \]

This is equivalent to using a symmetric Dirichlet prior on each row. The larger \(\alpha\) is, the more the estimate is pulled toward the uniform distribution.

5.14.2 Python example: estimating a transition matrix

import numpy as np

rng = np.random.default_rng(7243)

P_true = np.array([
    [0.85, 0.10, 0.05],
    [0.20, 0.65, 0.15],
    [0.10, 0.25, 0.65]
])

n_states = P_true.shape[0]
T = 5000
x = np.zeros(T + 1, dtype=int)

for t in range(T):
    x[t + 1] = rng.choice(n_states, p=P_true[x[t]])

counts = np.zeros((n_states, n_states), dtype=int)
for t in range(T):
    counts[x[t], x[t + 1]] += 1

row_counts = counts.sum(axis=1, keepdims=True)
P_hat = counts / row_counts

print("True P:")
print(np.round(P_true, 3))
print("\nEstimated P:")
print(np.round(P_hat, 3))
True P:
[[0.85 0.1  0.05]
 [0.2  0.65 0.15]
 [0.1  0.25 0.65]]

Estimated P:
[[0.855 0.093 0.052]
 [0.195 0.67  0.135]
 [0.103 0.245 0.652]]

This example illustrates two statistical issues:

  1. more frequently visited states have more reliable transition estimates;
  2. transition estimates are dependent because the observations come from one trajectory, not independent samples.

5.15 2.14 Markov chains induced by policies

Now return to reinforcement learning. Let

\[ P(s'\mid s,a) \]

be the environment transition probability and let \(\pi(a\mid s)\) be a fixed policy. Then the state process has transition matrix

\[ P_{\pi}(s,s')=\sum_a\pi(a\mid s)P(s'\mid s,a). \]

The expected one-step reward under \(\pi\) is

\[ r_{\pi}(s)=\sum_a\pi(a\mid s)r(s,a). \]

The pair

\[ (P_{\pi}, r_{\pi}) \]

defines a Markov reward process. This is the subject of Chapter 3.

5.15.1 Small finite example

Suppose there are two states and two actions. The action-specific transition matrices are

\[ P^{(0)}= \begin{pmatrix} 0.9 & 0.1 \\ 0.4 & 0.6 \end{pmatrix}, \qquad P^{(1)}= \begin{pmatrix} 0.2 & 0.8 \\ 0.1 & 0.9 \end{pmatrix}. \]

Let the policy be

\[ \pi(0\mid 1)=0.7, \qquad \pi(1\mid 1)=0.3, \]

and

\[ \pi(0\mid 2)=0.4, \qquad \pi(1\mid 2)=0.6. \]

Then

\[ P_{\pi}(1,\cdot)=0.7P^{(0)}(1,\cdot)+0.3P^{(1)}(1,\cdot), \]

and

\[ P_{\pi}(2,\cdot)=0.4P^{(0)}(2,\cdot)+0.6P^{(1)}(2,\cdot). \]

The policy averages the transition laws action by action and state by state.

import numpy as np

P0 = np.array([
    [0.9, 0.1],
    [0.4, 0.6]
])

P1 = np.array([
    [0.2, 0.8],
    [0.1, 0.9]
])

# pi[s, a]
pi_policy = np.array([
    [0.7, 0.3],
    [0.4, 0.6]
])

P_pi = np.zeros_like(P0)
for s in range(2):
    P_pi[s, :] = pi_policy[s, 0] * P0[s, :] + pi_policy[s, 1] * P1[s, :]

print(P_pi)
print("row sums:", P_pi.sum(axis=1))
[[0.69 0.31]
 [0.22 0.78]]
row sums: [1. 1.]

5.16 2.15 AI component: Markov state abstractions and generative simulation

Modern AI systems often interact with environments whose true state is high-dimensional. Examples include:

  • a robot observing images and joint angles;
  • an autonomous system observing sensor streams;
  • a language model interacting with a user over a long conversation;
  • a recommendation system observing user histories;
  • a scientific AI system choosing experiments adaptively.

In such systems, the raw observation may not be Markov. A useful state representation should summarize the past well enough that the future is predictable from the present. Mathematically, a representation map

\[ z_t=\phi(H_t) \]

turns a history

\[ H_t=(O_0,A_0,O_1,A_1,\ldots,O_t) \]

into a state-like variable \(z_t\). The hope is that

\[ P(z_{t+1}\mid H_t,A_t)\approx P(z_{t+1}\mid z_t,A_t). \]

This is a central idea behind representation learning for reinforcement learning.

5.16.1 AI-assisted modeling exercise

Use an AI assistant as a modeling partner, not as an authority. Ask it to propose a Markov state representation for one of the following tasks:

  1. adaptive tutoring for a mathematics course;
  2. personalized review scheduling for statistics students;
  3. robotic navigation in a building;
  4. dynamic pricing for a service;
  5. treatment planning in a simplified medical decision process.

For each proposed state variable, check the following mathematical question:

\[ P(S_{t+1}\mid S_t,A_t,H_{t-1}) \stackrel{?}{=} P(S_{t+1}\mid S_t,A_t). \]

If the equality is not plausible, identify which historical variables are missing.

AI caution. Language models are good at generating candidate state variables, but they may ignore hidden confounding, delayed effects, censoring, and nonstationarity. Always translate AI suggestions into explicit conditional probability statements.

5.16.2 Generative simulation perspective

A transition matrix is a simple generative model. Given the current state \(i\), the model samples the next state according to the categorical distribution

\[ X_{t+1}\sim \operatorname{Categorical}(P_{i1},\ldots,P_{in}). \]

In modern AI, a learned world model plays a similar role, but with a much larger state space:

\[ \widehat P_{\theta}(s'\mid s,a). \]

Planning with a learned transition model is sometimes called model-based reinforcement learning. The finite Markov-chain theory in this chapter is the clean mathematical prototype.

5.17 2.16 Common mistakes

5.17.1 Mistake 1: Confusing \(P_{ij}\) with \(P_{ji}\)

The convention here is

\[ P_{ij}=P(X_{t+1}=j\mid X_t=i). \]

Rows represent current states. Columns represent next states.

5.17.2 Mistake 2: Assuming \(P^t\) converges for every chain

If the chain is periodic, \(P^t\) may not converge. Irreducibility alone is not enough. Aperiodicity is also needed.

5.17.3 Mistake 3: Estimating transitions for unvisited states

If a trajectory never visits state \(i\), there is no empirical information about row \(i\) of \(P\). This becomes a major issue in offline reinforcement learning.

5.17.4 Mistake 4: Treating one trajectory as independent data

The sequence \(X_0,X_1,\ldots,X_T\) is dependent. Standard independent-sample intuition must be modified.

5.17.5 Mistake 5: Forgetting that a policy changes the chain

Different policies induce different matrices \(P_{\pi}\). A value function is always attached to the dynamics generated by a particular policy.

5.18 2.17 Summary

A finite Markov chain is specified by an initial distribution and a row-stochastic transition matrix. The distribution evolves by

\[ \mu_t=\mu_0P^t. \]

The same matrix acts on functions by conditional expectation:

\[ (Pf)(i)=E[f(X_{t+1})\mid X_t=i]. \]

Stationary distributions satisfy

\[ \pi=\pi P. \]

If the finite chain is irreducible and aperiodic, then the distribution converges to the unique stationary distribution. Absorbing chains have a special block form, and the fundamental matrix

\[ N=(I-Q)^{-1} \]

computes expected visits, absorption probabilities, and expected absorption times.

For reinforcement learning, the key identity is

\[ P_{\pi}(s,s')=\sum_a\pi(a\mid s)P(s'\mid s,a). \]

Once a policy is fixed, the state process is a Markov chain. Chapter 3 adds rewards to this chain and derives the first Bellman equations.

5.19 Exercises

5.19.1 Conceptual exercises

  1. Explain the Markov property in words. Give one example where the property is plausible and one example where it fails.

  2. Let \(P\) be a row-stochastic matrix. Prove that \(P\mathbf{1}=\mathbf{1}\).

  3. Suppose \(\mu_{t+1}=\mu_tP\). Prove by induction that \(\mu_t=\mu_0P^t\).

  4. Explain why a deterministic cycle of length \(3\) is periodic.

  5. Explain why a self-loop \(P_{ii}>0\) can help remove periodicity.

  6. In an RL problem, why does fixing a policy turn the controlled system into a Markov chain?

5.19.2 Mathematical exercises

  1. For

\[ P= \begin{pmatrix} 0.8 & 0.2 \\ 0.3 & 0.7 \end{pmatrix}, \]

find the stationary distribution by solving \(\pi=\pi P\).

  1. For the two-state matrix

\[ P= \begin{pmatrix} 1-a & a \\ b & 1-b \end{pmatrix}, \]

derive

\[ \pi=\left(\frac{b}{a+b},\frac{a}{a+b}\right). \]

  1. Let

\[ P= \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}. \]

Compute \(P^t\) for even and odd \(t\). Does \(P^t\) converge?

  1. Let

\[ P= \begin{pmatrix} 0.5 & 0.5 \\ 0.2 & 0.8 \end{pmatrix}. \]

Compute \(P^2\) and interpret the entry \((P^2)_{12}\).

  1. For an absorbing chain with

\[ Q= \begin{pmatrix} 0.3 & 0.4 \\ 0.2 & 0.3 \end{pmatrix}, \qquad R= \begin{pmatrix} 0.3 \\ 0.5 \end{pmatrix}, \]

compute \(N=(I-Q)^{-1}\) and the expected absorption times.

  1. Prove the discounted resolvent identity

\[ (I-\gamma P)^{-1}=\sum_{t=0}^{\infty}\gamma^tP^t \]

for \(0<\gamma<1\).

5.19.3 Computational exercises

  1. Simulate a Markov chain with transition matrix

\[ P= \begin{pmatrix} 0.7 & 0.2 & 0.1 \\ 0.1 & 0.8 & 0.1 \\ 0.2 & 0.3 & 0.5 \end{pmatrix}. \]

Estimate the stationary distribution from a long trajectory and compare it to the solution of \(\pi=\pi P\).

  1. Estimate a transition matrix from simulated data for several trajectory lengths \(T\). Plot the Frobenius norm error

\[ \|\widehat P-P\|_F \]

as a function of \(T\).

  1. Choose two different policies in a small MDP and compute the two induced transition matrices \(P_{\pi_1}\) and \(P_{\pi_2}\). Compare their stationary distributions.

  2. Construct a periodic Markov chain and show numerically that \(\mu_t\) oscillates. Then add a small self-loop probability and show that the modified chain converges.

  3. Build an absorbing random walk on the states \(\{0,1,2,3,4\}\), where \(0\) and \(4\) are absorbing. Compute the probability of absorption at \(4\) starting from each transient state.

5.19.4 AI-assisted exercises

  1. Ask an AI assistant to propose a state representation for an adaptive tutoring system. Translate the proposal into a conditional probability statement and critique whether it is plausibly Markov.

  2. Ask an AI assistant to generate a transition matrix for a three-state learning model with states confused,''developing,’’ and ``mastered.’’ Check whether the matrix is row-stochastic, irreducible, and aperiodic.

  3. Ask an AI assistant to explain the difference between a stationary distribution and a limiting distribution. Verify the explanation using the two-state periodic chain.

5.20 Notes for instructors

For MA Applied Math students, emphasize matrix powers, invariant subspaces, eigenvalues, fixed points, and resolvent identities. For MS Statistics students, emphasize conditional probability, empirical transition estimation, dependence in trajectories, stationary distributions as long-run frequencies, and uncertainty caused by rare states.

A good 75-minute lecture structure is:

  1. Markov property and transition matrix: 15 minutes;
  2. distribution evolution and conditional expectation operator: 15 minutes;
  3. stationary distributions and convergence: 20 minutes;
  4. absorbing chains and fundamental matrix: 15 minutes;
  5. connection to fixed policies in RL: 10 minutes.

For a computational recitation, focus on estimating \(P\) from one simulated trajectory and comparing empirical frequencies to stationary probabilities.

5.21 References

Classical Markov-chain material can be found in standard probability texts. For the reinforcement-learning connection, see (sutton2018reinforcement?), (puterman1994markov?), (bertsekas2012dynamic?), and (szepesvari2010algorithms?). The role of policy-induced Markov chains becomes central in the Markov reward process and Markov decision process chapters that follow.