5.1.5 · D3Reinforcement Learning Foundations

Worked examples — Bellman equations

3,213 words15 min readBack to topic

This is the practice companion to Bellman equations. The parent note built the four core equations from scratch. Here we do the opposite of theory: we hunt down every kind of situation the Bellman equations can throw at you and solve one representative of each — including the weird corner cases (terminal states, , , a real-world word problem, and an exam trap).

If any symbol below feels unfamiliar, it was defined in the parent note; skim these prerequisites too: Markov Decision Process, Discount Factor, Bootstrapping.

Recall Quick self-test on the symbols

::: the state at time step ; is the start, is one move later, etc. ::: the reward that arrives one tick after you act at time . ::: expected total discounted reward starting in and always acting by . ::: same, but forced to take action first, then follow . ::: the best possible versions — you get to pick the action that maximises value. ::: probability of landing in and getting reward after doing in — the world's dynamics. (discount) ::: a knob in that shrinks future rewards; multiplies the reward steps away.


The scenario matrix

Every Bellman problem is one (or a blend) of the cells below. The goal of this page is to leave no empty cell.

Cell What makes it special The trap it hides Example
A. Deterministic one action, one outcome — no averaging forgetting still compounds Ex 1
B. Stochastic policy spreads probability over actions weighting each branch by Ex 2
C. Terminal / zero input by definition recursion must stop there Ex 3
D. (degenerate) agent is myopic, ignores the future value collapses to one-step reward Ex 4
E. (limiting) future weighted almost equally sum can blow up unless it terminates Ex 4
F. Optimality / replaces the policy average using instead of Ex 5
G. Real-world word problem you must build the MDP yourself mislabelling reward vs. value Ex 6
H. Exam twist (-update) sample-based bootstrapping mixing target with old estimate Ex 7
I. Self-loop / infinite geometric state can return to itself forever must sum a geometric series Ex 8

We now walk cells A → I. Each example with geometry carries its own figure; the text tells you exactly what to look at in it.


Ex 1 — Cell A: Deterministic chain

Forecast: guess before computing — will be smaller (more negative) or larger than ? Write it down.

Figure — Bellman equations

What the figure shows: four circles in a row, white arrows (each labelled ) pointing right, and under each circle the value we are about to compute. The amber circle is the terminal with value . The cyan arrow along the bottom points left — the direction the recursion actually flows, because each value is built from the one to its right. Keep your eye on that cyan arrow as you read the steps: the numbers fill in from leftward.

Step 1 — Write Bellman for a deterministic move. Because there is exactly one action and one outcome, the double sum collapses to a single term (here for "right" and for the one landing spot): Why this step? Determinism kills all averaging, so the general Bellman equation shrinks to one clean recursive line.

Step 2 — Anchor the recursion at the terminal. (the amber circle). Why? A terminal state has no future, so its value is exactly zero — this is the base case that stops the recursion (Cell C in disguise).

Step 3 — Unroll backwards (follow the cyan arrow in the figure, right to left): Why backwards? Each value depends only on the one after it, and is the only value we already know.

Verify: Sum the discounted rewards directly from : three steps of discounted by : Our forecast: because discounting softens the two later penalties.


Ex 2 — Cell B: Stochastic policy (the parent's grid, finished)

Figure — Bellman equations

What the figure shows: the four cells laid out as a 2×2 grid. From (top-left, cyan) the four white arrows show where each action leads — "right" to , "down" to , and "left"/"up" bounce off the outer wall back into (curved arrows). (amber, bottom-right) is the terminal goal. Reading the four arrows out of is exactly how we count which branches land where — that is where the and below come from.

Setting up the shorthand. By the symmetry of the grid the two "middle" states and have equal value; we give that shared number a short name: So from here on, a lone "" means exactly this shared middle-state value — nothing else. The other unknown keeps its full name .

Forecast: the random policy wanders and bumps walls, so should be larger or smaller than the deterministic ?

Step 1 — Derive the equation for from its four arrows. Read the figure: from , "right" → (value ), "down" → (value ), "left" and "up" bounce off the wall and stay in . Every step costs , each action has probability : Collect terms: the four pieces sum to ; the two -branches give in front of ; the two self-loop branches give in front of : Move the self-term across: , i.e. 0.55\,V^\pi(s_1) = -1 + 0.45\,V. \tag{eq. 1} Why this step? This is where the mysterious coefficients come from: is "1 minus the discounted self-loop weight", and is "the two branches that escape to a middle state, discounted". Nothing is quoted — it is counted off the arrows.

Step 2 — Bellman for the middle state, in terms of . From (by symmetry is identical): "left" returns to (reward ), "down" reaches terminal with (reward ), and "right"/"up" bump walls and stay in (reward each). Each action has probability : Why this step? Every action must be enumerated — including the two wall-bumps that keep you in (Cell B's trap is forgetting them). Note the "down" branch uses the terminal value , not .

Step 3 — Simplify eq. 2. The two self-loop branches give ; the -branch gives ; the terminal branch contributes only its reward: V = -1 + 0.225\,V^\pi(s_1) + 0.45\,V \;\Rightarrow\; 0.55\,V = -1 + 0.225\,V^\pi(s_1). \tag{eq. 2}

Step 4 — Solve the 2×2 linear system. From eq. 1, . Substitute into eq. 2 and solve the two lines together: Why linear algebra? The Bellman expectation equation is linear in the unknown values, so a fixed policy always reduces to solving — the trick that powers policy evaluation in Dynamic Programming.

Verify (use tolerance on rounded values). Plug the exact solutions into eq. 1: holds identically. Numerically, eq. 1: and — match. Eq. 2: and ; the tiny gap is pure rounding of to three decimals — with the exact fractions both sides equal . Forecast answer: is far more negative than — random wandering and repeated wall-bumps pile up many extra penalty steps.


Ex 3 — Cell C: Terminal / zero degenerate input

Forecast: guess — is it , or the reward you earned to arrive there?

Step 1 — Read the definition. — recall is the start state and is the reward that arrives one tick after acting. From a terminal state, no more rewards ever occur, so every . Why this step? Value counts future reward. The reward for entering belongs to the previous state's equation, not to itself — this is the trap.

Step 2 — Conclude. and likewise for all .

Verify: In Ex 1 we used and the direct-sum check still matched . If we had wrongly set (the arrival reward), the recursion would give , contradicting the direct sum . So is correct.


Ex 4 — Cells D & E: Sweeping the discount factor

Figure — Bellman equations

What the figure shows: the curve plotted against the discount on the horizontal axis. Two amber dots mark our worked values ( and ). As you slide right along the curve toward the dashed white line at , watch the curve rocket upward — that vertical blow-up is Cell E's divergence made visible.

Forecast: which gives the biggest value, and does any give infinity?

Step 1 — Bellman for a self-loop. , so . Why this closed form? Solving algebraically is exact — a self-loop is the simplest possible recursion.

Step 2 — Cell D, (myopic). (left amber dot). Why? With the future is worthless; value equals the single immediate reward. The whole collapses to its term.

Step 3 — Middle, . (right amber dot).

Step 4 — Cell E, . (the blow-up at the dashed line). Why? Un-discounted, an endless stream of never stops summing — exactly the divergence Mistake 1 in the parent warns about. This is why we require for continuing tasks.

Verify: geometric sum: . And .


Ex 5 — Cell F: Optimality (max, not average)

Forecast: the immediate reward favours , but which action wins overall?

Step 1 — Compute each action's . With deterministic transitions the model puts probability on one pair, so the Bellman optimality equation for becomes a single term: Why first? — you must know each action's value before you can maximise.

Step 2 — Take the max, not the average. Why max? Under optimality we choose; we do not blend actions with . Using the policy-average would be the trap — that answers "value of a coin-flip policy", not "value of playing optimally". This distinction is the engine of Q-Learning and Temporal Difference Learning.

Verify: and , confirming the greedy choice differs from the average. The larger discounted future () beats the larger immediate reward ().


Ex 6 — Cell G: Real-world word problem

Forecast: should Full be worth more than Low, and by roughly how much?

Step 1 — Build the MDP. States: Full, Low, Dead (terminal, ). Rewards per active hour. This is the modelling step — turning prose into the transition model . Why? Bellman needs explicit transition probabilities before any arithmetic can start.

Step 2 — Bellman for Low (stochastic branch). Solve: Why include the Dead branch as ? The 50% death path contributes reward this hour but no future value (Cell C again).

Step 3 — Bellman for Full (deterministic to Low).

Verify: Re-plug Low: . And Full . Full is worth more than Low (11.67 vs 8.33) — sensible, since Full guarantees one safe extra hour before risk begins.


Ex 7 — Cell H: Exam twist (sample-based Q-update)

Forecast: the reward+future looks like but — how far does actually move? Guess the new value before reading on.

Step 1 — Form the TD target from Bellman optimality. The optimality equation says the true value satisfies . One sampled experience gives a single-sample estimate of that expectation, called the temporal difference target: Why this is the target? We cannot average over all outcomes — we only saw one transition — so we bootstrap: we plug our current estimate of the next state's best value into the Bellman right-hand side. This is Bootstrapping in action.

Step 2 — Move a fraction toward the target (do NOT jump all the way). Why not set straight to ? The target is one noisy sample; blends new evidence with the old estimate so learning is stable. The trap the matrix warns about is over-writing the old value entirely — mixing target with estimate is the whole point of the update.

Verify: TD error ; update ; new . Also .


Ex 8 — Cell I: Self-loop with escape (infinite geometric series)

Forecast: with this looks dangerous — will it diverge like Ex 4's Cell E, or does the guaranteed escape save us? Guess yes/no first.

Step 1 — Write Bellman with a self-reference. The state appears on both sides because one branch loops back: So . Why does not blow up here? Because with prob per step the episode terminates, so the expected number of loops is finite (a geometric distribution) — Cell E's divergence only happens when nothing ever stops. The escape probability is our safety valve.

Step 2 — Solve the linear equation.

Step 3 — See the hidden geometric series. Equivalently, the expected number of loop steps before escaping is (a geometric mean-number-of-failures), each costing , and finally you collect on exit: Why this matches: summing weighted rewards is exactly what solving the self-referential Bellman line does for you — the algebra is the geometric series.

Verify: Both methods give : Bellman , and escape-count .


Recall Which Bellman operator do you use for a

fixed policy vs. the optimal value? Fixed policy ::: expectation, — linear, solvable by linear algebra. Optimal ::: maximum, — non-linear, solved by value/policy iteration.

Related deep dives worth chaining next: Policy Gradient Methods (when you optimise the policy directly instead of via values) and the Hinglish walkthrough.