mediumPROTemporal-Difference Learning and ControlReinforcement LearningRL 1: Foundations→
Problem Statement
Run TD(0) prediction for a single state over a stream of (reward, v_next) transitions, starting from value v0, learning rate alpha, discount gamma. Apply updates sequentially and return the final value.
V←V+α[r+γvnext−V]
Implement td0_stream(v0, transitions, gamma, alpha) where transitions is a list of (reward, v_next) pairs.
Initialize the value estimate V to the starting value v0=0.0.
Process the first transition (r=1.0,vnext=0.0): calculate the TD error as 1.0+0.9⋅0.0−0.0=1.0, then update V by adding α⋅error=0.5⋅1.0, resulting in V=0.0+0.5=0.5.
Process the second transition (r=1.0,vnext=0.0): calculate the new TD error as 1.0+0.9⋅0.0−0.5=0.5, then update V by adding α⋅error=0.5⋅0.5=0.25, resulting in V=0.5+0.25=0.75.
Temporal-Difference (TD) learning is a family of reinforcement learning methods that update value estimates using a "bootstrapping" principle: the current estimate is nudged toward a target that combines the immediate reward with the next state's value estimate, rather than waiting for a full episode to finish. TD(0) is the simplest member of this family. Its update rule is
V←V+α[r+γvnext−V]
where r is the observed reward, vnext is the value of the successor state, γ∈[0,1] is the discount factor, and α∈(0,1] is the learning rate. The term r+γvnext−V is called the TD error; it measures the surprise between what the current value predicts and what the environment actually delivered plus the discounted future.
In a streaming setting, transitions arrive one at a time and the agent must update its value estimate immediately after each transition. This is different from batch learning, where you might average over many samples. Here, the order of updates matters because each new value becomes the starting point for the next update. The discount factor γ controls how much future rewards are valued relative to immediate ones: γ=0 makes the agent myopic, while γ→1 makes it far-sighted.
The key insight is that TD(0) is an online, stochastic approximation algorithm. It does not require knowledge of the full transition dynamics or a complete trajectory; it only needs the current state's value, the immediate reward, and the next state's value. This makes it suitable for large or infinite-horizon problems where storing entire episodes is impractical.
2. Algorithm Approach
The problem is a straightforward sequential update loop. You maintain a single scalar value estimate V that starts at v0. For each transition in the stream, you:
Extract the reward r and the next-state value vnext.
Compute the TD error: δ=r+γvnext−V.
Update the value: V←V+αδ.
After processing all transitions, return the final V. There is no need for additional data structures, bookkeeping, or convergence checks—the problem asks for the exact result of applying the updates in order.
PRO
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
PRO
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.