Markov Decision Processes
Imagine a robot navigating a warehouse. At every moment it chooses whether to turn, accelerate, or stop. Meanwhile, shelves, other robots, and battery levels change around it. To act well, the robot needs a precise language. It must describe "what state am I in," "what can I do," "what happens next," and "how good was that." The Markov Decision Process, or MDP, is exactly this language. It is the mathematical skeleton underneath essentially every reinforcement learning algorithm. This includes tabular Q-learning and the deep networks that play Go or align large language models. The reason MDPs matter goes beyond notation. Once a problem is written as an MDP, an enormous body of theory becomes available. This includes existence of optimal policies and algorithms that provably converge to them. It also includes precise statements of how much computation and memory those algorithms need. Real systems — recommendation engines, robotic controllers, inventory managers, dialogue agents — are modeled as MDPs (or close relatives). This lets this machinery be applied instead of reinvented from scratch for each application. This article builds the MDP tuple piece by piece: states, actions, transition dynamics, rewards, and discounting. It explains what the Markov property buys us. It shows why this is the load-bearing assumption of the entire framework. It also shows how this assumption turns a seemingly intractable planning problem into one solvable by dynamic programming. We derive the Bellman equations. We present policy evaluation, policy iteration, and value iteration with full pseudocode. We also prove why they converge. By the end, you will be able to formalize a decision problem as an MDP. You will write down its Bellman equations from first principles. You will also run dynamic programming to compute optimal behavior. This is the foundation on which Policies and Value Functions and later the The Exploration-Exploitation Dilemma are built.
- An MDP is the tuple : states, actions, transition dynamics, reward function, and discount factor.
- The Markov property says the next state depends only on the current state and action, not on history — this makes planning tractable.
- Bellman equations recursively relate the value of a state to the values of its successors, giving a fixed-point characterization of optimal behavior.
- Policy evaluation, policy iteration, and value iteration are dynamic programming algorithms that compute value functions and optimal policies exactly, given a known model.
- Convergence follows because the Bellman optimality operator is a -contraction in the max-norm, guaranteeing a unique fixed point reached at geometric rate.
- MDPs are the formal problem statement that all of reinforcement learning — model-based or model-free — is trying to solve.
An MDP formalizes sequential decision-making under uncertainty as a state-action-reward system obeying the Markov property, turning "act optimally over time" into a well-defined fixed-point problem solvable by dynamic programming.
The idea
A Markov Decision Process is a tuple
where is the set of states, is the set of actions, is the transition dynamics giving the probability of landing in state after taking action in state , (or a simpler ) is the expected immediate reward, and is the discount factor. An agent interacts with this process over a sequence of discrete time steps : at each step it observes state , selects action , receives reward , and transitions to . The crucial structural assumption is the Markov property, already familiar as a precondition for this framework: . The state is a sufficient statistic for the future — everything relevant about the history is compressed into . This is not merely a simplifying convenience; it is what makes the value of a state well-defined independent of how the agent arrived there, which in turn is what allows recursive (dynamic programming) solutions rather than search over entire histories. A policy is a mapping from states to (distributions over) actions; it is the object an agent is ultimately trying to choose well, and it will be treated fully in Policies and Value Functions. Given a policy, the return aggregates future rewards, discounted by — this quantity was introduced under Rewards and Returns and the MDP is precisely the generative model that produces the reward sequence being summed. The discount factor serves double duty: it encodes a preference for sooner reward over later reward, and — critically — it guarantees the return is a finite, well-defined random variable even in infinite-horizon tasks with bounded rewards, since for .
How it works
The payoff of formalizing a problem as an MDP is that value functions become computable via recursion. Define the state-value function under policy as the expected return starting from and following :
and the action-value function
Because the return can be unrolled one step, , and because of the Markov property, satisfies the Bellman expectation equation:
An optimal policy achieves for every state and every policy simultaneously (a nontrivial existence fact, justified below). Its value function satisfies the Bellman optimality equation:
This equation is not a formula to evaluate directly — appears on both sides — but a fixed-point condition. Three classical dynamic-programming algorithms exploit it, assuming the model is fully known.
1. Iterative policy evaluation computes for a fixed policy by repeatedly applying the Bellman expectation equation as an update rule until it stabilizes.
2. Policy iteration alternates full policy evaluation with a greedy policy improvement step.
3. Value iteration collapses evaluation and improvement into a single sweep using the max directly. ```
# --- Iterative Policy Evaluation ---
# Input: MDP (S, A, P, R, gamma), policy pi, tolerance theta
# Output: V approximating V_pi initialize V(s) = 0 for all s in S repeat: delta = 0 for each s in S: v_old = V(s) V(s) = sum over a of pi(a|s) sum over s' of P(s'|s,a) (R(s,a,s') + gamma * V(s')) delta = max(delta, |v_old - V(s)|) until delta < theta return V ``` ```
# --- Policy Iteration ---
# Input: MDP (S, A, P, R, gamma)
# Output: optimal policy pi, optimal value V initialize pi(s) arbitrarily for all s in S initialize V(s) = 0 for all s in S loop:
# 1. Policy evaluation: compute V ~= V_pi (as above, using current pi) V = PolicyEvaluation(pi, V)
# 2. Policy improvement policy_stable = true for each s in S: old_action = pi(s) pi(s) = argmax over a of sum over s' of P(s'|s,a) (R(s,a,s') + gamma V(s')) if old_action != pi(s): policy_stable = false if policy_stable: return pi, V ``` ```
# --- Value Iteration ---
# Input: MDP (S, A, P, R, gamma), tolerance theta
# Output: optimal value V, optimal (greedy) policy pi initialize V(s) = 0 for all s in S repeat: delta = 0 for each s in S: v_old = V(s) V(s) = max over a of sum over s' of P(s'|s,a) (R(s,a,s') + gamma V(s')) delta = max(delta, |v_old - V(s)|) until delta < theta
# Extract greedy policy for each s in S: pi(s) = argmax over a of sum over s' of P(s'|s,a) (R(s,a,s') + gamma V(s')) return V, pi ```
Complexity & correctness
Complexity. Let and . Each Bellman backup at a single state — computing the sum or max over actions and successor states — costs time in the worst case (dense transition matrices), or if each state has at most possible successors under any action ("branching factor"). One full sweep over all states therefore costs (dense) or (sparse). Memory is for the value table plus to store explicitly, or less if the model is given as a generative simulator rather than a table.
- Policy evaluation to tolerance takes sweeps in the worst case (derived below), each , so total .
- Policy iteration performs at most possible deterministic policies in principle, but in practice terminates after a small number of improvement steps; each step requires one policy evaluation. A classical (non-trivial) result shows policy iteration terminates in a number of iterations that is polynomial in , , and for a fixed discount, and always terminates finitely because the policy strictly improves at every non-terminal step and there are finitely many deterministic policies.
- Value iteration requires no inner evaluation loop; each outer sweep is , and the number of sweeps to reach tolerance is again .
Correctness — the contraction argument. Define the Bellman optimality operator acting on any value function by
Claim: is a contraction in the max-norm , with contraction factor :
Proof sketch. Fix . Let be the maximizing action for . Then
using that is a probability distribution so the weighted average is bounded by the max, and reward terms cancel since both use the same . By symmetry, swapping , we get for every , hence the same bound in max-norm. Since , the Banach fixed-point theorem guarantees has a unique fixed point , and iterating from any starting converges to it geometrically: . This is exactly what value iteration does, which is why it converges regardless of initialization, and why the number of sweeps to reach error scales as . The same contraction argument (with replaced by ) shows the Bellman expectation operator is also a -contraction, so iterative policy evaluation converges to for the same reason.
Correctness of policy improvement. The policy improvement theorem states: if for all , then for all , with strict inequality somewhere unless is already optimal. The proof unrolls the inequality repeatedly, substituting the Bellman equation at each step, which shows the return under dominates term by term. Because there are only finitely many deterministic policies ( of them) and each improvement step either strictly improves some state's value or leaves the policy unchanged (signaling optimality reached), policy iteration must terminate at the true optimum in finitely many steps — it cannot cycle.
Tradeoffs. Value iteration needs no nested evaluation loop but its single-sweep updates converge only asymptotically to ; you must choose a tolerance . Policy iteration converges in exact, finitely many outer steps but each step's inner evaluation is itself an iterative (or, if you solve the linear system directly, an -cost) computation. Both require the full model — this is dynamic programming, not learning from samples, and it scales poorly when is astronomically large (e.g. Go's states), which is precisely the gap that model-free reinforcement learning and function approximation are built to close.
Worked example
Consider a tiny robot-navigation MDP with three states , where is a terminal goal state (self-absorbing, reward 0 thereafter). Two actions: Wait (stay put, small cost) and Move (advance toward the goal, but may slip). Discount .
| From | Action | To | Probability | Reward |
|---|---|---|---|---|
| Wait | 1.0 | |||
| Move | 0.8 | |||
| Move | 0.2 | |||
| Wait | 1.0 | |||
| Move | 0.9 | |||
| Move | 0.1 | |||
| any | 1.0 |
Initialize . Run value-iteration sweeps:
Sweep 1.
Sweep 2.
Sweep 3 (values continue climbing, differences shrinking by roughly a factor bounded by each sweep):
Continuing until yields approximately:
Extracting the greedy policy: at , Move has higher backed-up value than Wait once is large enough (this flips after sweep 1, exactly when the chance of reaching the high-value outweighs the wasted step) — so . At , Move is optimal from the very first sweep since the 0.9 chance of the +10 terminal reward dominates. This matches intuition: always push toward the goal, never wait, because waiting only accrues cost with no chance of progress. The example also illustrates why value iteration's early sweeps can recommend the wrong action (at sweep 1, was a tie) before enough backed-up information propagates from the reward source — a concrete instance of the convergence bound in action.
Common pitfalls & traps
| Pitfall | Why it happens | Fix |
|---|---|---|
| Forgetting for infinite-horizon tasks with no terminal state | Return diverges if and rewards don't vanish | Use , or ensure episodic termination with only when episodes are guaranteed finite |
| Confusing with | Some texts define reward as a function of the pre-state and action only, others include the next state; formulas differ subtly | Fix one convention per problem and be consistent when writing Bellman equations |
| Treating a non-Markov observation as if it were the true state | Partial observability (hidden state) breaks the recursive Bellman structure silently | Augment state with sufficient history/summary statistics, or model explicitly as a POMDP |
| Assuming policy iteration must be run to full convergence of the inner evaluation | Modified/optimistic policy iteration (few evaluation sweeps) still converges and is often cheaper | Understand this is a valid tradeoff, not a bug — it interpolates toward value iteration |
| Sign/max-min errors when converting cost to reward | Minimizing cost is equivalent to maximizing negative reward, but people flip signs inconsistently in Bellman updates | Pick one sign convention (usually maximize reward) and check it end-to-end |
| Using a synchronous "sweep" update in place synchronously vs. in-place (asynchronous) without realizing they have different guarantees | In-place (Gauss-Seidel style) updates can converge faster but are analyzed differently | Know which update rule you implemented and cite the matching convergence guarantee |
Pioneers & history
Origins · 1 minRichard Ernest Bellman and how this was discovered
Bellman developed dynamic programming while working on multistage decision problems for the U.S. military and later industrial optimization. He introduced the principle of optimality — an optimal policy has the property that, whatever the initial decision, the remaining decisions must be optimal with respect to the state resulting from that decision — which is the conceptual seed of the equation that bears his name.
Where this matters
MDPs underlie the control loops of robotics research, where a robot's joint angles, velocities, and sensor readings form the state and torque commands the actions. Recommendation systems at scale — deciding which content to show a user next — are frequently modeled as MDPs to account for the long-term effect of a recommendation on future engagement, not just immediate clicks. Inventory management and supply-chain optimization use finite or infinite-horizon MDPs solved by dynamic programming or approximate variants when the state space is large. Operations research applications such as elevator scheduling, network routing, and revenue management were classical MDP successes well before deep learning existed. In modern AI, the MDP formalism (or its extension, the POMDP, for partial observability) is the object every reinforcement learning algorithm is implicitly trying to solve — including the algorithms used to fine-tune large language models via reward signals, where the "state" is the generated text so far and the "action" is the next token. Anywhere a system must choose actions sequentially under uncertainty, balancing immediate and future consequences, an MDP is the formal skeleton underneath the engineering solution.
Ask the tutor: How does the Bellman equation change when the reward depends only on instead of , and why does it not affect the contraction argument?
What is the difference between an MDP and a Markov chain?
A Markov chain is a passive stochastic process: it has states and transition probabilities but no actions or rewards, so its future is fully determined by dynamics alone. An MDP adds a decision-maker — actions that influence the transition probabilities — and a reward signal that defines what "good" behavior means. Fixing a policy in an MDP (i.e., choosing an action-selection rule for every state) collapses it back into an ordinary Markov chain, since the action distribution at each state becomes fixed and only the state transitions remain stochastic. This is precisely why policy evaluation works: evaluating is really just computing expected discounted rewards over the Markov chain induced by .
Do MDPs require finite state and action spaces?
No. The classical dynamic programming algorithms (policy iteration, value iteration) are most cleanly stated and analyzed for finite and , where sweeps over all states are computationally realizable. However, the MDP formalism itself extends to countably infinite and continuous state/action spaces, with the same Bellman equations holding in expectation form. In continuous settings exact dynamic programming becomes intractable (you cannot enumerate uncountably many states), which motivates function approximation, discretization, and sampling-based methods — the bridge from tabular reinforcement learning to deep reinforcement learning.
What happens if the discount factor γ equals 1?
With , the return is simply the undiscounted sum of future rewards, . This is fine for episodic tasks that are guaranteed to terminate in finite time (so the sum has finitely many nonzero terms), and is in fact the natural choice there — many chess or game-playing formulations use . For continuing tasks with no terminal state, an undiscounted sum of nonzero rewards diverges, making the value function undefined and breaking the contraction argument that guarantees convergence of dynamic programming. In continuing tasks you either need or must switch to an average-reward formulation instead of discounted return.
Why is the Bellman equation called a "fixed-point" equation rather than a formula?
Because appears on both sides of , it cannot be evaluated directly the way you'd evaluate an explicit closed-form expression. Instead, is defined as the unique function that remains unchanged when the Bellman optimality operator is applied to it, i.e., . Algorithms like value iteration find this fixed point iteratively, starting from an arbitrary guess and repeatedly applying , relying on the contraction-mapping guarantee that this process converges to the one function satisfying the equation, regardless of the starting guess.
How is an MDP different from what an RL agent actually has access to?
The MDP itself specifies the true dynamics of the environment, which are assumed fully known when doing dynamic programming as described in this article. A reinforcement learning agent, by contrast, typically does not know or in advance and must learn a good policy purely from sampled interaction — observed states, actions, and rewards. This distinction is the boundary between planning (dynamic programming with a known model) and learning (model-free methods like Q-learning, or model-based methods that first estimate and from data). The MDP is the underlying problem definition in both cases; what differs is whether the solver is handed the model or must discover it.
What is the relationship between the state-value function V and the action-value function Q?
They are related by — the state value is the action value averaged over the policy's action distribution. Conversely, — the action value is the immediate reward plus the discounted value of wherever that action leads. For the optimal policy, , and greedy action selection with respect to recovers an optimal policy directly, without needing the transition model — this is exactly what makes -learning, a model-free method, possible. This relationship will be developed fully in Policies and Value Functions.
Why can't we just search over all possible action sequences instead of using Bellman equations?
Brute-force search over action sequences of length with actions costs , which explodes exponentially and becomes hopeless even for modest horizons. The Bellman equation exploits the Markov property and the principle of optimality to avoid this: the optimal choice at every state depends only on the current state, not on the sequence of actions that led there, so optimal sub-solutions can be reused across many different histories that happen to land on the same state. This is exactly what dynamic programming means — computing once per state and reusing it, rather than recomputing value for every distinct trajectory, turning an exponential search into a polynomial-time computation (per sweep) over the state space.