Skip to content
Kudos AI

MDPs and the Bellman Equation

The five parts of a Markov decision process, why a policy beats a plan under uncertainty, and the self-consistency condition every utility must satisfy.

AdvancedModule 130 min · 130 XP
A three-step plan executed once, the first action slipping, and the rest left addressing a state the agent is no longer in - then the Bellman equation taken apart term by term.

Search assumed your actions do what you intend. Drop that assumption - let an action succeed most of the time and slip occasionally - and a fixed sequence of moves stops being a useful answer. What you need instead is a rule that tells you what to do from wherever you actually end up.

The five parts

A Markov decision process is a sequential decision problem with a fully observable, stochastic environment. It is specified by:

PartSymbolWhat it supplies
Statess∈Ss \in SThe situations the agent can be in
Actionsa∈A(s)a \in A(s)What it may attempt from state ss
Transition modelP(s′∣s,a)P(s' \mid s, a)Probability of landing in s′s'
RewardR(s)R(s)What being in ss is worth
Discountγ∈[0,1]\gamma \in [0,1]How much a future reward is worth now

Two of these carry more weight than they first appear to.

The transition model is where uncertainty lives. P(s′∣s,a)P(s' \mid s, a) is a distribution, not an outcome. Attempting to move right might take you right with probability 0.80.8 and leave you where you are with probability 0.20.2. The agent does not choose s′s'; it chooses only aa.

The Markov property is the assumption that makes this tractable. The next state depends on the current state and action alone - not on how you arrived. That is a real restriction, and it is what lets you attach one number to each state instead of to each possible history.

Reward on the state. This course follows Russell & Norvig, where the reward attaches to the state you are in, written R(s)R(s). Much of the literature attaches it to the transition instead, R(s,a,s′)R(s,a,s'). The theory is the same either way, but the equations look different, so expect the discrepancy when comparing sources.

Why the answer is a policy

Because outcomes are stochastic, a plan like "right, right, up" is worthless: the first slip puts you somewhere the rest of the sequence no longer addresses.

The answer is a policy π(s)\pi(s) - an action recommended for every state. It never runs out of applicability, because whatever happens, you are in some state and the policy has an answer for it. An optimal policy π∗\pi^* is one that maximises expected utility.

What the best policy depends on. The figure below is Russell & Norvig's 4x3 world: four squares by three with one blocked by a wall, and two exits worth +1+1 and −1-1. Its transition model is a little richer than the one above: each move goes as intended with probability 0.80.8 and to either side with 0.10.1, and there is no discounting. The one thing you move is the living reward R, the R(s)R(s) every non-terminal square pays, and the optimal policy is a step function of it: at eight thresholds, one square's arrow turns over. The button the textbook -0.04 brings it back to the value Russell & Norvig use.

Interactive: the policy as a step function of the living reward

The 4x3 world, 0.8 intended, 0.1 each side, discount 1.

(1,1)0.705(1,2)0.762(1,3)0.812(2,1)0.655(2,3)0.868(3,1)0.611(3,2)0.660(3,3)0.918(4,1)0.388(4,2)-1(4,3)+1-30-0.04-1-0.2
Utility of square (1,1)
0.705308
Policy interval
7 of 9
Squares unlike the textbook
0
Value iteration vs exact
5.4e-15

At R = -0.04 the optimal policy is the one that holds for every living reward between -0.044833 and -0.027357, and U(1,1) is 0.705308. At (3,1) the agent goes left, the long way round, as far from the pit as it can keep.

The Bellman equation

Suppose you already knew U(s)U(s), the utility of every state. Then the value of being in ss decomposes into what you collect now and what you can expect afterwards:

U(s)=R(s)+γmax⁡a∈A(s)∑s′P(s′∣s,a) U(s′)U(s) = R(s) + \gamma \max_{a \in A(s)} \sum_{s'} P(s' \mid s,a)\, U(s')

Read it slowly, because every piece is doing work:

  • R(s)R(s) - collected for being in ss, regardless of what you do next.
  • max⁡a\max_a - you choose the action, so you take the best one available.
  • ∑s′P(s′∣s,a)U(s′)\sum_{s'} P(s' \mid s,a) U(s') - you do not choose the outcome, so the future is an average weighted by the transition model.
  • γ\gamma - discounts that future relative to the present.

The order matters: maximise over what you control, average over what you do not.

This is a condition, not a recipe. It says nothing about how to find UU; it says only that the true UU must satisfy it at every state simultaneously. With nn states you get nn equations in nn unknowns - but they are nonlinear, because max⁡\max is not a linear operator, so you cannot simply solve them with linear algebra. Getting from this condition to actual numbers is the next lesson.

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.

Why discounting

With γ<1\gamma < 1, a reward kk steps away is worth γk\gamma^k times its face value. Two reasons this matters:

  1. It bounds the total. An infinite run of bounded rewards would otherwise sum to infinity, and infinities cannot be compared.
  2. It expresses genuine preference. Sooner is usually better.

At γ=0\gamma = 0 the agent is myopic, caring only about R(s)R(s). As γ→1\gamma \to 1 it becomes far-sighted, willing to accept a long unrewarding stretch for a large payoff at the end.

Try it live. Watch occupancy probabilities evolve from the transition rates alone, with no policy involved yet. This one is a continuous-time chain with an absorbing failure state, so it never settles: over the printed horizon the mass drains steadily into failed.

State occupancy via the matrix exponential (scipy)

Runs in your browser. The first run downloads the Python runtime (~10 MB), then it is cached.

Before the quiz

Be able to list the five parts, state what the Markov property forbids, explain why a policy beats a plan under uncertainty, and point at which term in the Bellman equation is a maximisation and which is an expectation - and why they are not interchangeable. The companion article Markov Decision Processes derives the same material at more length.

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.

Unlock the full path

This first lesson is free. Enrol to take the mastery quiz, earn XP, and unlock every module, with more interactive, runnable examples throughout.