Markov Decision Processes
The mathematical scaffolding for sequential decision-making — states, actions, rewards, and the Bellman view of optimality. The Markov property, the MDP tuple, discounting, policies, value functions, the Bellman expectation and optimality equations, and the contraction property that powers every algorithm to come.
01 · Motivation
Why does this matter?
Everything you have learned so far in this course — linear regression, classification, SVMs, kernel methods — assumed a fundamentally static world. You showed the model a fixed dataset and asked for a single prediction. There was no notion of time, no sense that the prediction you make today changes what data you see tomorrow.
But many of the most important problems are not static. What unites them is a common shape: an agent interacts with an environment over many time steps; each action changes the world; rewards may be delayed; and the goal is to maximise total reward over time, not to predict a single label.
A robot learning to walk
Every motor command changes the robot’s pose, which changes which next commands are even possible. A “wrong” step now means falling a second later. The cost is delayed, and there is no labelled dataset of “correct walking sequences.”
An investment portfolio
Today’s allocation determines tomorrow’s wealth, which determines what allocations are even feasible. A great trade today can lock you out of a better trade next week.
A chess game
A single brilliant move is worthless if the next twenty are bad. The “reward” — winning the game — arrives only at the end, dozens of decisions later.
Three reasons sequential problems are hard
1. Actions have long-term consequences. A move that looks good now may be catastrophic in ten steps. You cannot grade each action in isolation.
2. Reward is delayed. In chess the only reward arrives at checkmate. Which of the previous 60 moves caused the victory? This is the credit-assignment problem.
3. The agent’s behaviour shapes its data. Unlike supervised learning, the agent doesn’t see a fixed dataset — it generates its own data by interacting with the world. Aggressive exploration is risky; cautious behaviour might never discover the best strategy.
This chapter introduces the Markov Decision Process (MDP), the mathematical language that captures all three difficulties in a single, surprisingly clean framework. Once you can write a problem as an MDP, an entire toolbox of algorithms (Chapters 9 and 10) becomes available to solve it.
When is reinforcement learning useful?
Two situations make the RL viewpoint essential rather than optional:
- The environment dynamics are unknown or hard to model. No one writes down the equations of motion for the stock market or the response of a real customer to an ad.
- The model exists but is too complex to solve exactly. Chess has perfectly known rules, yet its game tree has more leaves than atoms in the observable universe. Approximate methods are the only way forward.
The MDP framework is the common ground for both. Whether you know the dynamics (Chapter 9, “dynamic programming”) or have to learn them from experience (Chapter 10, “reinforcement learning”), the language is the same.
How is this different from what you’ve learned before?
The boundaries against two closely related problems are sharp and exam-relevant.
Supervised learning
Fixed dataset . One prediction per example, graded immediately. No time, no sequence, no feedback loop. The learner cannot change which data it sees.
Multi-armed bandit
Repeated single-step decisions. Actions yield immediate stochastic reward, but actions do not change the state — every step starts fresh. Exploration matters; long-term consequences do not.
Markov decision process
Sequential, stateful, stochastic. Each action changes the world. Reward can be delayed across many steps. Bandits are the special case where .
One way to read the progression: supervised learning adds learning to a static problem; bandits add action selection under uncertainty; MDPs add temporal structure. Each step generalises the last. Everything here reduces correctly to bandit theory if you set , and to a degenerate problem if you also set .
02 · Intuition
The idea in plain language
The agent–environment loop
Picture two entities passing messages back and forth. The agent is the decision-maker (your robot, your trader, your chess engine). The environment is everything else (the physics, the market, the opponent). At every tick of the clock: the agent observes the state, chooses an action; the environment transitions to a new state and emits a reward; repeat.
That single picture is the whole framework. Everything in this chapter, in Chapter 9, and in Chapter 10 is just careful formalisation of this loop.
The Markov property: memoryless states
If the agent had to remember everything it has ever seen, the problem would explode in size with time. The Markov property is the simplifying assumption that rescues us.
The future is independent of the past, given the present
If the current state contains enough information to predict what comes next, then the past is irrelevant. The state is a sufficient statistic for the future. Once we know , we can throw away and lose nothing.
This is a property of how you define the state, not of the world itself. A chessboard position is Markov (the future of the game depends only on the current arrangement of pieces). A poker game’s “what cards I’ve seen” is Markov only if you include the full memory of seen cards in the state.
Rat example: how state design changes the problem
A rat in a maze keeps seeing a sequence of observations bell, light, lever, bell, lever, light, lever, light, food?
- If state = last observation, the rat answers based on the most recent lever or light.
- If state = counts of each observation, the rat aggregates frequency information.
- If state = complete history, the rat captures order and timing but pays in complexity.
Different state definitions lead to different problems and different optimal behaviours. State design is part of modelling the problem.
Fully vs partially observable worlds
There are actually three different “states” floating around in any sequential problem, and it pays to keep them distinct:
- Environment state : whatever information the environment actually uses to compute the next transition and reward. Often hidden from the agent.
- Agent state : whatever the agent maintains internally and uses to choose its next action — some function of the history.
- Observation : what the agent receives each step. May be a partial, noisy view of .
The clean case — and the one this chapter studies — is when everything coincides: . The agent sees the full environment state directly. This is the fully observable setting, and it is exactly what an MDP models. When observations only give partial information, we have a Partially Observable MDP (POMDP), a much harder beast.
Three deceptively simple ingredients
States & Actions — what can happen?
A list of every situation the agent can be in () and every choice it can make (). For chess: all positions, all legal moves.
Transitions — how does the world react?
A rule : if I’m in state and take action , how likely am I to land in ? Stochastic in general (slippery floors, opponent randomness).
Rewards — what do I want?
A scalar emitted each step — the agent’s only feedback signal. Positive = good, negative = bad. The agent’s purpose is to maximise the sum of these over time.
Plus one knob — the discount factor — that controls how much the agent cares about the far future versus the near future. We’ll meet it formally in a moment.
The reward hypothesis (Sutton)
All goals can be expressed as the maximisation of a scalar reward
This is the working hypothesis of the entire field of reinforcement learning. It is probably not literally true — human goals seem more textured than a single number — but it is so powerful and so flexible that we adopt it as a starting point.
Key principle: the reward should specify what you want, not how to achieve it. If you reward “moving forward” instead of “winning the race”, the agent may discover that running in circles is technically optimal. A famous boat-racing agent learned to orbit a lagoon of respawning power-ups forever, scoring infinite reward while never finishing the race. The reward said “collect points”; the goal was “finish first”; the agent obeyed the reward.
03 · Formalism
Definitions and equations
The Markov property, precisely
A stochastic process is Markov when
Conditioning on the full history is the same as conditioning on just the current state. The state absorbs every relevant piece of the past.
Why this assumption is load-bearing
Without the Markov property, predicting the next step would require conditioning on the entire history , which grows unboundedly with time. There would be no recursion to exploit, no fixed-point equation, no way to write “value of being here = reward + γ × value of next state” — because “next state” would depend on stuff that happened ten steps ago.
The Markov property is the assumption that buys us recursion. Every Bellman equation, every dynamic programming algorithm, every Q-learning update relies on it.
If we additionally assume stationarity (the transition probabilities don’t depend on ), we get a single transition matrix that works for every step.
The MDP tuple
A discrete-time, finite Markov Decision Process is a tuple
- States $\mathcal{S}$
- a finite set of states.
- Actions $\mathcal{A}$
- a finite set of actions (sometimes — the actions available depend on ).
- Transitions $\mathcal{P}$
- a transition kernel .
- Reward $\mathcal{R}$
- a reward function , the expected one-step reward.
- Discount $\gamma$
- a discount factor in .
- Initial law $\mu_0$
- an initial-state distribution .
Memorise this tuple
Every algorithm in Chapters 9 and 10 starts by writing “given an MDP …” The first job in any RL question is to identify these five components in the problem at hand.
The return: how we score a trajectory
At each step the agent collects a reward . The total score we want to maximise over a trajectory is the return . Several flavours exist depending on the time horizon.
Finite horizon
. Hard deadline at step . Used when the task has a natural ending (a chess game, one day of trading). Optimal policies may be non-stationary because “time left” matters.
Average reward
. Per-step reward in the long run. Useful for “ongoing” systems — queues, controllers, traffic — where there is no natural ending.
Discounted (most common)
. Infinite horizon, but each reward shrinks by per step. Mathematically clean, always finite for bounded rewards. The default in modern RL.
Episodic vs continuing tasks
- Episodic tasks have terminal/absorbing states (the Sleep state in our worked example, “checkmate” in chess). Once you enter one, the episode ends and rewards stop.
- Continuing tasks never terminate (a thermostat, a power-grid controller). Discounting is essential here to keep returns finite.
A clever trick: a finite-horizon or episodic task can be reframed as a continuing one by adding a single absorbing “end” state with reward 0 and a self-loop. After that, the infinite-discounted-return machinery handles everything.
Why discount? The role of γ
The discount factor might look like an arbitrary engineering parameter, but it serves several deep purposes at once:
- Mathematical convergence. An infinite sum of rewards could diverge. With and bounded rewards, is always finite.
- Modelling preference. “A euro today is worth more than a euro next year.” Humans, animals, and markets all discount the future.
- Uncertainty about the future. can be read as the probability the process continues each step; with probability the episode ends.
- Algorithmic stability. Discounting makes the Bellman operators contractions, which is what makes value iteration and policy iteration converge.
The choice of controls the agent’s “horizon”: is myopic (only the immediate next reward matters); is far-sighted (distant rewards weigh almost as much as immediate ones).
Policies: the agent’s strategy
A policy is the agent’s rule for choosing actions. In full generality a policy can be any function of the history, but for MDPs we get a wonderful simplification: we only ever need stationary deterministic Markovian policies (we’ll see why shortly). Policies are classified along three independent axes.
Markovian vs history-dependent
Markovian: depends only on the current state . History-dependent: depends on the full trajectory . For a Markov environment, optimal Markovian policies always exist.
Deterministic vs stochastic
Deterministic: , one action per state. Stochastic: is a distribution. Stochastic policies matter for exploration and for non-MDP settings (games, POMDPs).
Stationary vs non-stationary
Stationary: the same rule at every step. Non-stationary: the rule depends on (relevant for finite-horizon problems where “how much time is left” matters).
A distribution over actions depending only on the current state, not on or history. A deterministic policy is the special case where one action gets probability 1; we write it .
An MDP + policy collapse to a Markov chain
Once you fix a policy , there is no longer any choice to make — the agent’s actions are determined (in distribution) by the state. The combined system becomes a plain Markov reward process with
This is why fixing a policy makes evaluation easy — once the action choice is decided, the rest is just a Markov chain we know how to analyse.
Value functions: how good is a state?
Given a policy , two natural questions about every state and every (state, action) pair:
“If I start in and follow forever, how much reward do I expect to collect?"
"If I start in , take once, then follow forever.” Differs from only in the very first action.
The two are linked by a simple averaging:
The value of a state is the average of the action-values, weighted by how often the policy takes each action.
The Bellman expectation equations
Here is the key insight that unlocks all of dynamic programming. The value of a state can be written recursively: “immediate reward, plus the discounted value of where I land next.”
Read it as: now + γ × later
Every Bellman equation, no matter how intimidating it looks, has the same shape: value of where I am = expected immediate reward + γ × expected value of where I go next. Keep that one phrase in mind and every formula in this and the next two chapters falls into place.
Matrix form: a linear system
For a fixed policy , the Bellman expectation equation is linear in . Stack the values into a vector , the rewards into , and the transitions into a matrix . Then
Policy evaluation reduces to a linear solve. The matrix is always invertible for . Cost — fine for small problems, prohibitive when is large.
Bellman operators
It helps to package the right-hand side of the Bellman equation as an operator that acts on value functions.
takes any value function and returns the next iterate. The Bellman expectation equation says is the unique fixed point: .
The optimal value function and policy
Among all policies, the optimal ones achieve the highest expected return from every starting state:
For any finite MDP
- There exists an optimal policy with and for every .
- At least one optimal policy is deterministic — you never need randomisation to be optimal.
- At least one optimal policy is stationary — you never need to change strategy over time.
- At least one optimal policy is Markovian — you never need to remember the past beyond .
Given , recovering an optimal policy is trivial:
“In each state, take the action with the highest optimal action-value.” This greedy rule is optimal because of the four facts above. Solving the MDP = finding .
The Bellman optimality equations
and satisfy a non-linear analogue of the expectation equations, with a replacing the expectation over actions:
The expectation backup averages over actions; the optimality backup maximises. That single change is the whole difference between policy evaluation and optimisation.
The makes the optimality system non-linear, so we can no longer solve it with one matrix inverse. The next chapter is dedicated to algorithms that handle the iteratively.
Contraction: the algorithmic engine
The reason iterative algorithms work on MDPs is one beautiful property.
Bellman operators are γ-contractions in max-norm
For any two value functions and any policy :
Each application of a Bellman operator shrinks the gap between any two value functions by at least a factor of . By Banach’s fixed-point theorem, repeated application converges to a unique fixed point — for and for . This is the foundation of every algorithm in Chapter 9.
Deep dive
- Monotonicity: if componentwise, then and .
- Fixed-point uniqueness: is the only solution to ; is the only solution to .
Together with the contraction, these give existence, uniqueness, and convergence of the value-iteration sequence all at once.
A preview: solving the Bellman optimality equation
The Bellman optimality equation is non-linear — the kills closed-form solutions. But the γ-contraction property opens three powerful routes, all developed in Chapter 9:
- Value Iteration. Start with any , apply repeatedly. Convergence to guaranteed at rate .
- Policy Iteration. Alternate: evaluate the current policy (linear solve), then improve it by acting greedily. Often converges in surprisingly few iterations.
- Linear Programming. Write the Bellman optimality inequalities as constraints and minimise . The LP optimum equals .
All three assume we know the MDP. When it is unknown — when the agent must learn from experience — we enter true reinforcement learning (Chapter 10): SARSA, Q-learning, and friends. But they are all, ultimately, sample-based approximations of the same Bellman operators we have just introduced.
04 · Worked example
A 3-state MDP, by hand
Three states, two actions, all numbers chosen so the arithmetic is painless. Discount .
1 · The MDP
Three states: Study (S), Procrastinate (P), Sleep (Z — terminal/absorbing). Two actions: work (W) and rest (R). The transitions and one-step rewards:
| Next-state distribution | |||
|---|---|---|---|
| S | W | S, P | |
| S | R | P, Z | |
| P | W | S, P | |
| P | R | Z | |
| Z | — | self-loop, 0 reward |
We adopt the deterministic policy “always work”, i.e. .
2 · Evaluate the policy
Under “always W”, the MDP collapses to a Markov reward process. Sleep is absorbing, so , leaving two unknowns:
Move everything to the left and simplify:
3 · Solve the linear system
The determinant is , so by Cramer’s rule
Under “always work” the long-run value is roughly 37 from S and 30 from P. P is worth a lot — because from P we tend to bounce back to the high-reward state S.
4 · Compare to 'always rest'
From S, R sends us to P or Z with equal probability; from P, R sends us to Z. After one step we mostly hit Z and earn nothing more:
“Always rest” is dramatically worse: vs from S. A small immediate reward (the from resting in P) can be misleading; the route to real value is to keep accessing S.
5 · Verify Bellman optimality at S
Is “always work” optimal? Check the max over actions of the optimality right-hand side, using the values above as :
- : ✓ matches.
- : Smaller.
So W is the optimal action at S, and by the same check at P. “Always work” satisfies both Bellman equations — the expectation one (for ) and the optimality one (for the max). , deterministic, stationary, Markovian, exactly as promised.
05 · Visual explanation
Seeing the MDP machinery
The MDP as a graph
Every finite MDP is a labelled graph: states are circles, actions are smaller nodes attached to states, and transitions are arrows with probabilities and rewards. Here is our 3-state worked example.
Notice the two-layer structure: from each state the agent chooses one of several actions; from each (state, action) pair the world chooses (stochastically) a next state. Rewards live on the state-action layer.
Backup diagrams: the visual form of Bellman equations
The two Bellman expectation equations have a beautiful diagrammatic interpretation called a backup diagram. Time flows down; we “back up” estimates of future value into estimates of present value.
- backup: a state averages over the actions might take, then over the next states the world might produce.
- backup: a (state, action) pair averages over the next states, then over the actions would pick there.
The Bellman optimality backup is identical except the average over actions () is replaced by a maximum ().
How γ shapes the agent’s horizon
The discount factor doesn’t change what the agent sees — it changes which future rewards it cares about. A reward arriving steps in the future is weighted by . With , a reward 5 steps away is worth about 3% of an immediate one — the agent is essentially indifferent beyond a handful of steps. With , even rewards 100 steps away matter substantially. The choice of is a choice of the agent’s “personality.” The first hands-on widget lets you feel this directly.
06 · Hands-on
Try it yourself
Three widgets, each isolating one idea from the chapter. Push the controls, watch the numbers, and read the takeaways — these are where the abstractions become intuitions. (The geometric convergence of the Bellman backup, hinted at in §3, gets its own interactive plot in the next chapter.)
First, policy evaluation on a 4×4 gridworld: solve for a fixed policy and watch the goal’s value bleed backward through the grid as you change and the slip probability.
Policy evaluation on a 4×4 gridworld
The goal (top-right) pays +1; the pit below it pays −1; every other step costs −0.04. The chosen direction succeeds with probability 1−slip and slips perpendicular with probability slip/2 each. We solve V^π = T^π V^π by repeatedly applying the Bellman expectation operator; cell shade and number are the converged V^π(s).
Next, the discount factor as a weighting of a reward sequence: slide and watch each future reward’s contribution collapse toward the present, and the total return shrink.
How γ weights a reward sequence
A fixed stream of rewards arrives over ten steps — a few small ones early, a big +10 at the end. The bars show the discounted contribution γᵏrₖ of each future reward to today’s return. Slide γ and watch the far future fade: a myopic agent (γ→0) sees almost only the next reward; a far-sighted one (γ→1) values the distant +10 almost in full.
Finally, policy vs return: pick a policy on the 3-state worked example, see its closed-form , then roll out episodes and watch the Monte-Carlo estimate creep toward the exact value — a preview of the sampling problem that defines Chapter 10.
Policy vs return — sample the Markov chain
The 3-state worked example: Study, Procrastinate, and the absorbing Sleep. Pick a policy — its closed-form V^π is solved exactly by the Bellman expectation equation. Then roll out episodes: each is one trajectory through the induced Markov chain, and the empirical V̂(S) (the average discounted return) creeps toward the exact value as samples accumulate.
07 · Exam intel
What the exam actually tests
MDP questions on this exam are the most predictable in the course. Six question shapes appear over and over.
Write down the MDP for the following scenario
A short prose description (a robot, a game, a market) and you must specify the tuple . For full marks: be explicit about the state space (is the opponent’s hand part of it? the current bankroll?), enumerate actions, write as a table or formula, give with units, and justify the choice of .
Compute V-pi for the following policy
Given a small MDP and a fixed policy, evaluate it. Two valid approaches:
- Linear-system approach: write and solve. Cleaner for 2–3 states.
- Iterative approach: apply a few times from . Useful when the matrix is large or the question explicitly asks for “value-iteration steps.”
Show the matrix explicitly — that’s where marks are lost.
Write the Bellman equations for this MDP
Two flavours. Expectation form (given ): . Optimality form: . Common trap: writing when the policy is deterministic (a single term), or a single term when it is stochastic.
Prove the Bellman optimality operator is a γ-contraction
A short proof the exam loves. Skeleton:
- Fix any state and two value functions . Use .
- Pull out and bound each difference by ; since probabilities sum to 1, this is .
- Take on the left: ✓
The proof for is identical with replaced by .
State and justify the existence of an optimal deterministic policy
Memorise the four-part statement: for any finite MDP there exists an optimal policy that is simultaneously (i) deterministic, (ii) stationary, (iii) Markovian, and (iv) achieves and for all . The construction is .
What happens to the value function if γ changes?
A conceptual question. Two facts to deliver: the value function changes (higher means farther rewards matter more, so values rise for positive-reward problems); and the optimal policy may change (different can prefer different actions — the corridor / discount example is canonical).
Memorise four formulas and you have the chapter
- Bellman expectation for :
- Closed-form evaluation:
- Bellman optimality for :
- Greedy policy:
08 · Common mistakes
Where students get this wrong
The Markov property is a property of the environment
It is a property of how you define the state. A partially-observed environment isn’t Markov in its observations; but with the right state (including memory or hidden variables) it can be Markov again. State design is a modelling choice. If your state seems insufficient, enlarge it.
Confusing V and Q
assumes the policy is followed from the start. lets you take an arbitrary action once, then follow the policy. The relationship: . is more useful for control because choosing actions directly requires comparing values, not .
More reward = better — reward shaping gone wrong
Tempting to reward sub-goals (“moved one tile toward the goal: ”) to “help” the agent. This often backfires: the agent racks up shaping rewards by oscillating, never reaching the actual goal. The reward must specify what you want, not how to get it. (Potential-based reward shaping preserves optimality, but it’s easy to misuse.)
Optimal policies must be stochastic
False for MDPs. For any finite MDP there is always a deterministic optimal policy. Stochastic policies become necessary only when the problem is no longer a true MDP — multi-agent games, partial observability, or constrained MDPs. In a vanilla MDP, randomisation buys you nothing.
Reward and return are the same thing
Crucial distinction. Reward is the one-step scalar emitted at time . Return is the (possibly discounted) sum of all future rewards from onward. Value functions are expected returns, not expected rewards. Mixing the two up is the single most common source of Bellman-equation sign errors.
γ = 1 is fine if there's a terminal state
It can be — but only if every trajectory ends in finite time with probability 1. If there’s any chance of an infinite loop, undiscounted returns can diverge and the value function is ill-defined. Discounting () is the safe default that always gives finite values.
An MDP is just a bandit with more states
Half right and dangerously incomplete. A multi-armed bandit is an MDP with , but the behaviour is completely different. In a bandit, actions don’t change the world — every step is independent. In an MDP, an action’s value depends on which future states it makes accessible. The credit-assignment problem only exists in MDPs. Bandit algorithms (UCB, Thompson sampling) do not solve MDPs in general.
09 · Self-check
Can you answer these?
A robot's sensor reads the last 3 frames of camera data, and its action is move / turn / stop. Is this a Markov state?
You compute V-pi(s) = 12 and Q-pi(s, a₁) = 10, Q-pi(s, a₂) = 14. What is the most likely policy at s?
Why does the Bellman optimality equation have no closed-form matrix solution, while the expectation equation does?
With γ = 0.9, T* applied 50 times to V⁰. The gap ‖V⁵⁰ − V*‖∞ is at most:
In a continuing task with γ = 0.95 and rewards bounded by |r| ≤ 10, the maximum possible |vₜ| is:
10 · Recap
One-screen summary
Chapter 08 — load-bearing ideas
- Sequential decision-making is structurally different from supervised learning: actions have long-term consequences, rewards are delayed, and the agent shapes its own data.
- The Markov property — “future depends only on present, not past” — is what makes the problem tractable. State design ensures it.
- An MDP is a tuple . Every RL problem starts here.
- The discount factor γ ensures finite returns, models preference for sooner rewards, and is the source of all algorithmic convergence guarantees.
- A policy collapses an MDP into a Markov chain. For any MDP there is always a stationary, deterministic, Markovian optimal policy.
- Two value functions: (start in , follow ) and (start in , take , then follow ). Linked by .
- Bellman expectation: . Linear in ; closed form via matrix inverse.
- Bellman optimality: . Non-linear; requires iteration.
- Bellman operators are γ-contractions in max-norm. Their fixed points are and . This is the foundation of every algorithm in Chapters 9 and 10.
- Solving the MDP = finding . The optimal policy is then .
What's next
You now have the language. Chapter 9 (Solving MDPs) shows how to actually solve them when you know and : policy iteration, value iteration, and linear programming all emerge directly from the contraction property you just met. Chapter 10 (Reinforcement Learning) drops the assumption that you know the dynamics, and shows how the same Bellman backups can be estimated from experience. Every algorithm from here on is a variation on “approximate the Bellman operator.”