Introduction to Reinforcement LearningPart 1 · Foundations

Lecture 4Theory of dynamic programming

Why dynamic programming works: norms, contraction mappings and fixed points, the Bellman operators, how fast value iteration converges, and what a small error in the values can cost a greedy policy when dynamic programming is only approximate.

By the end you should be able to explain why value iteration converges, how fast, and how much a small error in the values can cost a greedy policy.

Key ideas, one per episode

  • Ep. 15Apply a contraction until it stops moving.
  • Ep. 16Exact DP converges. Approximate DP can break, but greedy degrades gracefully.

Based on: Lecture 4: Theoretical Fundamentals of Dynamic Programming, DeepMind × UCL Reinforcement Learning Lecture Series (2021), by Diana Borsa. The slides follow that order of topics, in our own words, figures and examples; they are not official material of the original course.

The title slide of Lecture 4: Theory of dynamic programming

39 slides, written to be read on your own, with worked examples, “try it” boxes, small simulations and check-yourself questions (answers at the end). PDF, 1.1 MB.

Watch

2 short episodes of ACML Explains for this lecture. Pick one from the list, and open the transcript to read along: each timestamp plays the episode from there. The narration is generated with a voice clone of Teddy, used with his permission.

Episode 15 · 5:05

Why dynamic programming works

Drop a map of this room on the floor of this room. Exactly one point lands on itself. That's why value iteration converges.

Apply a contraction until it stops moving.

TranscriptEpisode 15

The map on the floor

Here's a trick. Take a map of this room, and drop it on the floor of this room. Somewhere on that map, there's exactly one point lying right on top of the spot it shows. Exactly one. Always. Wherever the map lands, however it's turned. As long as the whole map lies inside the room. Rotated, crumpled a bit, it doesn't matter. Today's question is why value iteration and policy evaluation always converge. And it's the same reason that map works.

The worst state

First, we need a way to say how far apart two value functions are. Take two guesses for the values, u and v. Compare them state by state, and keep the biggest gap. That's the max norm. The distance between two value functions is the worst state. Two guesses that agree everywhere, except one state where they differ by ten? The distance is ten. Not the average state. The worst. Bit is only as right as his most wrong state. Why not the average? Because a disaster in one state can hide behind a good average.

Contractions

Now, an operator: a machine that takes a whole value function in, and gives a whole value function out. It's a contraction if it pulls any two inputs closer together, by a fixed factor smaller than one. Distance before: d. Distance after: at most α times d, with α below one. And a fixed point is an input that comes out unchanged. T of x equals x. Like a thermostat set to twenty-one degrees. It pushes the room there, and once it's there, it stays. The map on the floor is a contraction of the room. And the point that lies on its own spot? The fixed point.

Banach's theorem

Now the beautiful theorem, from Stefan Banach, nineteen twenty-two. If T is a contraction on a complete space, then first: it has exactly one fixed point. Not two. Not zero. One. Second: start anywhere, apply T, apply it again, and again. You always land on that point. And third, it's fast: after n steps, the error is at most α to the n, times the error you started with. Geometric convergence. Every step chops off the same fraction of what's left. Banach proved this in his doctoral thesis. Not bad, for a thesis.

The Bellman operator

Now look at value iteration again. One sweep takes a value function, and returns a new one. For every state: the max over actions, of the reward plus γ times the expected value of where you land. Call that machine T star, the Bellman optimality operator. Value iteration is just: apply T star, again and again. Picture it: every possible value function is a point in a huge space. T star moves points around in that space. And T star is a contraction, with factor γ. So Banach does the rest.

The proof in one picture

Why is it a contraction? One picture. Take two guesses, u and v, that differ by at most d, anywhere. Average them over the next states: the averages differ by at most d. Averaging never stretches a gap. Multiply by γ: now at most γ times d. Add the same reward to both: the gap doesn't change. And taking the max over actions can't stretch it either. So every sweep shrinks the worst gap by at least γ. That's the whole proof. No calculus. Just three facts. Averages don't stretch gaps. Maxes don't stretch gaps. And γ shrinks them. Three facts come out: there's one fixed point, v star. Value iteration reaches it from any start. And T star is monotone: bigger input, bigger output. Monotone matters too. Start below v star, and you stay below it, creeping up on every sweep.

Our numbers

We tried it on a small random MDP we made up: ten states, two actions, γ at point nine. The error starts at about four point six. After forty sweeps: about point zero six. That's about seventy-five times smaller. And the bound, γ to the k, sits right on top of it the whole way. With γ at point nine, every twenty-two sweeps cut the error at least ten times. With γ at point nine nine, you need about two hundred thirty sweeps for the same cut. Looking far ahead costs computation.

One idea behind DP

Same story for policy evaluation. Drop the max, average over what π picks instead. Call it T π. Also a γ-contraction. Also monotone. Its one fixed point is v π. So iterative policy evaluation converges too, from any starting guess. And the same holds for q functions. So every algorithm from module three is secretly the same thing: apply a contraction until it stops moving. This is why I keep saying this course makes good promises. Here's the proof. Value iteration: apply T star. Policy evaluation: apply T π. Policy iteration: alternate evaluating with T π and improving greedily. But all of this assumed we know p and can store every value exactly. Next time: what breaks when we can't. With me so far?

Interactive lab

Contraction lab

Start two different value functions on a small random MDP and apply the Bellman operator to both, again and again. The distance between them shrinks by at least a factor γ each time, so both end at the same fixed point.

This is the contraction argument of Lecture 4, run instead of proved: ‖T u − T w‖∞ ≤ γ ‖u − w‖∞. The dashed line is the bound γᵏ ‖u₀ − w₀‖∞; the measured distance never goes above it.

Simple math

Two starting points, one fixed point

A small random MDP with six states and two actions. Both value functions, u and w, get the same operator again and again.

The Bellman operators
(T* v)(s) = maxₐ [ r(s, a) + γ Σ p(s′ | s, a) v(s′) ]
T^π averages over π instead of taking the max.

Both are maps from one value function to another; v* and v^π are their fixed points.

A γ-contraction
‖T u − T w‖∞ ≤ γ ‖u − w‖∞

Each application shrinks the largest gap between two value functions by at least a factor γ.

So, after k steps
‖Tᵏ u − Tᵏ w‖∞ ≤ γᵏ ‖u − w‖∞

The gap falls at least geometrically, whatever the start: there is one fixed point and every start reaches it (Banach).

Steps k
0
‖u − w‖∞
–
Last ratio
–
Largest ratio so far
–

u₀ and w₀ are random. Press Apply once.

The gap, on a log scale

Measured ‖uₖ − wₖ‖∞ against the bound γᵏ ‖u₀ − w₀‖∞, and how far u still is from the fixed point.

‖uₖ − wₖ‖∞bound γᵏ ‖u₀ − w₀‖∞‖uₖ − v‖∞ (v: fixed point)

The two value functions

One pair of bars per state. The green tick is the fixed point both are heading to.

uwfixed point