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 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 and vector 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:
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.
Is the arrangement of the pieces on a chessboard a Markov state for chess?
- Yes: the positions of the pieces determine everything
- 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
- No: chess can only be modeled with the full list of moves played
- 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 and waiting earns , but searching drains the battery: from high it stays high with probability , and from low it stays low with probability . Otherwise a low battery runs flat, the robot must be rescued (reward ), and it is put back on the charger, high.
| high | search | high | ||
| high | search | low | ||
| low | search | high | ||
| low | search | low | ||
| high | wait | high | ||
| low | wait | low | ||
| low | recharge | high |
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 and the vector of a stochastic policy (the next step derives them). Here has rows and , and . The row P[HIGH, RECHARGE] stays zero, which is harmless because the policy never picks that action.
With , 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, , so ; in general it is .
Policies, trajectories and the chain a policy induces
A deterministic policy picks one action per state, ; a stochastic policy gives a distribution . A policy and an MDP together define a distribution over trajectories , with each reward drawn given its transition:
Averaging over the action turns the MDP into a Markov chain on states, with one expected reward per state:
The distribution of is the initial distribution pushed through steps of the chain, so the expected reward at time is linear in :
Summing with weights gives the expected return in one linear-algebra expression. The series converges because is a stochastic matrix and ; Value Functions & the Bellman Equations derives the same formula from the Bellman equation.
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?
Show the answer
C.
Multiply the policy and transition factors along the trajectory: .
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.
| Problem | States | Actions | Dynamics |
|---|---|---|---|
| Recycling robot | 2 | 2 or 3 | Stochastic |
| 4×3 gridworld | 11 cells, 2 of them exits | 4 | Stochastic (slips) |
| Inventory or queue control | Countably many: stock level, queue length | A few order sizes or service rates | Stochastic demand |
| Backgammon | About positions | Legal moves | Stochastic (dice) |
| Go | About legal positions | Up to 361 moves, plus passing | Deterministic rules; the opponent's replies make it a game |
| Cart-pole | 4 continuous variables: position, velocity, angle, angular velocity | 2: push left or right | Deterministic physics |
| Legged robot | Dozens of continuous joint angles and velocities, plus sensor readings | Continuous joint torques | Physics plus noise, partly observed |
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 separates what the agent controls (actions), what the world does (transition probabilities) and what is wanted (rewards and ).
- 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 and rewards ; 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.
How many deterministic stationary policies does the recycling robot have?
- 5
- 6
- 8
- 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 . In general there are of them, or when every state allows all actions.
In state a policy takes with probability 0.25 and with probability 0.75. Action moves to with probability 0.4, and with probability 0.8. What is ?
- 0.6
- 0.32
- 1.2
- 0.7
Show the answer
D. 0.7
.
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?
- It is not: two scenes with identical positions can evolve differently. Adding the velocities, or the previous snapshot of positions, fixes it
- It is Markov, because the positions determine the future
- It is not, and only the full history of the drive can fix it
- 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.
Which of these does the trajectory distribution not depend on?
- The initial distribution
- The policy
- The transition probabilities
- The discount factor
Show the answer
D. The discount factor
describes what happens, not how it is scored. The rewards and enter only when trajectories are turned into returns.
Go deeper
- The Markov Property & Agent State · Histories, sufficient statistics, and building a state from what the agent observes.
- Policies & Why Simple Ones Suffice · Why a deterministic, stationary, Markov policy is optimal in discounted MDPs.
- Occupancy Measures & the Dual View · Where a policy spends its time, the flow constraints it obeys, and the linear program they give.
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).