Skip to content

Markov Decision Processes

Make decisions whose consequences unfold over time: states, actions, rewards, transitions, and a discount

Quick recap
  • An MDP models sequential decisions with states, actions, transitions, rewards, and a discount.
  • A policy maps states to actions; a value function scores the expected discounted return.
  • Value iteration solves a known MDP by repeatedly applying the Bellman backup.

This lesson follows From Base Model to Assistant and leads into Reinforcement Learning. The probability background is in Probability & Statistics.

1. Decisions over time

A robot can move toward the nearest reward or take a longer route to a larger one. How should it decide when every move changes the next choice?

A classifier makes one prediction. An agent makes a sequence of decisions, and each decision changes the state it will face next. A robot that moves left may reach a wall, a reward, or a place from which the reward is one step farther away.

The MDP framework makes that feedback loop explicit. The agent observes a state, chooses an action, receives a reward, and transitions to a new state. The goal is not the next reward; it is the total reward over time.

Look at the grid in the animation. Before reading the Bellman equation, predict which cell has the highest value and which action you would take from the start.

Try it

The goal is worth 10 and γ = 0.9. Without doing the full backup, what is the value of a cell directly next to the goal?

2. The MDP tuple

A finite MDP is a tuple of states, actions, transitions, rewards, and a discount:

The MDP

S is the set of states, A is the set of actions, T(s,a,s') is the probability of landing in s' after action a in s, and R(s,a) is the immediate reward. γ is the discount factor. The Markov property says the next state depends only on the current state and action, not the full history.

A policy π(s) chooses an action for each state. A deterministic policy is enough for a finite MDP with known transitions; a stochastic policy can still be useful for exploration or for continuous actions.

3. Return and discount

The return is the discounted sum of future rewards. With γ = 0.9 and rewards 1, 1, 1, the return is 1 + 0.9 + 0.81 = 2.71.

Discounted return

Gt is the return from step t, and Rt+k is the reward k steps later. γ controls how much the agent cares about the future. At γ = 0 it is greedy for the next reward; as γ approaches 1 it values distant rewards more. With γ = 1 and an infinite process, the sum can diverge, so discounted infinite-horizon problems usually use γ < 1.

A reward is immediate. A return is a sum over a trajectory. Confusing the two is the most common MDP mistake: an action with a small immediate reward can be optimal because it leads to a much larger future reward.

4. The Bellman view

Suppose a state gives reward 2, the discount is 0.9, and the next state is worth 20. The current value is 2 + 0.9 × 20 = 20. In general, the value of a state under a policy satisfies a recursive equation: immediate reward plus the discounted value of the next state, averaged over transitions:

Bellman equation for a policy

Vπ(s) is the value of state s under policy π. For the optimal value, replace the policy action with a max over actions:

Bellman optimality

V*(s) is the best value achievable from s. The optimal policy chooses the action with the highest action-value. Q*(s,a) is the best expected return after taking action a in state s:

Policy from Q

π*(s) is the optimal action in state s. A backup is one update that combines a reward with the value of a next state. The Bellman equation turns a global planning problem into local backups. That recursion is the seed of both value iteration and Q-learning.

5. Value iteration

Value iteration starts with all values at zero and repeatedly applies the optimality backup. With γ < 1, the backup is a contraction: each sweep shrinks the maximum distance to the fixed point by at least a factor γ. Repeated sweeps therefore converge to the unique fixed point.

For the 3×3 grid, the goal has value 10, its neighbors have value 9, and the values decay with distance. The policy arrows point toward the goal. The animation’s later scenes show values propagating outward from the goal.

Value iteration assumes you know the transition and reward model. The next lesson removes that assumption and learns from sampled experience instead.

6. Worked example

Two states, “left” and “right.” In either state, “stay” keeps you there with reward 2. In “left,” “go” moves to “right” with reward 1. In “right,” “go” also stays with reward 2. Use γ = 0.9.

The right state has value 2 + 0.9V(right), so V(right) = 20. The left state’s “go” option is 1 + 0.9(20) = 19; “stay” is 0 + 0.9(19) = 17.1. The best action is “go.”

Worked example: value iteration on a chain

statestay valuego valueV(s)policy
left0 + 0.9(19) = 17.11 + 0.9(20) = 1919go
right2 + 0.9(20) = 202 + 0.9(20) = 2020stay/go

The values satisfy the Bellman equations, and the greedy policy is optimal. The right state has a tie because both actions stay there and earn the same reward.

The animation’s grid is larger, but the same backup is applied cell by cell. The exact policy can have ties; the value is what is unique.

The solved grid result has 6 value-iteration sweeps before convergence at this tolerance. A different tolerance changes the count, not the optimal policy.

7. Where it fails

The state space can be enormous. A robot with continuous sensors or a game with millions of positions cannot store one value per state. Function approximation, sampling, and decomposition become necessary.

The model may be wrong. Transition probabilities estimated from logs can be biased, and a reward that is easy to measure may not be the reward you actually want. Reward hacking is a specification failure, not just an optimization failure.

The Markov assumption can hide important history. Partial observability, delayed effects, and non-stationary environments require richer state or different methods.

Next lesson: Reinforcement Learning

8. Going deeper

Policy iteration alternates exact policy evaluation with policy improvement and often converges in fewer iterations than value iteration. Linear programming can solve an MDP exactly, but it scales poorly.

Partially observable MDPs maintain a belief over hidden states. Continuous-state control uses function approximation or model predictive control. Multi-agent settings turn the environment into other agents, which breaks the single-agent assumption.

The discount factor is a modeling choice. It encodes patience, but it also changes the optimal policy. Report it with the result; a policy optimized for γ = 0.9 is not the same policy as one optimized for γ = 0.99.

9. Practice

Try it

Why can two states have the same immediate reward but different values?

Try it

What happens to the optimal policy as γ approaches 0?

Try it

Why is value iteration guaranteed to converge for γ < 1?

10. Resources

The MIT chapter derives finite- and infinite-horizon value functions and value iteration. The Wikipedia article gives the formal tuple, Bellman equations, and variants. Both links were opened and checked for this lesson.

Continue the track

The mathematics behind this topic: Probability & statistics.

All Machine Learning lessons

Site soundtrack