5.1.11Reinforcement Learning Foundations

Temporal Difference learning

3,397 words15 min readdifficulty · medium6 backlinks

What is Temporal Difference Learning?

The Fundamental TD Update

Let's derive the core TD(0) update from first principles.

Start with the value function definition: Vπ(s)=Eπ[GtSt=s]V^\pi(s) = \mathbb{E}_\pi[G_t \mid S_t = s]

where Gt=Rt+1+γRt+2+γ2Rt+3+G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots is the return.

Key insight: We can rewrite the return recursively: Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1}

Why this step? We're separating the immediate reward from all future rewards, which lets us substitute our estimate of future value.

Substitute into the value function: Vπ(s)=Eπ[Rt+1+γGt+1St=s]V^\pi(s) = \mathbb{E}_\pi[R_{t+1} + \gamma G_{t+1} \mid S_t = s]

Why this step? By linearity of expectation: Vπ(s)=Eπ[Rt+1St=s]+γEπ[Gt+1St=s]V^\pi(s) = \mathbb{E}_\pi[R_{t+1} \mid S_t = s] + \gamma \mathbb{E}_\pi[G_{t+1} \mid S_t = s]

The bootstrapping magic: The second term is just the value of the next state: Vπ(s)=Eπ[Rt+1+γVπ(St+1)St=s]V^\pi(s) = \mathbb{E}_\pi[R_{t+1} + \gamma V^\pi(S_{t+1}) \mid S_t = s]

This is the Bellman equation for VπV^\pi.

Deriving the TD Target

Figure — Temporal Difference learning

The diagram shows why TD works: at each step, we get new information (the actual reward Rt+1R_{t+1}) that makes our estimate more accurate than it was before.

The three estimates in play:

  1. Old estimate: V(St)V(S_t) — what we thought before taking action
  2. Actual sample: We observe Rt+1R_{t+1} — real, experienced data
  3. TD target: Rt+1+γV(St+1)R_{t+1} + \gamma V(S_{t+1}) — combines real reward with our current best guess about the future

Why this is smarter than Monte Carlo:

  • MC waits until episode end to see Gt=Rt+1+γRt+2+γ2Rt+3+G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots
  • TD substitutes V(St+1)V(S_{t+1}) for the infinite sum Rt+2+γRt+3+R_{t+2} + \gamma R_{t+3} + \cdots
  • This reduces variance because we're estimating less (only one step of randomness)
  • But introduces bias because V(St+1)V(S_{t+1}) is not the true value (yet)

TD(0) Algorithm for Policy Evaluation

Worked Examples

Common Mistakes and Misconceptions

TD vs Monte Carlo vs Dynamic Programming

Property TD Monte Carlo Dynamic Programming
Requires model? No No Yes
Bootstraps? Yes No Yes
Online? Yes No N/A
Complete episodes? No Yes N/A
Bias Biased Unbiased N/A
Variance Low High N/A
Convergence Guaranteed* Guaranteed Guaranteed

*with appropriate learning rate schedule

Recall Feynman Explanation (Explain to a 12-year-old)

Imagine you're walking home from school, and you want to guess how long it will take. You could:

  1. Monte Carlo way: Walk all the way home, check your watch, and then say "Oh, it took 20 minutes." Only update your guess after you're done.
  2. TD way: After each block, update your guess. "I thought it'd take 20 minutes total, but I just walked one block in 2 minutes, and I think the rest will take 17 more minutes. So my new guess is 2 + 17 = 19 minutes."

TD is smarter because:

  • You learn during the walk, not just at the end
  • If something unexpected happens (like hitting traffic), you update your guess right away
  • By the time you're halfway home, you already have a pretty good estimate

The "temporal difference" is the difference between what you thought would happen and what actually happened. Every time reality surprises you (good or bad), you adjust your guess a little bit.

Why the fancy math? Rt+1+γV(St+1)V(St)R_{t+1} + \gamma V(S_{t+1}) - V(S_t) is just:

  • Rt+1R_{t+1} = what just happened (like walking one block)
  • V(St+1)V(S_{t+1}) = your guess for the rest of the journey
  • V(St)V(S_t) = your old guess for the full journey
  • The difference tells you if your old guess was too high or too low!

Connection to OtherRL Concepts

Key Takeaways

  1. TD learns from incomplete episodes by bootstrapping from current value estimates
  2. The TD error δt=Rt+1+γV(St+1)V(St)\delta_t = R_{t+1} + \gamma V(S_{t+1}) - V(S_t) drives all updates
  3. TD combines strengths: sampling (like MC) + bootstrapping (like DP) = model-free online learning
  4. Bias-variance tradeoff: TD has lower variance than MC but introduces bias from bootstrapping
  5. Faster propagation: Values spread backward through state space more efficiently than MC

#flashcards/ai-ml

What is the fundamental idea behind Temporal Difference learning? :: TD learning updates value estimates immediately after each step by comparing the current prediction with a new prediction based on the actual reward received plus the estimated value of the next state (bootstrapping). It learns from incomplete episodes.

What is the TD(0) update rule?
V(St)V(St)+α[Rt+1+γV(St+1)V(St)]V(S_t) \leftarrow V(S_t) + \alpha[R_{t+1} + \gamma V(S_{t+1}) - V(S_t)], where the bracketed term is the TD error measuring how much our prediction was off.
What is the TD error formula and what does it represent?
δt=Rt+1+γV(St+1)V(St)\delta_t = R_{t+1} + \gamma V(S_{t+1}) - V(S_t). It represents the difference between our improved estimate (TD target: Rt+1+γV(St+1)R_{t+1} + \gamma V(S_{t+1})) and our old estimate V(St)V(S_t). It tells us how wrong we were.
What is the TD target?
The TD target is Rt+1+γV(St+1)R_{t+1} + \gamma V(S_{t+1}). It's our improved estimate combining the actual observed reward Rt+1R_{t+1} with our current best guess about future value γV(St+1)\gamma V(S_{t+1}).
What is bootstrapping in the context of TD learning?
Bootstrapping means updating an estimate based on other estimates rather than waiting for actual final outcomes. TD uses V(St+1)V(S_{t+1}) (an estimate) to update V(St)V(S_t), rather than waiting for the true return GtG_t.
What are the three key differences between TD and Monte Carlo?
1) TD updates online (each step) vs MC updates after episode; 2) TD bootstraps from estimates vs MC uses actual returns; 3) TD is biased but lower variance vs MC is unbiased but higher variance.
Why does TD learning have lower variance than Monte Carlo?
TD only depends on one step of randomness (R_{t+1} and S_{t+1}), then uses an estimate. MC depends on the randomness of all remaining steps in the episode. Fewer random variables = lower variance.
Why does TD learning have bias while Monte Carlo does not?
TD uses V(St+1)V(S_{t+1}) which is an estimate (not the true value) to compute the target. This introduces bias. MC uses the actual return GtG_t, which is an unbiased sample of the true value.
Is TD learning model-free or model-based and why?
Model-free. TD learns from sampled experience (transitions) without needing to know the transition dynamics P(ss,a)P(s'|s,a) or reward function R(s,a)R(s,a). We observe (St,At,Rt+1,St+1)(S_t, A_t, R_{t+1}, S_{t+1}) directly from environment interaction.
What properties must the learning rate α satisfy for TD convergence?
For guaranteed convergence: t=1αt=\sum_{t=1}^{\infty} \alpha_t = \infty (sufficient learning) and t=1αt2<\sum_{t=1}^{\infty} \alpha_t^2 < \infty (decreasing step sizes). Example: αt=1/t\alpha_t = 1/t or αt=1/n(s)\alpha_t = 1/n(s) where n(s) is state visit count.
How does TD learning propagate value information through state space?
Backward propagation: states near rewards learn first, then their predecessors learn from them, creating a value gradient. Each state "teaches" its predecessor through bootstrapping, allowing faster propagation than MC.
What happens if you set α=1 in TD learning?
With α=1, you get V(St)Rt+1+γV(St+1)V(S_t) \leftarrow R_{t+1} + \gamma V(S_{t+1}), completely replacing the old estimate with a single noisy sample. This causes wild oscillations and prevents convergence—no smoothing over multiple experiences.
What are the advantages of TD over Dynamic Programming?
TD is model-free (doesn't require knowing P(ss,a)P(s'|s,a) or R(s,a)R(s,a)), learns from actual experience, and naturally handles stochastic environments without neding to compute expectations over all possible transitions.
Why might TD error never reach zero even with perfect value estimates?
Due to stochasticity in the environment. Even with correct values, random rewards and transitions cause TD error to fluctuate around zero. Only the expected TD error E[δt]\mathbb{E}[\delta_t] converges to zero, not individual samples.
What is the relationship between TD(0) and the Bellman equation?
TD(0) is a stochastic approximation to the Bellman equation. The TD target Rt+1+γV(St+1)R_{t+1} + \gamma V(S_{t+1}) is a sampled estimate of the right side of the Bellman equation E[Rt+1+γV(St+1)St]\mathbb{E}[R_{t+1} + \gamma V(S_{t+1})|S_t].

Concept Map

defined as expectation of

rewritten as

substituted gives

approximated by

minus old estimate

scaled by alpha drives

relies on

shares learning from experience

shares bootstrapping

enables

Value Function V

Return Gt

Recursive Return

Bellman Equation

TD Target

TD Error delta

TD 0 Update Rule

Bootstrapping

Monte Carlo

Dynamic Programming

Online Updates

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Temporal Difference learning ko samajhne ke liye ek simple examplete hain. Maan lo tum school se ghar jaa rahe ho aur tumhe guess karna hai kitna time lagega. Monte Carlo method mein tum pora walk complete karte ho, tab jake update karte ho—"Oh, 20 minute laga." Lekin TD learning smart hai—har block ke bad tum apna guess update karlete ho: "Ek block mein 2 minute laga, baki ka 17 minute lagega, toh total 19 minute." Real-time learning hai ye.

TD ka core idea hai bootstrapping—matlab tum apne hi estimates seekhte ho. Formula dekho: V(St)V(St)+α[Rt+1+γV(St+1)V(St)]V(S_t) \leftarrow V(S_t) + \alpha[R_{t+1} + \gamma V(S_{t+1}) - V(S_t)]. Yahan Rt+1R_{t+1} actual reward hai jo tumne dekha, aur V(St+1)V(S_{t+1}) tumhara next state ka estimate hai. Dono ko combine karke "TD target" banta hai, jisko tum apne purane estimate V(St)V(S_t) se compare karte ho. Jo difference ata hai, usko "TD error" bolte hain—ye bata hai ki tumhara prediction kitna galat tha.

Ye approach powerful kyon hai? Pehla, episodes incomplete bhi hon tab bhi kaam karta hai—khelne ke bech mein hi seekh lete ho, wait nahi karna padta. Dosra, values jaldi propagate hoti hain backwards—goal ke pas wale states pehle seekhte hain, phir unke pehle wale, aur aise hi chain reaction hoti hai. Monte Carlo mein har state ko khud puri episode khelni padti hai value seekhne ke liye, lekin TD mein ek state apne neighbor seekh leta hai.

Ek common confusion: log sochte hain ki TD error zero ho jayega jab learning complete hogi. Galat! Environment stochastic hai—random rewards aur transitions hain. Toh individual TD errors fluctuate karte rahenge, lekin average zero ke around hoga. Aur learning rate alpha ko carefully choose karna padta hai—zyada b toh values oscillate karenge, bohot chhota toh learning slow hogi. TD learning reinforcement learning ki backbone hai—Q-learning, SARSA, sabhi isi foundation pe khade hain.

Go deeper — visual, from zero

Test yourself — Reinforcement Learning Foundations

Connections