Value Iteration & Q-Learning

P9.rl-foundations.04 · Audience: guest, it-ml, language-pro · Prerequisites: Markov Decision Processes

Real LLM grading for this pageLLM grading (this page):

The last module ended one step short of an answer: an MDP is solved by a value map plus a policy, but we never said how to fill in the values. This module supplies the method — and it is beautifully simple. One equation, the Bellman backup, says how the value of a cell relates to its neighbours; apply it over and over and the whole value map falls out. We will trace it by hand on the three-cell corridor, watch it converge, then meet Q-learning — the same idea when the agent does not even know the rules and must learn the values purely from experience.

Step 1 / 5The value of a state

Fix the idea the last module reached for: the value of a state is the total future reward you can expect to collect, starting from there and acting optimally — with rewards discounted by γ the further off they are. A cell next to the goal has a high value; a cell far away, a smaller one. Value is the currency that lets a far-sighted agent compare actions: pick the one that lands you in the highest-valued state.

We use the same 1×3 corridor: cells L, M, R in a row, with R the +1 goal (a terminal cell — the agent stops on reaching it), every step paying 0, and γ = 0.9. By convention a terminal cell has value 0: its reward is collected on entering it, not for sitting in it. Our target answer, which we are about to derive, is:

CellOptimal value V*Best action
L (left)0.9right
M (middle)1.0right
R (goal, terminal)0.0

M is worth 1.0 because one step right enters the goal and collects the full +1 immediately. L is worth 0.9 because from L the goal is one discounted step further off. The policy "right, right" walks L → M → goal. Now let us compute those numbers from nothing.

Going deeper (technical) — why these methods converge at all

Interview-grade notes for the technical (it-ml) track.

Value iteration is a contraction. The Bellman optimality backup is an operator T that maps a value function to a new one. Its key property is that it is a γ-contraction in the max-norm: for any two value functions U and V,

max_s | (T U)(s) − (T V)(s) |  ≤  γ · max_s | U(s) − V(s) |

Every sweep shrinks the worst-case error by at least a factor of γ. By the Banach fixed-point theorem a contraction has exactly one fixed point, and repeated application converges to it geometrically — so value iteration converges to the unique optimal V* from any starting values, and the error after k sweeps is bounded by γ^k times the initial error. This is why γ < 1 is not a mere modelling preference but a requirement for the guarantee: at γ = 1 the contraction becomes non-strict and convergence needs extra structure (a proper policy reaching a terminal with probability 1). It also explains the corridor's behaviour — with the goal one and two steps away, the error hits zero after two informative sweeps, and the third only confirms it.

Q-learning's guarantee is stochastic and stricter. Watkins & Dayan (1992) proved tabular Q-learning converges to the optimal Q* with probability 1, but under two conditions the planner never needed:

  • Infinite exploration — every state–action pair must be visited infinitely often. If exploration ever abandons a pair (e.g. ε decayed to 0 too soon, or a purely greedy behaviour policy), its Q-value can freeze at a wrong estimate and the proof breaks. This is the deep reason the bandit module's explore–exploit lesson never goes away: coverage is a convergence requirement, not a nicety.
  • A decaying learning rate — the step sizes α_t for each pair must satisfy the Robbins–Monro conditions, Σ α_t = ∞ and Σ α_t² < ∞ (sum to infinity so the estimate can travel any distance, but square-sum finite so the noise is eventually averaged out). A constant α, common in practice, does not strictly satisfy this — it keeps Q(s, a) bouncing in a neighbourhood of the truth rather than settling exactly, which is usually a fine engineering trade for faster tracking of a changing environment.

Function approximation forfeits the guarantee. Both proofs assume an exact table. Swap in a function approximator (deep RL) and the contraction argument no longer holds — the projection onto the approximator's representable functions can expand rather than contract error, and the notorious "deadly triad" (function approximation + bootstrapping + off-policy updates) can diverge outright. The stabilising tricks of modern deep RL (target networks, experience replay, clipped updates) exist precisely to claw back the stability the tabular theory gave for free.

Ask the mentor about this module

Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.

Ctrl/Cmd + Enter to send

🎓 Practice ladder

4 graded rungs · ~40 min

Now compute the values yourself. Each rung is a three-panel workspace: instructions on the left, a code editor in the middle, output and test results on the right. Run checks the visible tests; Submit grades against hidden edge cases. You start with a single Bellman backup, iterate it to convergence, then learn the same values from experience with tabular Q-learning; the senior rung diagnoses a reward-shaping bug that makes the agent loop.

Rung 1 — One Bellman backup

Loading exercise…

Rung 2 — Value iteration to convergence

Loading exercise…

Rung 3 — Tabular Q-learning

Loading exercise…

Rung 4 — Reward shaping gone wrong (senior)

Loading exercise…

Try it yourself

A scratch console for this page's ideas — ungraded, nothing you run here is recorded.

Scratch console

A scratch console with the scientific stack (pandas, numpy, scikit-learn). Runs on the server — no network, resource-limited and measured.

Output appears here.

Where next?