Simple Algorithm
Reinforcement Learning · Bootstrapping · 22 min

Markov Decision Processes

States, actions and transition probabilities: the mathematical model of sequential decisions.

A recycling robot must decide, again and again, whether to keep searching for cans, wait for someone to bring one, or go back to recharge, never sure whether its battery will last. A Markov decision process is the model that makes such decisions precise: states, actions, transition probabilities, rewards and a discount. Nearly every method in this course is an algorithm for an MDP, or a way around one of its assumptions.

What you’ll learn

  • Define a finite MDP (S,A,P,r,γ,μ)(\mathcal S, \mathcal A, P, r, \gamma, \mu) and test whether a proposed state description has the Markov property.
  • Model a problem as an MDP, using Sutton and Barto's recycling robot, and write its dynamics as a table and as arrays.
  • Compute trajectory probabilities, and the matrix PπP^\pi and vector rπr^\pi that a stochastic policy induces.
  • Classify MDPs (finite, countable or continuous; deterministic or stochastic) and recognize where real problems break the assumptions.

The model

The defining assumption is that the current state carries everything needed to predict the consequences of an action, the Markov property:

Pr⁡(St+1=s′,Rt+1=r ∣ St,At,St−1,At−1,…,S0)=Pr⁡(St+1=s′,Rt+1=r ∣ St,At).\Pr\big(S_{t+1} = s', R_{t+1} = r \,\big|\, S_t, A_t, S_{t-1}, A_{t-1}, \dots, S_0\big) = \Pr\big(S_{t+1} = s', R_{t+1} = r \,\big|\, S_t, A_t\big).

It is a property of the state description you choose, not of the world. The same system can be Markov in one description and not in another: a ball's position alone is not Markov, since two balls in the same place can be moving in opposite directions, but its position and velocity together are, for a ball moving under gravity.

Check your understanding

Is the arrangement of the pieces on a chessboard a Markov state for chess?

  1. Yes: the positions of the pieces determine everything
  2. Almost: it must also record whose turn it is, the castling and en passant rights, and enough history for the repetition and fifty-move rules
  3. No: chess can only be modeled with the full list of moves played
  4. It depends on how strong the opponent is
Show the answer

B. Almost: it must also record whose turn it is, the castling and en passant rights, and enough history for the repetition and fifty-move rules

The legal moves and the outcome depend on information the arrangement does not show: the side to move, whether each king and rook has moved (castling), the previous move (en passant), and counts for the repetition and fifty-move draw rules. Add those and the state is Markov. Standard position encodings such as FEN record all of this except the repetition history.

A worked example: the recycling robot

Sutton and Barto's recycling robot (Example 3.3) collects empty cans in an office. Its battery is high or low. In either state it can search for cans or wait for someone to bring one, and when low it can also recharge. Searching earns rsearchr_{\text{search}} and waiting earns rwait<rsearchr_{\text{wait}} < r_{\text{search}}, but searching drains the battery: from high it stays high with probability α\alpha, and from low it stays low with probability β\beta. Otherwise a low battery runs flat, the robot must be rescued (reward −3-3), and it is put back on the charger, high.

ssaas′s'P(s′∣s,a)P(s' \mid s, a)r(s,a,s′)r(s,a,s')
highsearchhighα\alpharsearchr_{\text{search}}
highsearchlow1−α1-\alpharsearchr_{\text{search}}
lowsearchhigh1−β1-\beta−3-3
lowsearchlowβ\betarsearchr_{\text{search}}
highwaithigh11rwaitr_{\text{wait}}
lowwaitlow11rwaitr_{\text{wait}}
lowrechargehigh1100
The transition graph as a table, with the zero-probability rows left out. With α=0.8\alpha = 0.8, β=0.6\beta = 0.6, rsearch=2r_{\text{search}} = 2 and rwait=1r_{\text{wait}} = 1, searching when low has expected reward r(low,search)=0.4⋅(−3)+0.6⋅2=0r(\text{low}, \text{search}) = 0.4 \cdot (-3) + 0.6 \cdot 2 = 0.
python
import numpy as np

HIGH, LOW = 0, 1
SEARCH, WAIT, RECHARGE = 0, 1, 2
alpha, beta, r_search, r_wait = 0.8, 0.6, 2.0, 1.0

P = np.zeros((2, 3, 2))                  # P[s, a, s']
R = np.zeros((2, 3, 2))                  # R[s, a, s']: reward of that transition
P[HIGH, SEARCH] = [alpha, 1 - alpha];  R[HIGH, SEARCH] = r_search
P[LOW, SEARCH] = [1 - beta, beta];     R[LOW, SEARCH] = [-3.0, r_search]  # flat battery: rescued
P[HIGH, WAIT] = [1.0, 0.0];            R[HIGH, WAIT] = r_wait
P[LOW, WAIT] = [0.0, 1.0];             R[LOW, WAIT] = r_wait
P[LOW, RECHARGE] = [1.0, 0.0]          # recharging exists only in LOW and pays 0
r = (P * R).sum(axis=2)                  # r(s, a) = sum over s' of P(s'|s,a) r(s,a,s')

pi = np.array([[0.9, 0.1, 0.0],          # HIGH: search 90%, wait 10%, never recharge
               [0.3, 0.2, 0.5]])         # LOW: search, wait, recharge
P_pi = np.einsum("sa,sat->st", pi, P)    # P^pi(s, s') = sum over a of pi(a|s) P(s'|s,a)
r_pi = (pi * r).sum(axis=1)              # r^pi(s) = sum over a of pi(a|s) r(s, a)

The same MDP as arrays, plus the matrix PπP^\pi and the vector rπr^\pi of a stochastic policy (the next step derives them). Here PπP^\pi has rows (0.82,0.18)(0.82, 0.18) and (0.62,0.38)(0.62, 0.38), and rπ=(1.9,0.2)r^\pi = (1.9, 0.2). The row P[HIGH, RECHARGE] stays zero, which is harmless because the policy never picks that action.

Predict before you look

With α=0.8\alpha = 0.8, the robot always searches when its battery is high and recharges when it is low. In the long run, what fraction of the time is the battery high?

Show the answer

0.83

The induced chain moves from high to low with probability 0.2 and from low to high with probability 1. Its stationary distribution balances the two flows, Pr⁡(high)⋅0.2=Pr⁡(low)⋅1\Pr(\text{high}) \cdot 0.2 = \Pr(\text{low}) \cdot 1, so Pr⁡(high)=1/1.2≈0.83\Pr(\text{high}) = 1/1.2 \approx 0.83; in general it is 1/(2−α)1/(2-\alpha).

Policies, trajectories and the chain a policy induces

A deterministic policy picks one action per state, a=π(s)a = \pi(s); a stochastic policy gives a distribution π(a∣s)\pi(a \mid s). A policy and an MDP together define a distribution over trajectories τ=(S0,A0,S1,A1,… )\tau = (S_0, A_0, S_1, A_1, \dots), with each reward drawn given its transition:

pπ(τ)=μ(S0) ∏t≥0π(At∣St) P(St+1∣St,At)p_\pi(\tau) = \mu(S_0)\,\prod_{t \ge 0}\pi(A_t \mid S_t)\,P(S_{t+1} \mid S_t, A_t)
For a trajectory prefix of length TT the product runs over t<Tt < T. Every expectation in RL, from values to policy gradients, is an expectation under pπp_\pi.
DerivationThe chain a policy induces
  1. Pπ(s,s′)=∑aπ(a∣s) P(s′∣s,a),rπ(s)=∑aπ(a∣s) r(s,a)P^\pi(s, s') = \sum_a \pi(a \mid s)\,P(s' \mid s, a),\qquad r^\pi(s) = \sum_a \pi(a \mid s)\,r(s,a)

    Averaging over the action turns the MDP into a Markov chain on states, with one expected reward per state:

  2. Pr⁡π(St=s′)=(μ⊤(Pπ)t)(s′),Eπ[Rt+1]=μ⊤(Pπ)t rπ\Pr_\pi(S_t = s') = \big(\mu^\top (P^\pi)^t\big)(s'),\qquad \E_\pi[R_{t+1}] = \mu^\top (P^\pi)^t\, r^\pi

    The distribution of StS_t is the initial distribution pushed through tt steps of the chain, so the expected reward at time tt is linear in rπr^\pi:

  3. J(π)=∑t≥0γt μ⊤(Pπ)t rπ=μ⊤(I−γPπ)−1rπJ(\pi) = \sum_{t\ge0}\gamma^t\,\mu^\top (P^\pi)^t\, r^\pi = \mu^\top\big(I - \gamma P^\pi\big)^{-1} r^\pi

    Summing with weights γt\gamma^t gives the expected return in one linear-algebra expression. The series converges because PπP^\pi is a stochastic matrix and γ<1\gamma < 1; Value Functions & the Bellman Equations derives the same formula from the Bellman equation.

Check your understanding

The robot starts high and follows the stochastic policy of the code above (search 90% and wait 10% when high). What is the probability that it first searches and stays high, and then searches and becomes low?

  1. 0.14760.1476
  2. 0.160.16
  3. 0.12960.1296
  4. 0.1440.144
Show the answer

C. 0.12960.1296

Multiply the policy and transition factors along the trajectory: π(search∣high)⋅α⋅π(search∣high)⋅(1−α)=0.9⋅0.8⋅0.9⋅0.2=0.1296\pi(\text{search} \mid \text{high})\cdot\alpha\cdot\pi(\text{search} \mid \text{high})\cdot(1-\alpha) = 0.9 \cdot 0.8 \cdot 0.9 \cdot 0.2 = 0.1296.

Noise, and what counts as an MDP

The finite, fully observed MDP is the cleanest case, and the one this topic's theory is built on. Real problems stretch it in three directions: more states, continuous states and actions, and states the agent cannot see.

ProblemStatesActionsDynamics
Recycling robot22 or 3Stochastic
4×3 gridworld11 cells, 2 of them exits4Stochastic (slips)
Inventory or queue controlCountably many: stock level, queue lengthA few order sizes or service ratesStochastic demand
BackgammonAbout 102010^{20} positionsLegal movesStochastic (dice)
GoAbout 2×101702 \times 10^{170} legal positionsUp to 361 moves, plus passingDeterministic rules; the opponent's replies make it a game
Cart-pole4 continuous variables: position, velocity, angle, angular velocity2: push left or rightDeterministic physics
Legged robotDozens of continuous joint angles and velocities, plus sensor readingsContinuous joint torquesPhysics plus noise, partly observed
Finite MDPs have transition matrices; continuous ones have transition densities or kernels, and sums become integrals. Huge or continuous state spaces need function approximation (Function Approximation & Deep Q-Learning).

The Markov property is the assumption broken most often. A single Atari frame does not show which way the ball is moving, a robot's camera does not show what is behind it, and the meaning of a sentence in a dialogue depends on what was said turns ago. When the agent receives observations rather than states, the problem is a partially observable MDP, and agents cope by building an agent state from the history: stacking recent frames, running a recurrent network, or maintaining a belief.

Key takeaways

  • An MDP (S,A,P,r,γ,μ)(\mathcal S, \mathcal A, P, r, \gamma, \mu) separates what the agent controls (actions), what the world does (transition probabilities) and what is wanted (rewards and γ\gamma).
  • The Markov property belongs to the chosen state description: the state must include whatever about the past still matters for the future.
  • A policy turns the MDP into a Markov chain with Pπ(s,s′)=∑aπ(a∣s)P(s′∣s,a)P^\pi(s,s') = \sum_a\pi(a \mid s)P(s' \mid s,a) and rewards rπr^\pi; trajectory probabilities multiply policy and transition factors.
  • Stochastic dynamics change optimal behavior: when moves can slip, agents avoid risky shortcuts.
  • Real problems stretch the finite, fully observed MDP: huge or continuous spaces need function approximation, and partial observability needs an agent state.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

How many deterministic stationary policies does the recycling robot have?

  1. 5
  2. 6
  3. 8
  4. 9
Show the answer

B. 6

A deterministic stationary policy picks one allowed action in each state: 2 choices when high and 3 when low, so 2×3=62 \times 3 = 6. In general there are ∏s∣A(s)∣\prod_s \abs{\mathcal A(s)} of them, or ASA^S when every state allows all AA actions.

Question 2

In state ss a policy takes a1a_1 with probability 0.25 and a2a_2 with probability 0.75. Action a1a_1 moves to s′s' with probability 0.4, and a2a_2 with probability 0.8. What is Pπ(s,s′)P^\pi(s, s')?

  1. 0.6
  2. 0.32
  3. 1.2
  4. 0.7
Show the answer

D. 0.7

Pπ(s,s′)=∑aπ(a∣s)P(s′∣s,a)=0.25⋅0.4+0.75⋅0.8=0.1+0.6=0.7P^\pi(s,s') = \sum_a \pi(a \mid s)P(s' \mid s,a) = 0.25 \cdot 0.4 + 0.75 \cdot 0.8 = 0.1 + 0.6 = 0.7.

Question 3

A self-driving car's state records the position of every vehicle nearby but not their velocities. Is it Markov, and what is the simplest fix?

  1. It is not: two scenes with identical positions can evolve differently. Adding the velocities, or the previous snapshot of positions, fixes it
  2. It is Markov, because the positions determine the future
  3. It is not, and only the full history of the drive can fix it
  4. It is not, because other drivers behave randomly
Show the answer

A. It is not: two scenes with identical positions can evolve differently. Adding the velocities, or the previous snapshot of positions, fixes it

Velocities determine where the vehicles go next, so they are information about the past that still matters. Two consecutive snapshots of positions recover the velocities approximately, the same reason Atari agents stack several frames.

Question 4

Which of these does the trajectory distribution pπ(τ)p_\pi(\tau) not depend on?

  1. The initial distribution μ\mu
  2. The policy π\pi
  3. The transition probabilities PP
  4. The discount factor γ\gamma
Show the answer

D. The discount factor γ\gamma

pπ(τ)=μ(S0)∏tπ(At∣St) P(St+1∣St,At)p_\pi(\tau) = \mu(S_0)\prod_t\pi(A_t \mid S_t)\,P(S_{t+1} \mid S_t, A_t) describes what happens, not how it is scored. The rewards and γ\gamma enter only when trajectories are turned into returns.

Go deeper

Further reading

  • R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 3 (MIT Press, 2nd ed., 2018).
  • M. L. Puterman, Markov Decision Processes: Discrete Stochastic Dynamic Programming, ch. 2–4 (Wiley, 1994).
  • R. Bellman, "A Markovian decision process," Journal of Mathematics and Mechanics 6(5), 679–684 (1957).