Skip to content
Lambdian

Markov decision processes and Bellman equations

The language of reinforcement learning: states, actions, rewards, returns, policies and value functions.

Animated explanation

The idea

In reinforcement learning nobody gives the right answers. An agent acts in an environment, receives rewards, and must learn which actions lead to the most reward over time. A chess move that loses a pawn now may win the game later.

A Markov decision process makes this precise: states ss, actions aa, transition probabilities P(s′∣s,a)P(s'\mid s,a), rewards rr and a discount γ\gamma. A policy π(a∣s)\pi(a\mid s) says what to do in each state. The value Vπ(s)V^\pi(s) is the expected discounted return from ss when following π\pi.

The key insight, due to Bellman, is that values are recursive: the value of a state is the immediate reward plus the discounted value of the next state. Almost every RL algorithm is a way of solving or approximating that recursion.

The Markov property

The next state and reward depend only on the current state and action, not on the history: P(st+1∣st,at,st−1,… )=P(st+1∣st,at)P(s_{t+1}\mid s_t, a_t, s_{t-1}, \dots) = P(s_{t+1}\mid s_t, a_t). If your observation doesn't have this property (a single video frame doesn't show velocity), extend the state (stack frames) or use a partially observable model.

State values and action values

Vπ(s)V^\pi(s) is the value of being in ss. Qπ(s,a)Q^\pi(s,a) is the value of taking action aa in ss and then following π\pi. Knowing QQ lets you act without a model: pick arg⁡max⁡aQ(s,a)\arg\max_a Q(s,a). The optimal values V∗V^\ast, Q∗Q^\ast satisfy the Bellman optimality equations, and any policy greedy with respect to Q∗Q^\ast is optimal.

Policy evaluation

For a fixed policy the Bellman expectation equation is a linear system in the ∣S∣|S| unknowns Vπ(s)V^\pi(s): V=Rπ+γPπVV = R^\pi + \gamma P^\pi V, so V=(I−γPπ)−1RπV = (I - \gamma P^\pi)^{-1}R^\pi. For large state spaces we iterate the equation instead; because it is a γ\gamma-contraction, the iteration converges from any start.

The algorithm, step by step

  1. 1
    Define states, actions, P(s′∣s,a)P(s'\mid s,a), rewards and γ\gamma.
  2. 2
    Iterative policy evaluation: start with V(s)=0V(s) = 0.
  3. 3
    Sweep: for each non-terminal ss, V(s)←∑aπ(a∣s)∑s′P(s′∣s,a)[r+γV(s′)]V(s) \leftarrow \sum_a\pi(a\mid s)\sum_{s'}P(s'\mid s,a)[r + \gamma V(s')].
  4. 4
    Repeat until the largest change in a sweep is below a tolerance.

The maths

Return

Gt=∑k=0∞γkrt+k+1G_t = \sum_{k=0}^\infty\gamma^k r_{t+k+1}

Action value

Qπ(s,a)=∑s′P(s′∣s,a)[r(s,a,s′)+γVπ(s′)]Q^\pi(s,a) = \sum_{s'}P(s'\mid s,a)\big[r(s,a,s') + \gamma V^\pi(s')\big]

Bellman expectation (matrix form)

Vπ=Rπ+γPπVπ  ⇒  Vπ=(I−γPπ)−1RπV^\pi = R^\pi + \gamma P^\pi V^\pi \;\Rightarrow\; V^\pi = (I - \gamma P^\pi)^{-1}R^\pi

Bellman optimality

Q∗(s,a)=∑s′P(s′∣s,a)[r+γmax⁡a′Q∗(s′,a′)]Q^\ast(s,a) = \sum_{s'}P(s'\mid s,a)\big[r + \gamma\max_{a'}Q^\ast(s',a')\big]

Strengths and weaknesses

Strengths

  • One framework for sequential decisions under uncertainty
  • Bellman equations turn long horizons into one-step recursions
  • Policy evaluation has exact and iterative solutions

Weaknesses

  • Requires a Markov state
  • Exact methods need the full model P and small state spaces
  • Rewards must be designed carefully (agents exploit loopholes)

Reach for it when…

Any problem where actions have delayed consequences: games, robotics, inventory, recommendation sequences, treatment plans.

Check your understanding

Question 1

Q1With γ = 0.9, a reward of 10 received 2 steps from now is worth…

Question 2

Q2What does Qπ(s,a)Q^\pi(s,a) measure?

Question 3

Q3For a fixed policy, the Bellman expectation equations form…

Lambdian Lab

Returns and policy evaluation

The gridworld above the dashed line is provided (don't edit it).

  • discounted_return(rewards, gamma): ∑kγkrk\sum_k\gamma^kr_k.
  • evaluate_policy(policy, gamma, tol): iterative policy evaluation; policy is a dict state → action; return a dict state → value (terminals 0).
  • evaluate_exact(policy, gamma): solve the linear system (I−γPπ)V=Rπ(I-\gamma P^\pi)V = R^\pi over the non-terminal states.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.