← → / SPACE to navigate
LECTURE 04 · VALUES & OPTIMALITY

Optimality & the
Bellman equation.

We have the MDP — states, actions, rewards. But what makes one policy better than another, and what is the best one? Build the value function, play the doors and warrior-tree games to see why greedy ≠ optimal, and write the Bellman equation that pins down \(\pi^*\).

Reading: Sutton & Barto, Ch 3.5–3.6

Title
1 / 13
FORMAL SETUP

A discounted MDP.

Everything today lives inside one object \(\mathcal{M}=(\mathcal{S},\mathcal{A},\mathbb{P},c,\gamma)\) — five pieces that fully describe the world.

\(\mathcal{S}\)
states
\(\mathcal{A}\)
actions
\(\mathbb{P}(\cdot\!\mid\! s,a)\)
transition kernel
\(c(s,a)\)
one-stage cost
\(\gamma\in(0,1)\)
discount
A POLICY DRIVES THE TRAJECTORY

A policy \(\pi(a\!\mid\! s)\) is the probability of choosing action \(a\) at state \(s\). Fix \(\pi\), and the world rolls forward:

\[ a_t \sim \pi(\cdot\!\mid\! s_t), \qquad s_{t+1} \sim \mathbb{P}(\cdot\!\mid\! s_t, a_t). \]

pick an action · pay a cost · land somewhere new · repeat

The MDP
2 / 13
TRY THE KNOB

What does γ change?

γ was the fifth element of \(\mathcal{M}\) we just listed. It sets how much a cost one step later still counts: a cost \(t\) steps away is weighted \(\gamma^t\). Slide it and watch how far the agent "sees."

0.90 discount γ

now
+1
+2
+3
+4
+5
+6
+7

Small γ → the agent only cares about the next step or two. γ near 1 → distant costs matter almost as much as today's.

Discount γ
3 / 13
DEFINITION

The value function.

\(V^\pi(s)\) is the total cost you expect to pay if you start in state \(s\) and follow \(\pi\) forever — with costs further in the future discounted by \(\gamma\).

\[ V^\pi(s) \;:=\; \mathbb{E}_\pi\!\left[\, \sum_{t=0}^{\infty} \gamma^{t}\, c(s_t, a_t) \;\Bigm|\; s_0 = s \,\right] \]
\(c(s_0,a_0)\)
\(s_0\)
weight 1
\(\gamma\, c(s_1,a_1)\)
\(s_1\)
weight γ
\(\gamma^2 c(s_2,a_2)\)
\(s_2\)
weight γ²
\(\gamma^3 c(s_3,a_3)\)
\(s_3\)
weight γ³
⋯ +

add up every future cost — but each step ahead counts a little less (γ < 1)

Value Function
4 / 13
PART 2 OF 3

Dynamic Programming —
the Algorithm.

Now, solve the problem. Walk backward from the future: compute the value of every state by reusing values you've already computed one step ahead. That's dynamic programming.

Part 2: DP
5 / 13
DP · DOORS GAME

Pick a door. Four rooms.

Two doors at every step. Each has a peephole — peek to see the reward inside. Step through to claim it; two new doors appear. 4 picks total. Highest score wins.

Step1 / 4
Path so far
Total reward0

The peephole shows only this step's reward — not what waits behind the next door. Easy to grab the big number now and lose later.

Doors Game
6 / 13
DP · TREE GAME

The warrior's greedy trap.

Take 4 steps. At each step, go left or right. Each cell pays a reward — a 🪙 coin (+5 to +10) or 💀 boss (−10 to −5). Goal: finish with the highest total. There are \(2^4 = 16\) possible paths.

🤺 −9 −6 +8 +6 +4 −8 −9 +5 +10 −7 +6 −8 +7 −6 −7 −5 −6 +5 +9 +10 +6 −8 −7 +8 +5 −6 +7 −8 −7 +9 🪙 coin (+5..+10) 💀 boss (−10..−5) greedy path: pick the biggest reward at each step
Q1 · GREEDY

Sum the rewards along the red path. What total does greedy end with?

Q2 · HOW TO AVOID GREEDY?

What information would let you make the right choice at every step?

Warrior Test
7 / 13
DP · KEY INSIGHT

Don't be greedy — plan ahead.

Greedy fails because it judges each move by this step's reward only. The fix in one sentence: at every step, pick the move whose whole future looks best. Today's choice is judged by where it eventually leads — all the way to the end.

😬 GREEDY

"Take the biggest reward I can see now." Looks one step ahead. Walks straight into a boss two steps later.

🧭 PLANNING

"Take the move whose total future reward is highest." Looks all the way to the leaves before committing the first step.

🤔 THINK Easy to say. But how would you actually compute "the total future reward of a move" on the warrior tree? Try the most direct idea you can think of — we'll try yours next.
Plan, Don't Be Greedy
8 / 13
DP · BRUTE FORCE

First idea: try every plan.

A plan is a fixed 4-step sequence of L/R moves. There are \(2^4 = 16\) plans. For each one, sum the four rewards along its path; pick the plan with the highest total.

A FEW OF THE 16 PLANS
LLLL
(−9) + (+8) + (−9) + (−7)
= −17
LRLL
(−9) + (+6) + (+10) + (+9)
= +16
LRLR
(−9) + (+6) + (+10) + (+10)
= +17
RRRR
(−6) + (−8) + (−6) + (+9)
= −11
… 12 more …
enumerate all 16, sum each
take the max
⚠️ Q · EFFICIENCY

4 steps gave 16 plans. What if the warrior took 10 steps? 20? 50? How many plans must we sum?

📐 ANSWER

\(2^{10}{=}1{,}024\) · \(2^{20}{\approx}10^6\) · \(2^{50}{\approx}10^{15}\). Exponential — brute force is hopeless almost immediately. We need a smarter way.

Enumerate Every Plan
9 / 13
DP · WHEN GREEDY WINS

Greedy does work — on a 1-step tree.

Greedy got trapped on the big tree because each move hides a long future. Strip the future to nothing — make the problem one step — and greedy snaps back to optimal. Three observations turn that into a plan.

① ONE STEP LEFT — DEFINE V

If the tree is just one step deep, the best total reward starting from \(s\) is just the larger reward. Call this best total \(V(s)\) — the value of state \(s\). No future, no trap.

🤷 +4 +6
\( V(s) = \max_a R(s,a) = +6 \)
② WALK BACKWARDS

What if every next state \(s'\) came tagged with its value \(V(s')\) — the best total starting from there? Then any step is again a 1-step problem: pick the action with the biggest reward + value.

🤷 −9 + V(s') −6 + V(s')
\( V(s) = \max_a \big[\, R(s,a) + V(s') \,\big] \)
🤔 PROBE At the leaves the future is 0 (no more steps), so depth-3 is already a 1-step problem — solve it. Now depth-2 sees depth-3’s “future” and is also 1-step. Roll up layer by layer until the root — this is dynamic programming. Next slide does it on the warrior tree.
When Greedy Wins
10 / 13
BELLMAN OPTIMALITY

From a fixed policy to the best one.

Evaluation answered "how good is this π?" Now ask "what's the best we can do?" Same recursion, but instead of averaging over what π would do, we pick the best action at every state — \(\sum_a\pi(a\mid s)\) becomes \(\max_a\):

BELLMAN OPTIMALITY · ONE STATE \(s\) AT A TIME
\[ V^*(s) \;=\; \max_a\, \underbrace{\big[\, r(s,a) \;+\; \gamma \sum_{s'} \mathbb{P}(s'\!\mid\! s,a)\, V^*(s') \,\big]}_{Q^*(s,a)} \qquad \pi^*(s) = \arg\max_a Q^*(s,a) \]

the best value = best action's value; the best policy = the action that attains it

🤺 −9 −6 +8 +6 +4 −8 −9 +5 +10 −7 +6 −8 +7 −6 −7 −5 −6 +5 +9 +10 +6 −8 −7 +8 +5 −6 +7 −8 −7 +9 inside = reward violet (nodes) = V* red (edges) = Q* dashed = optimal path π* · click a box to check
SAME TREE · NOW TAKE THE MAX
\( V^*(s) = \max_a Q^*(s,a),\qquad Q^*(s,a) = r(s,a) + \gamma\, V^*(s') \)

Roll up the same way, but each node now keeps the better of its two children instead of averaging. The greedy-looking root \(V^*=+17\) beats the random policy's \(-4.1\); reading \(\arg\max\) at each node traces the dashed optimal path \(\pi^*\).

Optimality
11 / 13
POLICY EVALUATION

The Bellman equation.

Split the infinite sum into the cost paid now plus the discounted value of wherever you land next — averaged over the action you pick (\(\pi\)) and the move that follows (\(\mathbb{P}\)). Written one state at a time:

ELEMENT-WISE · ONE STATE \(s\) AT A TIME
\[ V^\pi(s) = \sum_a \pi(a\!\mid\! s)\Big[\, \underbrace{c(s,a)}_{\text{pay now}} + \gamma \underbrace{\sum_{s'}\mathbb{P}(s'\!\mid\! s,a)\,V^\pi(s')}_{\text{discounted future}} \,\Big] \]

stack all \(|\mathcal{S}|\) rows → matrix form \(\;V^\pi = c_\pi + \gamma\,\mathbb{P}_\pi V^\pi\) (with \(c_\pi,\mathbb{P}_\pi\) the \(\pi\)-averaged cost and kernel)

🤺 −9 −6 +8 +6 +4 −8 −9 +5 +10 −7 +6 −8 +7 −6 −7 −5 −6 +5 +9 +10 +6 −8 −7 +8 +5 −6 +7 −8 −7 +9 inside = reward violet (nodes) = V_π red (edges) = Q_π leaves: V = 0 · click a box to check
FIXED π · ONE BACKWARD PASS
\( V_\pi(s) = \sum_a \pi(a\mid s)\,\big[\, r(s,a) + \gamma\, V_\pi(s') \,\big] \)

Last lecture's tree, in rewards (here \(r=-c\), so maximize). Fix \(\pi=\) "go L with 0.6, R with 0.4." Start at the leaves (\(V=0\)), then fill each parent: its own reward plus the averaged value below. The root's \(V_\pi=-4.1\) is exactly the Bellman equation, solved bottom-up. The kernel here is deterministic, so the \(\sum_{s'}\) collapses to one term — all the randomness lives in \(\pi(a\mid s)\).

Bellman
12 / 13
A BIT OF HISTORY · 1953

Why is it called dynamic programming?

👨‍🔬
Richard E. BellmanRAND Corporation · 1950s
I was interested in planning… but "programming" was a respectable word. The Secretary of Defense had a pathological fear of the word "research." So I used "dynamic" — it's impossible to use in a pejorative sense. It was a name no one could object to.
— Bellman, Eye of the Hurricane (paraphrased)

The Principle of Optimality

Bellman's one big idea: the rest of an optimal plan, from any state onward, must itself be optimal — so a long problem splits into best-first-move + optimal-tail:

\( V^*(s)=\min_a\big[\,\underbrace{c(s,a)}_{\text{first step}}+\gamma\,\underbrace{V^*(s')}_{\text{optimal tail}}\,\big] \)

If the best first step from 🤺 is L, reuse L's own value \(V^*(L)\) as-is — never recompute it.

Why it still matters (the cool part)

Scoring every plan explodes: \(2^H\) plans in a depth-\(H\) tree, \(A^{|\mathcal S|}\) policies in an MDP. Bellman never enumerates them:

ENUMERATE PLANS\(2^{H}\)exponential · impossible
SOLVE BELLMAN\(|\mathcal S|\)linear · one lookup / state

Each value is computed once and reused, so \(2^H\) collapses to \(|\mathcal S|\) lookups — the engine behind every RL algorithm here.

History
13 / 13