Markov Decision Processes
How to plan when actions do not reliably do what you intend: states, transition models, rewards and discounting, the Bellman equation, and value iteration worked numerically to its fixed point.
Prerequisites: Probability from Zero: The Language of Uncertainty
Adversarial Search and Minimax planned against an opponent, but assumed the world itself was reliable: play a move and the board changes exactly as expected. Real environments are not so obliging. A robot commanded to move forward may drift; a recommendation may or may not be acted on. Actions have probability distributions over outcomes, not single outcomes.
A Markov decision process is the standard formalism for this situation, and it is the foundation the whole of reinforcement learning is built on.
A. The ingredients
An MDP is specified by four things:
- a set of states ;
- a set of actions available in each state;
- a transition model , the probability of landing in when action is taken in ;
- a reward function .
The name comes from the Markov property: the probability of the next state depends only on the current state and action, not on the history of how the agent got there. That is what makes the problem tractable - the current state is a sufficient summary of the past.
A note on where the reward sits. Following Russell & Norvig, the reward here is attached to the state the agent is in, and appears outside both the maximisation and the expectation. Much of the reinforcement-learning literature instead writes , a reward on the transition, which moves it inside the sum. The two formulations are equivalent for our purposes, but the equations look different, so it is worth knowing which convention you are reading.
A policy is a function recommending an action for every state - not a plan for one contingency, but a complete rule of behaviour. Solving an MDP means finding a good policy.
B. Why we discount
The utility of executing a policy from state is the expected sum of rewards along the way:
where is the state reached at time and is the discount factor. Discounting is not a technicality bolted on for convenience. If the agent may never reach a terminal state, histories are infinitely long and undiscounted sums generally diverge - and comparing two policies that both score is not a well-posed question.
With and rewards bounded by , the geometric series settles it:
Every utility is finite, so every pair of policies is comparable. near makes the agent myopic; recovers plain additive rewards, which is safe only when the agent is guaranteed to reach a terminal state - a policy with that guarantee is called proper.
An optimal policy is then . A pleasant consequence of discounted infinite-horizon rewards is that does not depend on the starting state, so we can speak of the optimal policy and write for the utility under it.
and are different quantities. is the short-term reward for being in ; is the long-term total from onward. Conflating them is the single most common source of confusion in this material.
C. The Bellman equation
Here is the central idea. The utility of a state is its immediate reward plus the discounted expected utility of wherever the best action takes you:
That is the Bellman equation, after Richard Bellman (1957). Read it slowly: the chooses the best action, the averages over where that action might actually land you, and discounts the future relative to the present.
If there are states there are such equations in unknowns. They are not linear, because is not a linear operator - so we cannot simply invert a matrix and be done.
The order of operations is the part a formula cannot show, so the figure below takes it apart. Each action gets its own line with its own average over the outcomes it does not control, and the maximum is taken visibly between the lines rather than inside a symbol. Drag the discount to watch the future term grow from nothing: at zero the agent sees only the living cost, and near one it is dominated by a reward several steps away.
Interactive: one Bellman backup, opened up
Maximise over what you control. Average over what you do not.
Each action, averaged over what it cannot choose
- right0.8 x 0.7972 (B) + 0.2 x 0.6512 (A) = 0.7680
- stay1.0 x 0.6512 (A) = 0.6512
- Collected now
- -0.0400
- Discounted future
- 0.6912
- U at this state
- 0.6512
- Action taken
- right
From A at a discount of 0.90, the reward collected now is -0.0400 whatever you do - that is R(s), and it does not depend on the action. Then each action averages over outcomes it does not control: going right is worth 0.7680 on average and staying 0.6512. The max picks right, and U works out at 0.6512. Drag the discount: the chosen action never changes on this problem, because B is always nearer the reward than A. What changes is the value, from a myopic -0.0400 that is nothing but the living cost to a far-sighted number a reward several steps away dominates.
D. Value iteration, worked to convergence
The fix is to iterate. Start with arbitrary utilities, evaluate the right-hand side, and use the result as the new left-hand side. Repeat.
Take a four-state world. Two non-terminal states, and , each with
- a small penalty per step, which encourages finishing. Two terminals:
GOAL with utility and PIT with . Set .
| State | Action | Outcomes |
|---|---|---|
| Right | , | |
| Stay | ||
| Right | GOAL, PIT | |
| Left | , |
Initialise .
Sweep 1. For , Right gives , while Left gives . So
For , both actions still see zeros, so .
Sweep 2. Now can see the value that has appeared in . Right gives , beating Stay's :
does not move, because both its outcomes are terminal.
Continuing:
| Sweep | ||
|---|---|---|
| 0 | 0.0000 | 0.0000 |
| 1 | −0.0400 | 0.5000 |
| 2 | 0.3128 | 0.5000 |
| 3 | 0.3763 | 0.5000 |
| 4 | 0.3877 | 0.5000 |
| 5 | 0.3898 | 0.5000 |
| 6 | 0.3902 | 0.5000 |
| 7 | 0.3902 | 0.5000 |
The values stop moving at , .
We can confirm that fixed point exactly rather than trusting the iteration. Once Right is known to be the better action at , the Bellman equation there reads
so , matching the table to four decimals.
Reading the policy off the converged utilities gives Right in both states, with expected successor utilities of versus at , and versus at . Adding and discounting turns those into the action values of the next article: versus at , and versus at , the same ranking.
Runs in your browser. The first run downloads the Python runtime (~10 MB), then it is cached.
Running it reproduces the table above and prints Right for both states.
E. Policy iteration
Value iteration computes utilities to high precision and reads the policy off at the end. But the policy often stops changing long before the numbers settle - in our world, Right was optimal at from sweep 2, while the fourth decimal place kept moving for several sweeps more. Policy iteration exploits that by alternating:
- Policy evaluation - given a fixed policy , compute the utilities it produces.
- Policy improvement - recompute the best action in each state using those utilities, giving .
Repeat until the policy stops changing. The payoff is in step 1: with the action in each state fixed by the policy, there is no left, and the Bellman equation becomes
These are linear - equations, unknowns, solvable exactly by standard linear algebra in . For small state spaces exact policy evaluation is often the fastest approach; for large ones the cubic cost bites, and an approximate evaluation (a few sweeps rather than an exact solve) is used instead.
F. What this buys, and what it assumes
Both algorithms deliver an optimal policy for a known MDP. That assumption is the important one: value and policy iteration both require the transition model and reward function up front. They are planning algorithms, not learning algorithms.
An agent dropped into an unknown environment has neither. It must act, observe what happens, and improve - which is the subject of Reinforcement Learning and Q-Learning.
Key takeaways
- An MDP is states, actions, a transition model, and rewards, with the Markov property making the current state a sufficient summary of the past.
- A policy specifies an action for every state; solving an MDP means finding an optimal one.
- Discounting keeps infinite-horizon utilities finite, bounded by , so policies remain comparable.
- The Bellman equation is nonlinear because of the .
- Value iteration applies it as an update until the utilities converge; our world settled at , confirmed exactly as .
- Policy iteration alternates evaluation and improvement; fixing the policy removes the and leaves linear equations.
- Both require a known model - they plan, they do not learn.
What's next
Reinforcement Learning and Q-Learning drops the assumption that the model is known and learns good behaviour from experience alone.
References & further reading
- Stuart Russell, Peter Norvig, Artificial Intelligence: A Modern Approach, Pearson (3rd edition), 2010· Kudos AI reference library
Copyrighted works are cited for reference only and are not hosted here; please consult the publisher for access.