Temporal-difference prediction
Temporal-difference (TD) prediction is a method to estimate the value function of a fixed policy by bootstrapping from successive state estimates; engineers use it for online prediction, model-free learning when sampling is cheap but full-episode returns are unavailable, and as a building block for
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Temporal-difference (TD) prediction is a core method in reinforcement learning used to estimate the expected return (value) of states under a fixed policy. Engineers use TD prediction for online estimation in systems that interact in real time (robotics, adaptive control, network routing) because it updates estimates after each step and does not need complete episodes.
Key ideas
Start from first principles and simple definitions.
- Agent and environment: the interacting system where the agent takes actions and the environment returns next states and rewards. (We will only use the agent to follow a fixed policy here.)
- Policy (π): a mapping from states to (possibly stochastic) actions. We treat the policy as fixed for prediction.
- Return (G_t): total discounted future reward from time t, defined below. The return is a random variable because rewards and transitions are stochastic.
- Value function (v_π(s)): the expected return when starting in state s and following policy π thereafter: v_π(s) = E[G_t | S_t = s]. This is the quantity we estimate.
- Markov reward process (MRP): An MRP is a Markov chain with rewards, formed by fixing a policy in an MDP. It satisfies the Markov property: the next state and reward depend only on the current state, not on earlier history.
Derivation of the key TD idea (TD(0)):
- Define the return for discount factor γ (0 ≤ γ ≤ 1): G_t = R_{t+1} + γ R_{t+2} + γ^2 R_{t+3} + ···.
- The value satisfies the Bellman equation for v_π: v_π(s) = E[R_{t+1} + γ v_π(S_{t+1}) | S_t = s]. This is an exact relation.
- Monte Carlo (MC) policy evaluation uses sample returns G_t to update: v(s) ≈ v(s) + α (G_t - v(s)). MC waits until episode end to compute G_t.
- Temporal-difference (TD) replaces the full return G_t by a one-step bootstrap target T_t ≡ R_{t+1} + γ v(S_{t+1}). This uses current estimate v for the next state's value.
- Define the TD error (δ_t) as the difference between the bootstrap target and current estimate: δ_t = R_{t+1} + γ v(S_{t+1}) - v(S_t).
- The TD(0) update (for state S_t) is: v(S_t) ← v(S_t) + α δ_t.
Intuition: TD uses available immediate reward plus current prediction for the next state; it therefore ``bootstraps'' from its own estimates and can learn online after every step.
Assumptions and convergence statements commonly required in university answers:
- Assume a finite state set and that the Markov chain induced by π is ergodic (irreducible and aperiodic), ensuring a unique stationary distribution; alternatively, explicitly assume each state is visited infinitely often.
- For tabular TD(0) with a step-size schedule α_t(s) satisfying Robbins–Monro conditions (for each state: sum_t α_t = ∞ and sum_t α_t^2 < ∞) and under the ergodicity assumption, v_t converges with probability 1 to v_π (the true value function). Cite standard stochastic approximation theorems for full proof.
- With constant small α, TD(0) will approach a neighbourhood of v_π; mean-square error contracts under certain spectral properties of the transition matrix.
Compare TD and MC (exam-type statement): TD has lower variance (uses one-step sample + estimate) but is biased (because uses v(S_{t+1}) that is itself an estimate). MC is unbiased but high variance and requires episode termination.
What to draw (diagram): Draw a chain of states S_t → S_{t+1} → S_{t+2} with arrows. Label the arrow from S_t to S_{t+1} with R_{t+1}. Next to each state write its current estimated value v(S_t), v(S_{t+1}). Draw an update arrow from v(S_t) pointing to its new value showing +α·δ_t with δ_t annotated as R_{t+1} + γ v(S_{t+1}) − v(S_t).
Formulas
G_t = R_{t+1} + γ R_{t+2} + γ^2 R_{t+3} + ···
- G_t: return at time t (cumulative discounted reward), unit = same as reward units (often unitless), applies for 0 ≤ γ ≤ 1; for continuing tasks require 0 ≤ γ < 1 to ensure a finite sum (with bounded rewards), while for episodic tasks γ may be 1 if episodes terminate almost surely.
v_π(s) = E[G_t | S_t = s]
- v_π(s): value of state s under policy π, unit = reward units, applies for stationary policy π.
v_π(s) = E[R_{t+1} + γ v_π(S_{t+1}) | S_t = s] (Bellman equation for policy evaluation)
- γ: discount factor (dimensionless), R_{t+1}: reward received (reward units), applies for Markov reward process induced by π.
δ_t = R_{t+1} + γ v(S_{t+1}) - v(S_t) (TD error)
- δ_t: scalar error signal (reward units), applies at each step t for TD(0). For terminal S_{t+1}, set v(S_{t+1}) = 0 (so the target is just R_{t+1}).
v(S_t) ← v(S_t) + α δ_t (TD(0) update)
- α: step-size (learning rate), dimensionless, 0 < α ≤ 1 typically, applies for incremental online updates.
v(s) ← v(s) + α (G_t - v(s)) (Monte Carlo update for comparison)
- G_t: actual return, applies when full-episode returns are available.
Worked examples
Example 1 — One-step MRP numerical TD(0) updates
Problem: A three-state MRP with states A, B, C. Discount γ = 0.9. Transition samples observed: from A we go to B with reward 2.0, then from B to C with reward 1.0. Initial estimates v(A)=0.5, v(B)=0.2, v(C)=0.0. Use α = 0.1. Compute TD(0) update after the transition A → B and then B → C.
Solution:
Step 1: For transition A → B with R = 2.0:
Formula: δ = R + γ v(B) − v(A).
Numbers: δ = 2.0 + 0.9·0.2 − 0.5 = 2.0 + 0.18 − 0.5 = 1.68.
Update: v(A) ← 0.5 + 0.1·1.68 = 0.5 + 0.168 = 0.668.
Units: reward units.
Step 2: For transition B → C with R = 1.0:
δ = 1.0 + 0.9·v(C) − v(B) = 1.0 + 0.9·0.0 − 0.2 = 0.8.
v(B) ← 0.2 + 0.1·0.8 = 0.2 + 0.08 = 0.28.
Final estimates (after two updates): v(A)=0.668, v(B)=0.28, v(C)=0.0.
Answer: v(A)=0.668, v(B)=0.28, v(C)=0.0.
Example 2 — Short exam-style derivation: show TD expected update moves value toward Bellman target Question: Show that the expected TD(0) update for state s (expectation over next state and reward) moves v(s) toward the Bellman target. Model answer:
- TD(0) update: v_{new}(s) = v(s) + α [R_{t+1} + γ v(S_{t+1}) − v(s)].
- Take expectation conditioned on S_t = s: E[v_{new}(s) | S_t = s] = v(s) + α [E[R_{t+1} + γ v(S_{t+1}) | S_t = s] − v(s)].
- Define the Bellman operator T_π; for any v, (T_π v)(s) ≡ E[R_{t+1} + γ v(S_{t+1}) | S_t = s].
- So the expected change is α[(T_π v)(s) − v(s)], which shows that in expectation v moves towards satisfying v = T_π v, i.e., the Bellman fixed point. Moreover, when 0 ≤ γ < 1, T_π is a γ-contraction under the sup-norm, implying a unique fixed point v_π. State assumptions: step-size small and appropriate mixing of chain. Complete answer ends: TD(0) is a stochastic approximation to solving v = T v.
Common mistakes
- Using the same constant α for all states without considering convergence conditions; for theoretical convergence, α_t(s) schedules that satisfy Robbins–Monro are needed.
- Thinking TD is unbiased like Monte Carlo; TD is biased because it bootstraps from estimates.
- Applying TD(0) convergence claims without checking assumptions (finite states, ergodicity, correct step-size conditions).
For your exam
Types of questions: short definitions (define TD error, value function, bootstrap), short notes (compare MC and TD, strengths/weaknesses), derivations (derive TD update from Bellman equation, show expected TD update equals Bellman residual), numericals (compute a sequence of TD updates given rewards/γ/α), diagrams (draw the state transition with TD update displayed), and theoretical proofs (state assumptions and state-convergence result or sketch proof by stochastic approximation).
What a full-marks answer includes:
- Precise definitions of terms used (value function, return, TD error, bootstrap).
- Correct formulas with symbols defined and units noted if asked.
- Statement of necessary assumptions for convergence (finite state, ergodicity, step-size conditions) when discussing proofs.
- Clear step-by-step algebra in derivations, and correct numeric calculations with units and rounding explained.
Quick check
- Give the formula for the TD error δ_t.
- Why is TD called a bootstrapping method? Give one short reason.
- State one assumption needed for the tabular TD(0) convergence theorem.
Answers:
δ_t = R_{t+1} + γ v(S_{t+1}) - v(S_t). TD bootstraps because it uses the current estimate v(S_{t+1}) as part of the target rather than waiting for the full return. One assumption: finite state set with ergodic Markov chain under policy π and step-sizes α_t(s) satisfying Robbins–Monro conditions.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?