← → / SPACE to navigate
LECTURE 03 · MDP, V/Q & BELLMAN EQUATION

MDPs, Bellman Equation,
and Dynamic Programming.

Last time we built the probability toolkit. Today we use it to name every part of the agent–environment loop — state, action, reward, transition, policy — bundle them as the MDP, meet two values V and Q, and write down the equation that ties them together: Bellman.

🧭 S, A, R ⛓️ Markov & MDP 🎮 Doors & tree game 📐 V, Q & Bellman
Title
01 / 25
RECAP · ALL ON ONE DIE

Every probability tool, on one die.

One running example — a fair 6-sided die — touches every concept we built. From outcomes all the way to belief.

① SETUP — \(\Omega, A, P\)

Sample space \(\Omega = \{1,2,3,4,5,6\}\). Pick out an event \(A = \{2,4,6\}\) ("even").

1 2 3 4 5 6
highlighted = event \(A\)
\(P(A) = \tfrac{3}{6} = \tfrac{1}{2}\)
② BUILD — \(X, p_X, \mathbb{E}[X]\)

Define a random variable \(X(\omega)=\omega\). Each face has the same chance — \(p_X(k)=\tfrac{1}{6}\).

1
2
3
4
5
6
distribution \(p_X\) — uniform
\(\mathbb{E}[X] = \sum_k k \cdot \tfrac{1}{6} = 3.5\)
③ UPDATE — \(P(\cdot \mid B)\)

You learn \(B = \{2,4,6\}\). Odd values drop to 0; even values rise to \(\tfrac{1}{3}\) each.

1
2
3
4
5
6
belief after observing \(B\)
\(P(X{=}4 \mid B) = \tfrac{1/6}{1/2} = \tfrac{1}{3}\)

Same die, three lenses. Now we use these tools to formalize the agent–environment loop.

Probability Recap
02 / 25
FRAMEWORK · 3 STEPS

How math tackles a real-world problem.

Almost every applied-math task — from physics to RL — follows the same pattern. Today's lecture walks all three steps, once, end to end.

① MODEL

Real life → math.

Pick symbols and structure so a messy real-world problem becomes a precise math object you can reason about.

Today → MDP · Part 1
② SOLVE

Find a method.

Given the math model, design an algorithm that produces the answer. Try it on small examples first.

Today → Dynamic Programming · Part 2
③ VERIFY

Prove it works.

The math is abstract — but that's the superpower. Proofs of correctness, convergence rates: guarantees, not hope.

Today → Bellman equation · Part 3

Same recipe everywhere — physics: laws → solve → measure. Finance: model → strategy → backtest. RL is no different.

Math Framework
03 / 25
PART 1 OF 3

MDP — the Problem.

First, name every piece of the agent–environment loop. State, action, reward, transition, discount factor — bundled as the Markov Decision Process. This is how we write the problem down precisely.

Part 1: MDP
04 / 25
MDP · INTERFACE

Two players, each with one rule.

Same picture as Lecture 1 — but now we name what lives inside each box. The agent carries a policy \(\pi(a \mid s)\); the environment carries a transition kernel \(P(s' \mid s, a)\). Both are conditional probabilities — exactly the kind we just built.

Agent THE LEARNER 🤖 POLICY π(a | s) Environment THE WORLD 🌍 KERNEL P(s' | s, a) At St+1, Rt+1

At time \(t\) the agent feeds \(S_t\) to its policy \(\pi\) and samples \(A_t\). The environment feeds \((S_t, A_t)\) to its kernel \(P\) and returns \(S_{t+1}, R_{t+1}\). Two conditional distributions, taking turns.

Interface
05 / 25
MDP · STATE

What you see right now.

\[ S_t \in \mathcal{S} \]
A state at time \(t\)
belongs to the state space \(\mathcal{S}\)

How big is the state space?

Small enough to list, or too big to even count.

SMALL · DISCRETE
🎮 Tic-tac-toe: \(\sim\)5,000 board positions
🎲 Snakes & Ladders: 100 squares

Few enough you could write them all down.

HUGE OR CONTINUOUS
⚫ Go: \(\sim 10^{170}\) board positions
🤖 Robot: joint angles + velocities (real numbers)
🚗 Self-driving: camera + lidar + GPS

Real-world tasks usually live here.

Crucially: the state should contain everything you need to decide what to do next. (More on this when we hit the Markov property.)

State
06 / 25
MDP · ACTION

What you can do.

\[ A_t \in \mathcal{A}(S_t) \]
The action you choose
from the legal set in state \(S_t\)

What does an action look like?

Pick from a finite list, or choose a real number.

DISCRETE · PICK FROM A LIST
⬆️ Gridworld: \(\{\)up, down, left, right\(\}\)
⚫ Go: any legal stone placement (\(\sim\)250)

A finite menu of choices.

CONTINUOUS · PICK A NUMBER
🛞 Steering: angle \(\in [-90°, +90°]\)
🦾 Robot arm: 7 joint torques at once

A vector of one or more real numbers.

Note \(\mathcal{A}\) can depend on the state — a chess piece can move differently depending on where it is.

Action
07 / 25
MDP · REWARD

Reward: a function of \((s, a, s')\).

\[ R(s, a, s') \in \mathbb{R} \]
The reward earned on transition
from \(s\) via \(a\) to next state \(s'\)

Does \(R\) always need all three?

Often the action drops out. Sometimes the next state does. The general form covers either case.

R(s') · LANDING TELLS THE STORY
🐭 Maze: \(R = +1\) only when \(s' =\) goal cell
♟️ Chess: \(R = +1/-1/0\) only when \(s'\) is terminal

Where you land tells you everything — the action you chose to get there doesn't matter.

R(s, a) · ACTIONS HAVE THEIR OWN COST
🦾 Robot: each motor command burns a different amount of energy
🚗 Driver: braking, idling, accelerating — different fuel costs

The action you choose has its own cost — regardless of where you end up.

🤔 PAUSE & THINK · ASYMMETRY

Why is \(R(s, a)\) "enough" — but \(R(s')\) sometimes isn't?

R(s,a) absorbs s'; R(s') can't see the action.

Once \((s, a)\) is fixed, the kernel \(P(s' \mid s, a)\) determines \(s'\). So \(R(s, a) := \mathbb{E}_{s' \sim P}[\,R(s, a, s')\,]\) — averaging over \(s'\) loses nothing. But \(R(s')\) can't distinguish two actions that land in the same \(s'\): a robot that spins gently vs hard reaching the same cell gets the same reward, and action costs vanish.

So: \(R(s, a)\) is the canonical form. \(R(s')\) is a special case for goal-only tasks where "where you land" tells the whole story.

tap to reveal

In our warrior tree the reward sits on each cell — that's \(R(s')\). Whatever the form, the agent's only goal is to maximize the sum over time.

Reward
08 / 25
MDP · KERNEL

Next state: the transition kernel.

DEFINITION · TRANSITION KERNEL

\[ P(s' \mid s, a) \;=\; \Pr\!\bigl(\,S_{t+1} = s' \,\big|\, S_t = s,\; A_t = a\,\bigr) \]

For each \((s, a)\), a distribution over next states — \(\sum_{s'} P(s' \mid s, a) = 1\).

ASSUMPTION · MARKOV

t-2St-2 t-1St-1 NOWSt → ? t+1St+1

The kernel uses only the current \((S_t, A_t)\) — past states and actions don't enter.

Is the next state always determined?

Sometimes \((s, a)\) fixes \(s'\). Sometimes the world rolls dice.

DETERMINISTIC · ONE OUTCOME
♟️ Chess: a move always lands on the same board
🧩 Sliding puzzle: a tile slide always gives the same layout

\(P(s' \mid s, a) = 1\) for one \(s'\), 0 for the rest.

STOCHASTIC · A REAL DISTRIBUTION
🎲 Snakes & Ladders: dice decide where you land
🤖 Robot: motor noise & slippage
🌦️ Weather, traffic, opponents

Probability spreads over many possible \(s'\).

A chess board state captures everything — you don't need the move history to decide your next move. The state is sufficient.

Kernel
09 / 25
MDP · DISCOUNT γ

Future rewards matter — but a little less.

Same prize, sooner or later? Most people pick sooner. We bake that preference into the math by discounting each future step by a factor \(\gamma \in [0, 1]\) — the last knob we need before writing the MDP down.

RETURN — TOTAL DISCOUNTED REWARD
\[ G \;=\; R_1 \;+\; \gamma R_2 \;+\; \gamma^2 R_3 \;+\; \gamma^3 R_4 \;+\; \cdots \]

Each step further out picks up one more factor of \(\gamma\). With \(\gamma < 1\) the powers shrink fast, so far-future rewards barely register.

γ = 0 — myopic
\(G = R_1\)

Only the next reward counts. Everything past one step gets multiplied by 0 and disappears.

γ = 0.9 — typical
\(G = R_1 + 0.9\,R_2 + 0.81\,R_3 + \cdots\)

A reward 10 steps away is worth \(0.9^{10} \approx 0.35\) of one now — present-biased, but still long-sighted.

γ = 1 — patient
\(G = R_1 + R_2 + R_3 + \cdots\)

Every step weighs the same. Fine when the task ends (sum stops). Risky when it never ends — the total can blow up.

One number turns "future" into a dial — from "only now" to "all equal." That's all γ does.

Discount
10 / 25
MDP · THE TUPLE

RL's formal model: the MDP.

A Markov Decision Process is just a 5-tuple. If you can write your problem this way, you can solve it with RL.

\[ \big(\,\mathcal{S},\;\; \mathcal{A},\;\; P,\;\; R,\;\; \gamma\,\big) \]
\(\mathcal{S}\)
States

All possible situations

\(\mathcal{A}\)
Actions

All possible moves

\(P\)
Transitions

Rules of the world — \(P(s' \mid s, a)\)

\(R\)
Reward

How well you're doing

\(\gamma\)
Discount

How much future matters

MDP
11 / 25
MDP · RL PROBLEM TYPES

Different types of RL problems.

RL problems vary along a few axes. Where a problem sits on each one decides which methods apply — and how hard it is.

Observability
Full Agent sees the true state, \(O_t = S_t\).
vs
Partial (POMDP) Sees only part of it, \(O_t \neq S_t\).
Model
Known kernel \(P(s'\mid s,a)\) given — we can plan.
vs
Unknown kernel Agent must learn from experience.
Horizon
Finite Bounded steps \(T\) (episodic).
vs
Infinite No terminal step (continuing).

The MDP we just defined sits at full obs, unknown kernel, stochastic — the standard setting we'll work in for this course.

RL Problem Types
12 / 25
RL TYPES · QUIZ

Four games. Place each one.

For each scenario, decide where it sits on the three axes — and name the five ingredients \(\mathcal{S}, \mathcal{A}, R, P, \gamma\). Tap to reveal.

Q1 · TEXAS HOLD'EM (poker)

You're playing a hand of Texas Hold'em.

Partial obs (POMDP) Unknown kernel Finite horizon
\(\mathcal{S}\)
your hand · community cards · pot · stacks · betting history
\(\mathcal{A}\)
fold, check, call, raise (with amount)
\(R\)
chips won or lost when the hand ends
\(P\)
card draws (known) + opponents' decisions (unknown)
\(\gamma\)
\(\approx 1\) — one hand is short

You can never see opponents' hole cards — that's partial observability. The deck is fair, but opponents' strategies are the unknown part of the kernel.

tap to reveal
Q2 · GO (the board game)

You're playing Go on a 19×19 board.

Full obs Unknown kernel Finite horizon
\(\mathcal{S}\)
full board configuration + whose turn it is
\(\mathcal{A}\)
place a stone on any legal intersection (or pass)
\(R\)
\(+1\) win · \(-1\) loss (terminal, otherwise \(0\))
\(P\)
your move = deterministic rules; opponent's reply = unknown
\(\gamma\)
\(\approx 1\) — game length is bounded

The whole board is visible — that's full observability. Rules are deterministic, but the opponent's policy lives inside \(P\), and we don't know it.

tap to reveal
Q3 · THERMOSTAT (keep the room at 70°F)

A smart thermostat runs continuously, switching the heater on or off to keep the room near 70°F.

Full obs Unknown kernel Infinite horizon
\(\mathcal{S}\)
current room temperature (and outdoor temp)
\(\mathcal{A}\)
heater off · heater on
\(R\)
\(-|T - 70|\) — penalty for being far from target
\(P\)
how the room temp evolves given heater state + weather
\(\gamma\)
\(\gamma < 1\) — must discount; the loop never ends

No terminal step — it just keeps running. That's exactly when \(\gamma < 1\) earns its keep: it makes the infinite sum of rewards finite.

tap to reveal
Q4 · LLM (generating an answer)

An LLM produces an answer one token at a time, then a reward model scores the finished response.

Full obs Known kernel Finite horizon
\(\mathcal{S}\)
prompt + tokens generated so far
\(\mathcal{A}\)
pick the next token from the vocabulary
\(R\)
\(0\) until done; reward-model score at the end
\(P\)
deterministic — append the chosen token to the state
\(\gamma\)
\(\approx 1\) — episodes are short and bounded

The transition is just "stick the token on the end" — fully known. That's why post-training methods can plan against the LLM itself.

tap to reveal
RL Types Quiz
13 / 25
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
14 / 25
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
15 / 25
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
16 / 25
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
17 / 25
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
18 / 25
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
19 / 25
DP · OPTIMAL POLICY

Where do those numbers come from?

Same warrior tree (rewards untouched). Violet next to each node = V (state values). Red on each edge = Q (action values). The dashed path \(L\to LR\to LRL\to LRLR\) is the optimal route the math panel walks.

🤺 −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 V*=+17 +26 +18 +10 +20 +14 +14 −5 +5 +10 +6 +8 +5 +7 +9 +17 +12 +18 +26 +18 +6 −14 +10 +20 −1 +14 −3 +14 +3 inside = reward violet (nodes) = V red (edges) = Q leaves: V = 0 (terminal)
π* · IN SYMBOLS
\( V(s) = \max_a Q(s,a) = \max_a \big[\, R(s,a) + V(s') \,\big] \)
V on Tree (det)
20 / 25
DP · RANDOM POLICY

How can we evaluate a random policy?

Same tree, same rewards. Suppose at every state π picks L with probability 0.6, R with 0.4. Max → expectation. Violet = \(V_\pi\). Red = \(Q_\pi\).

🤺 −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 Vπ=−4.1 +6.1 +0.1 −7.8 +9.0 0.0 +2.2 −6.2 −1.6 +9.4 +0.4 −1.0 +0.6 +1.0 −0.6 −2.9 −5.9 +0.2 +15.0 +4.0 −5.8 −15.2 +3.4 +19.4 −6.6 +5.0 −7.4 +8.0 −6.6 inside = reward violet (nodes) = V_π red (edges) = Q_π leaves: V = 0 (terminal)
RANDOM π · IN SYMBOLS
\( V_\pi(s) = \sum_a \pi(a\mid s)\, Q_\pi(s,a) = \sum_a \pi(a\mid s)\,\big[\, R(s,a) + V_\pi(s') \,\big] \)
V on Tree (random)
21 / 25
PART 3 OF 3

Bellman Equation — the Math.

Finally, write it down as one equation: the value of a state equals the reward plus the value of the next state. Recursive, exact, and the foundation of every algorithm in the rest of the course.

Part 3: Bellman
22 / 25
BELLMAN · V AND Q

V and Q define each other.

Two values, one circular definition: V averages Q over actions; Q reaches forward through V. This recursion is the seed of Bellman's equation — the workhorse of RL we'll meet in L4.

V — STATE VALUE · V FROM Q
\( V_\pi(s) = \mathbb{E}_{a\sim\pi}\!\big[\, Q_\pi(s,a) \,\big] \)

The value of being at \(s\) is the average action value at \(s\), weighted by how often the policy picks each action.

Greedy π*: \(V(s) = \max_a Q(s,a)\)
🤺 Slide 12: \(V(\text{start}) = \max(+17, +12) = +17\)
Q — ACTION VALUE · Q FROM V
\( Q_\pi(s, a) = r(s,a) + V_\pi(s') \)

The value of taking \(a\) from \(s\) is the reward you collect now plus the value of the next state.

🤺 At the start, under π*:
\(Q(\text{start}, L) = -9 + V(L) = -9 + 26 = +17\)
Value & Q
23 / 25
BELLMAN · RANDOM KERNEL

When the next state is random.

So far each \((s, a)\) landed at one fixed \(s'\). In general the kernel \(P(s' \mid s, a)\) — from slide 9 — gives a distribution over next states. \(Q\) can't peek at one \(s'\); it has to average over them.

DETERMINISTIC · ONE NEXT STATE
\( Q_\pi(s, a) = r(s, a) + V_\pi(s') \)

The warrior tree: each \((s, a)\) lands at exactly one \(s'\). \(V_\pi(s')\) is one number — no averaging needed.

STOCHASTIC · AVERAGE OVER NEXT STATES
\( Q_\pi(s, a) = r(s, a) + \sum_{s'} P(s' \mid s, a)\, V_\pi(s') \)

Each \((s, a)\) gives a distribution over \(s'\). \(Q\) collects the reward, then takes the expected next-state value: \(\mathbb{E}_{s' \sim P(\cdot \mid s, a)}[V_\pi(s')]\).

📌 SAME PATTERN \(V\) averages \(Q\) over actions (previous slide). Now \(Q\) averages \(V\) over next states. Two layers of expectation — add one more piece (discount \(\gamma\)) and we get Bellman equation.
Random Kernel
24 / 25
BELLMAN · THE EQUATION

Bellman equation: V defines itself.

Slide 14 noticed something circular: \(V\) averages \(Q\) over actions; \(Q\) reaches forward through \(V\). Substitute one into the other and you get Bellman's equation — the most-used equation in all of RL.

Q · DETERMINISTIC KERNEL
\( Q_\pi(s, a) = R(s, a) + V_\pi(s') \)

One next state per \((s, a)\) — no averaging needed.

Q · STOCHASTIC KERNEL
\( Q_\pi(s, a) = R(s, a) + \sum_{s'} P(s'\mid s, a)\, V_\pi(s') \)

\((s, a)\) gives a distribution over \(s'\) — average \(V_\pi\) over them.

📐 γ · DISCOUNT \(\gamma \in [0, 1]\) — future rewards count less by a factor of \(\gamma\) per step. Use \(\gamma < 1\) for infinite tasks (so the tail shrinks); \(\gamma = 1\) works for finite ones like the warrior tree's 4 steps.
Bellman
25 / 25