Skip to content
Kudos AI
Lire en français
Reinforcement Learning

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.

9 min readKudos AI

Prerequisites: Probability from Zero: The Language of Uncertainty

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.

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 ss;
  • a set of actions A(s)A(s) available in each state;
  • a transition model P(s′∣s,a)P(s' \mid s, a), the probability of landing in s′s' when action aa is taken in ss;
  • a reward function R(s)R(s).

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 R(s)R(s) 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 R(s,a,s′)R(s, a, s'), 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 π\pi 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 π\pi from state ss is the expected sum of rewards along the way:

Uπ(s)=E[∑t=0∞γtR(St)],U^{\pi}(s) = E\left[\sum_{t=0}^{\infty} \gamma^{t} R(S_t)\right],

where StS_t is the state reached at time tt and γ∈[0,1]\gamma \in [0, 1] 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 +∞+\infty is not a well-posed question.

With γ<1\gamma < 1 and rewards bounded by Rmax⁡R_{\max}, the geometric series settles it:

U([s0,s1,s2,… ])=∑t=0∞γtR(st)  ≤  ∑t=0∞γtRmax⁡=Rmax⁡1−γ.U([s_0, s_1, s_2, \dots]) = \sum_{t=0}^{\infty} \gamma^{t} R(s_t) \;\le\; \sum_{t=0}^{\infty} \gamma^{t} R_{\max} = \frac{R_{\max}}{1 - \gamma}.

Every utility is finite, so every pair of policies is comparable. γ\gamma near 00 makes the agent myopic; γ=1\gamma = 1 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 π∗=arg⁡max⁡πUπ(s)\pi^{*} = \arg\max_{\pi} U^{\pi}(s). A pleasant consequence of discounted infinite-horizon rewards is that π∗\pi^{*} does not depend on the starting state, so we can speak of the optimal policy and write U(s)U(s) for the utility under it.

U(s)U(s) and R(s)R(s) are different quantities. R(s)R(s) is the short-term reward for being in ss; U(s)U(s) is the long-term total from ss 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:

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').

That is the Bellman equation, after Richard Bellman (1957). Read it slowly: the max⁡\max chooses the best action, the ∑\sum averages over where that action might actually land you, and γ\gamma discounts the future relative to the present.

If there are nn states there are nn such equations in nn unknowns. They are not linear, because max⁡\max 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, s1s_1 and s2s_2, each with R(s)=−0.04R(s) = -0.04 - a small penalty per step, which encourages finishing. Two terminals: GOAL with utility +1+1 and PIT with −1-1. Set γ=0.9\gamma = 0.9.

StateActionOutcomes
s1s_1Right0.8→s20.8 \to s_2, 0.2→s10.2 \to s_1
s1s_1Stay1.0→s11.0 \to s_1
s2s_2Right0.8→0.8 \to GOAL, 0.2→0.2 \to PIT
s2s_2Left0.8→s10.8 \to s_1, 0.2→s20.2 \to s_2

Initialise U(s1)=U(s2)=0U(s_1) = U(s_2) = 0.

Sweep 1. For s2s_2, Right gives 0.8(1)+0.2(−1)=0.60.8(1) + 0.2(-1) = 0.6, while Left gives 0.8(0)+0.2(0)=00.8(0) + 0.2(0) = 0. So

U(s2)=−0.04+0.9×0.6=−0.04+0.54=0.50.U(s_2) = -0.04 + 0.9 \times 0.6 = -0.04 + 0.54 = 0.50 .

For s1s_1, both actions still see zeros, so U(s1)=−0.04+0.9×0=−0.04U(s_1) = -0.04 + 0.9 \times 0 = -0.04.

Sweep 2. Now s1s_1 can see the value that has appeared in s2s_2. Right gives 0.8(0.50)+0.2(−0.04)=0.400−0.008=0.3920.8(0.50) + 0.2(-0.04) = 0.400 - 0.008 = 0.392, beating Stay's −0.04-0.04:

U(s1)=−0.04+0.9×0.392=−0.04+0.3528=0.3128.U(s_1) = -0.04 + 0.9 \times 0.392 = -0.04 + 0.3528 = 0.3128 .

U(s2)U(s_2) does not move, because both its outcomes are terminal.

Continuing:

SweepU(s1)U(s_1)U(s2)U(s_2)
00.00000.0000
1−0.04000.5000
20.31280.5000
30.37630.5000
40.38770.5000
50.38980.5000
60.39020.5000
70.39020.5000

The values stop moving at U(s1)=0.3902U(s_1) = 0.3902, U(s2)=0.5000U(s_2) = 0.5000.

We can confirm that fixed point exactly rather than trusting the iteration. Once Right is known to be the better action at s1s_1, the Bellman equation there reads

U(s1)=−0.04+0.9(0.8×0.5+0.2 U(s1))=0.32+0.18 U(s1),U(s_1) = -0.04 + 0.9\big(0.8 \times 0.5 + 0.2\,U(s_1)\big) = 0.32 + 0.18\,U(s_1),

so U(s1)=0.32/0.82=0.390243…U(s_1) = 0.32 / 0.82 = 0.390243\ldots, matching the table to four decimals.

Reading the policy off the converged utilities gives Right in both states, with expected successor utilities ∑s′P(s′∣s,a) U(s′)\sum_{s'} P(s' \mid s, a)\,U(s') of 0.4780.478 versus 0.3900.390 at s1s_1, and 0.6000.600 versus 0.4120.412 at s2s_2. Adding R(s)R(s) and discounting turns those into the action values Q(s,a)Q(s, a) of the next article: 0.39020.3902 versus 0.31120.3112 at s1s_1, and 0.50000.5000 versus 0.33100.3310 at s2s_2, the same ranking.

Python

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 s1s_1 from sweep 2, while the fourth decimal place kept moving for several sweeps more. Policy iteration exploits that by alternating:

  1. Policy evaluation - given a fixed policy πi\pi_i, compute the utilities it produces.
  2. Policy improvement - recompute the best action in each state using those utilities, giving πi+1\pi_{i+1}.

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 max⁡\max left, and the Bellman equation becomes

Ui(s)=R(s)+γ∑s′P(s′∣s,πi(s)) Ui(s′).U_i(s) = R(s) + \gamma \sum_{s'} P(s' \mid s, \pi_i(s))\, U_i(s') .

These are linear - nn equations, nn unknowns, solvable exactly by standard linear algebra in O(n3)O(n^3). 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 P(s′∣s,a)P(s' \mid s, a) and reward function R(s)R(s) 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 Rmax⁡/(1−γ)R_{\max}/(1-\gamma), so policies remain comparable.
  • The Bellman equation U(s)=R(s)+γmax⁡a∑s′P(s′∣s,a)U(s′)U(s) = R(s) + \gamma \max_a \sum_{s'} P(s' \mid s,a)U(s') is nonlinear because of the max⁡\max.
  • Value iteration applies it as an update until the utilities converge; our world settled at U(s1)=0.3902U(s_1) = 0.3902, confirmed exactly as 0.32/0.820.32/0.82.
  • Policy iteration alternates evaluation and improvement; fixing the policy removes the max⁡\max 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.

Related reading

5 min readReinforcement Learning

The Parameter Nobody Chooses

The living reward in a grid world is written down once and never discussed, and the optimal policy is a step function of it: eight thresholds between -3 and 0, each flipping exactly one square. The textbook value of -0.04 sits 0.0048 away from the one that decides whether the agent takes the shortcut past the pit, and above -0.0221, when steps are nearly free, the optimal move in one corner is to walk into a wall on purpose.

Artificial Intelligence
5 min readProbabilistic Reasoning

The Week That Cannot Have Happened

Take the most likely state on each day and write them down in order, and you have a report the model assigns probability exactly zero: on a four-day machine-monitoring example the day-by-day answer is healthy, healthy, failed, failed, and healthy to failed is a transition that cannot occur. What the two questions actually are, why smoothing and Viterbi answer different ones, and what the 0.411 posterior on the best path means for anyone who has to act on it.

Artificial IntelligenceProbability
3 min readProbabilistic Reasoning

A Hundred Thousand Samples, Four Hundred of Them Real

On the burglary network with both neighbours calling, rejection sampling keeps 183 of 100,000 draws and likelihood weighting keeps all of them at an effective sample size of 396. Both estimates are about 10% off a posterior of 0.284172, and the reason is exactly computable: 252 samples carry 76% of the weight and 99.975% of the squared weight.

Artificial IntelligenceProbability
← Back to all articles