Intuition What this page is
The parent note told you what replay is and why it cures instability. Here we run the machine by hand across every case it can hit : normal transitions, terminal states, the sign of the TD error, the α and β knobs at their extremes, a full mini-batch gradient, and a word problem. If a scenario exists, there is a cell for it below and an example that fills it.
Everything below reuses only symbols the parent already earned. Let us restate the three we lean on hardest, in plain words, before any arithmetic:
Definition The three quantities we compute over and over
TD target y — "the better guess for how good this move was", built as reward now plus discounted best future value:
y = r + γ ( 1 − d ) max a ′ Q θ − ( s ′ , a ′ ) .
Here r is the reward, γ ∈ [ 0 , 1 ) the discount (how much we care about the future), d ∈ { 0 , 1 } the done flag (1 if the episode ended at this step), and Q θ − the target network — a frozen copy of the Q-network.
TD error δ = y − Q θ ( s , a ) — "how wrong my current guess Q θ ( s , a ) was". Positive δ = I under-estimated; negative = I over-estimated.
Priority p = ∣ δ ∣ + ϵ — the size of the mistake (sign discarded), nudged by a tiny ϵ so nothing gets probability zero.
Every cell below is a distinct thing the reader could face. Each example is tagged with the cell(s) it fills.
#
Cell class
Distinguishing input
Filled by
A
Normal non-terminal transition, δ > 0
d = 0 , under-estimate
Ex 1
B
Terminal transition, δ < 0
d = 1 , over-estimate
Ex 2
C
Full mini-batch loss + gradient sign
mixed batch
Ex 3
D
PER probabilities, general α
0 < α < 1
Ex 4
E
Degenerate knob α = 0
uniform limit
Ex 5
F
Degenerate knob α → 1
greedy limit
Ex 5
G
Importance-sampling weights + normalisation
β ∈ ( 0 , 1 )
Ex 6
H
Limiting behaviour of buffer size on correlation
N small vs large
Ex 7
I
Real-world word problem
end-to-end
Ex 8
J
Exam twist: off-policy validity
on-policy trap
Ex 9
We reference these figures as we go:
Worked example Example 1 — Cell A: normal non-terminal, positive error
A sampled transition: reward r = 1 , not terminal (d = 0 ), discount γ = 0.9 , and the target network says max a ′ Q θ − ( s ′ , a ′ ) = 5 . Your current estimate is Q θ ( s , a ) = 4 . Find y and δ .
Forecast: Will δ be positive or negative? (Guess before reading — is 4 too low or too high?)
Compute the target. y = r + γ ( 1 − d ) max a ′ Q θ − ( s ′ , a ′ ) = 1 + 0.9 ⋅ 1 ⋅ 5 = 5.5 .
Why this step? The target injects the discounted future value — this is the bootstrap from the Bellman optimality equation . Because d = 0 , the ( 1 − d ) = 1 factor keeps the future term alive.
Compute the TD error. δ = y − Q θ ( s , a ) = 5.5 − 4 = 1.5 .
Why this step? δ is the learning signal — it is literally the "temporal difference" of Temporal-Difference Learning . Positive means the network under -valued the move.
Verify: δ > 0 matched the forecast (estimate 4 < 5.5 ). Units: everything is in units of value (expected return), consistent. Gradient descent will now raise Q θ ( s , a ) toward 5.5 .
Worked example Example 2 — Cell B: terminal state, negative error
A different sample: r = 2 , terminal (d = 1 ), γ = 0.9 , current estimate Q θ ( s , a ) = 2.3 .
Forecast: How much does the future (max Q θ − ) contribute here?
Kill the bootstrap. y = 2 + 0.9 ⋅ ( 1 − 1 ) ⋅ ( anything ) = 2 + 0 = 2 .
Why this step? A terminal state has no future — the episode is over, so there is nothing to bootstrap. The ( 1 − d ) factor is exactly the switch that zeroes the future term. Forgetting it is a classic bug (the net chases a value that doesn't exist).
TD error. δ = 2 − 2.3 = − 0.3 .
Why this step? Negative δ ⇒ the network over -valued a state that actually just ends. Gradient descent will lower Q θ ( s , a ) .
Verify: With d = 1 the future never entered, so y can't exceed r = 2 ; indeed y = 2 . Sign check: 2.3 > 2 ⇒ δ < 0 . ✓
Worked example Example 3 — Cell C: mini-batch loss and gradient direction
Mini-batch B = { e 2 , e 4 } from the two examples above: δ 2 = 1.5 , δ 4 = − 0.3 . Compute the batch loss.
Forecast: Does the sign of δ matter for the loss? Guess, then check.
Square each error. δ 2 2 = 2.25 , δ 4 2 = 0.09 .
Why this step? We minimise the squared TD error; squaring makes the loss depend only on the magnitude of the mistake — a fully symmetric penalty for over- and under-estimating.
Average over the batch. L = 2 1 ( 2.25 + 0.09 ) = 2 1 ( 2.34 ) = 1.17 .
Why this step? The Monte-Carlo estimate of ∇ θ L needs the mean over the batch (dividing by ∣ B ∣ = 2 ), not the sum — otherwise your step size secretly scales with batch size.
Gradient direction (sign). ∂ ℓ / ∂ Q θ = − 2 δ . For e 2 : − 2 ( 1.5 ) < 0 ⇒ pushes Q θ up . For e 4 : − 2 ( − 0.3 ) > 0 ⇒ pushes Q θ down .
Why this step? This confirms the network moves toward y in both cases — exactly what "shrink δ " means.
Verify: 2 1 ( 1. 5 2 + 0. 3 2 ) = 1.17 . Since squares are non-negative, L ≥ 0 always. ✓
Worked example Example 4 — Cell D: PER probabilities, general
α
Four buffer priorities from errors ∣ δ ∣ = ( 1.5 , 0.3 , 0.3 , 0.9 ) , with ϵ = 0.01 and exponent α = 0.6 . Find each sampling probability P ( i ) .
Forecast: The biggest error is 1.5 vs the smallest 0.31 — about 5 × larger. Will it be sampled 5 × more often, or less?
Raw priorities. p = ∣ δ ∣ + ϵ = ( 1.51 , 0.31 , 0.31 , 0.91 ) .
Why this step? ϵ guarantees even a perfectly-predicted transition (δ = 0 ) keeps a nonzero chance — no data is ever frozen out.
Raise to α . p 0.6 = ( 1.5 1 0.6 , 0.3 1 0.6 , 0.3 1 0.6 , 0.9 1 0.6 ) ≈ ( 1.286 , 0.487 , 0.487 , 0.945 ) .
Why this step? α softens the priorities. At α < 1 the ratio between big and small shrinks — so PER is only partly greedy. This is why 1.5 won't get its full 5 × edge.
Normalise. Sum ≈ 1.286 + 0.487 + 0.487 + 0.945 = 3.205 . Divide: P ≈ ( 0.401 , 0.152 , 0.152 , 0.295 ) .
Why this step? Probabilities must sum to 1; dividing by the total turns raw scores into a distribution over which transition to draw .
Verify: ∑ P = 1 (check). The top error is sampled 0.401/0.152 ≈ 2.6 × more than a low one — less than the raw ≈ 5 × , exactly because α = 0.6 softened it. Forecast confirmed. ✓
Worked example Example 5 — Cells E & F: the two degenerate knob settings
Same priorities p = ( 1.51 , 0.31 , 0.31 , 0.91 ) . Evaluate P ( i ) at the extremes α = 0 and α → 1 .
Forecast: Which α makes PER identical to plain uniform replay?
α = 0 : every p i 0 = 1 , so P ( i ) = 1/4 = 0.25 for all i .
Why this step? Anything to the power 0 is 1 — the priorities vanish, and we recover uniform DQN replay . This is cell E, the "no prioritisation" limit.
α = 1 (fully greedy): P = p / ∑ p = ( 1.51 , 0.31 , 0.31 , 0.91 ) /3.04 ≈ ( 0.497 , 0.102 , 0.102 , 0.299 ) .
Why this step? At α = 1 the raw priority is the (unnormalised) probability — the sharpest, most greedy focus on high-error transitions. This is cell F.
Verify: α = 0 : P = 0.25 each, sum = 1 . ✓ α = 1 : sum = 1 , and the top error's share rose from 0.401 (at α = 0.6 ) to 0.497 — monotonically sharper as α grows, as expected. ✓
Worked example Example 6 — Cell G: importance-sampling weights + max-normalisation
Using the α = 0.6 probabilities P = ( 0.401 , 0.152 , 0.152 , 0.295 ) , buffer size N = 4 , and β = 0.5 . Compute the IS weights w i , then normalise by their max.
Forecast: Which transition gets the smallest weight — the one sampled most or least often?
Raw weight. w i = ( N 1 ⋅ P ( i ) 1 ) β = ( 4 P ( i ) 1 ) 0.5 .
w 1 = ( 1/ ( 4 ⋅ 0.401 ) ) 0.5 = ( 0.6234 ) 0.5 ≈ 0.7896
w 2 = ( 1/ ( 4 ⋅ 0.152 ) ) 0.5 = ( 1.6447 ) 0.5 ≈ 1.2825
w 3 ≈ 1.2825 (same as w 2 )
w 4 = ( 1/ ( 4 ⋅ 0.295 ) ) 0.5 = ( 0.8475 ) 0.5 ≈ 0.9206
Why this step? The over-sampled transition (P 1 largest) gets the smallest weight — we down-weight it to undo the bias of picking it too often. That's the whole point of Importance Sampling .
Normalise by the max. max j w j = w 2 = 1.2825 . Divide all: w ~ ≈ ( 0.6157 , 1.0 , 1.0 , 0.7178 ) .
Why this step? Dividing by max keeps every weight ≤ 1 , so the correction can only shrink gradients — never blow them up. This is a stability trick, not a change of relative sizes.
Verify: The most-sampled transition (e 1 ) got the smallest normalised weight (0.616 ) — forecast confirmed. All w ~ ≤ 1 . ✓ (As β → 1 the weights would spread further, fully correcting the bias.)
Worked example Example 7 — Cell H: limiting behaviour of buffer size on correlation
A car game emits states 3 pixels apart. Buffer D holds the last N transitions. Sample two at random. Estimate the chance they are temporally adjacent (within, say, 5 steps of each other) for N = 10 vs N = 1000 .
Forecast: As N grows 100 × , does adjacency probability go up or down?
Model adjacency. For a random pick, the chance the second pick lands within a fixed window of ± 5 steps around the first is ≈ N 10 (window size over pool size).
Why this step? Adjacent samples are the correlated ones SGD hates — the parent's "Disease 1". We want this small.
Plug in. N = 10 : ≈ 10/10 = 1.0 (basically certain to be near each other). N = 1000 : ≈ 10/1000 = 0.01 .
Why this step? This is the quantitative statement of "bigger buffer ⇒ more i.i.d. -like samples".
Verify: 10/1000 = 0.01 ≪ 1.0 = 10/10 . Larger buffer ⇒ far lower correlation. ✓ Matches the parent's Forecast answer that correlation goes down with 100 × size. (Beware: too large N keeps stale data — the freshness trade-off.)
Worked example Example 8 — Cell I: real-world word problem, end to end
A trading bot uses DQN with replay. It just made a rare, profitable trade: r = + 10 , non-terminal, γ = 0.99 , target-net best future value max Q θ − = 20 . Current estimate Q θ ( s , a ) = 15 . (a) What's δ ? (b) Under PER with α = 1 , ϵ = 0 , if the only other buffered priority is 0.9 , what fraction of draws hit this trade?
Forecast: A rare high-reward event — should PER sample it more or less than average?
Target and error. y = 10 + 0.99 ⋅ 20 = 10 + 19.8 = 29.8 ; δ = 29.8 − 15 = 14.8 .
Why this step? Huge positive δ ⇒ the bot badly under-valued a lucrative move — precisely the "rare informative experience" the parent said replay must not throw away.
Priority. p = ∣ δ ∣ + 0 = 14.8 . Other transition p ′ = 0.9 .
PER probability (α = 1 ). P ( trade ) = 14.8 + 0.9 14.8 = 15.7 14.8 ≈ 0.9427 .
Why this step? PER routes ~94% of compute to the transition it's most wrong about — the rare win gets rehearsed dozens of times instead of once.
Verify: y = 29.8 , δ = 14.8 , P ≈ 0.9427 , and 0.9427 + ( 0.9/15.7 ) = 0.9427 + 0.0573 = 1 . ✓ Rare high-value event is sampled far more — forecast confirmed. Compare DDPG / Soft Actor-Critic , which use the same buffer trick for continuous actions.
Worked example Example 9 — Cell J: exam twist, off-policy validity
Exam question: "You bolt a replay buffer onto vanilla policy gradient (an on-policy method). After training, performance is worse than without replay. Explain quantitatively why, using an importance ratio."
Forecast: Is stored data from an old policy usable as-is to estimate a gradient for the current policy?
Name the distributions. A stored transition was generated by an old policy π old ; policy gradient needs an expectation under the current π θ .
Why this step? Off-policy vs On-policy : on-policy estimators are only unbiased for data from the policy being improved.
The correction that's missing. The unbiased fix is an importance ratio ρ = π old ( a ∣ s ) π θ ( a ∣ s ) . Suppose π θ ( a ∣ s ) = 0.1 but π old ( a ∣ s ) = 0.8 . Then ρ = 0.1/0.8 = 0.125 .
Why this step? Using raw replayed samples silently assumes ρ = 1 . Here the true weight is 0.125 — an 8 × over-count of that action's gradient.
Conclusion. Without multiplying by ρ , the gradient is biased by up to that factor, so training degrades. DQN dodges this because its Bellman target is off-policy by construction (the max makes it independent of the behaviour policy).
Verify: ρ = 0.1/0.8 = 0.125 = 1 . Non-unit ratio ⇒ raw replay is biased for on-policy PG. ✓ This is the parent's "replay isn't universal" mistake, made quantitative.
Recall Self-test across the matrix
Terminal flag d = 1 makes the target equal to what? ::: Just the reward r (the future term is zeroed by ( 1 − d ) ).
At α = 0 , every PER sampling probability equals? ::: 1/ N — plain uniform replay.
The IS weight for the most-sampled transition is the largest or smallest? ::: Smallest (we down-weight over-sampled data).
Why divide IS weights by max j w j ? ::: To keep all weights ≤ 1 so the correction only shrinks gradients (stability).
Doubling buffer size does what to correlation between two random samples? ::: Roughly halves it — more i.i.d.-like.