n-step and Lambda Returns
Problem Statement
Given a full episode of rewards R_1..R_T and bootstrap value estimates V(s_0)..V(s_T) (length T+1), compute two prediction targets for time 0.
n-step return: G0(n)=∑k=0n−1γkRk+1+γnV(sn) (if n >= T, use the full Monte-Carlo return with no bootstrap, i.e. gamma^T * V(s_T) added where V(s_T)=0 for a terminal state — but here just apply the formula with V(s_n), and if n >= T set the bootstrap term to gamma^T * values[T]).
lambda return (offline, full episode): G0λ=(1−λ)∑n=1T−1λn−1G0(n)+λT−1G0(T)
Implement n_step_and_lambda(rewards, values, gamma, lam, n) returning the tuple (g_n, g_lambda). rewards has length T, values has length T+1.
Example:
n_step_and_lambda([1.0, 1.0], [0.0, 0.0, 0.0], 1.0, 0.5, 1)
(1.0, 1.5)
- Compute the 1-step return (G0(1)): With n=1, we sum the first reward discounted by γ0 and add the bootstrap term γ1V(s1). Using the inputs R1=1.0, γ=1.0, and V(s1)=0.0, the calculation is 1.0×1.0+1.0×0.0=1.0. This is the first element of the output tuple.
- Compute the 2-step return (G0(2)): Since the episode length T=2, the 2-step return uses the full horizon. We sum both rewards (R1 and R2) and add the bootstrap term γ2V(s2). The calculation is (1.0×1.0)+(1.0×1.0)+(1.0×0.0)=2.0.
- Calculate the λ-return (G0λ): The formula combines the 1-step and 2-step returns using λ=0.5. It is computed as (1−λ)G0(1)+λT−1G0(T). Substituting the values: (1−0.5)×1.0+0.52−1×2.0.
- Finalize the λ-return value: Evaluating the expression from the previous step: 0.5×1.0+0.5×2.0=0.5+1.0=1.5. This is the second element of the output tuple.
- The final output is (1.0, 1.5)
Constraints:
len(values) == len(rewards) + 1.- Clamp
nto at mostTfor the n-step term. - Return a tuple
(g_n, g_lambda)of floats.
1. Background Knowledge
In Temporal-Difference (TD) learning, prediction targets balance bias and variance by mixing immediate rewards with future value estimates. The n-step return G0(n) unrolls the Bellman equation for exactly n steps: it accumulates discounted rewards R1,…,Rn and then bootstraps from the estimated value V(sn). When n exceeds the episode length T, the bootstrap term collapses to the terminal value, effectively recovering the Monte-Carlo return.
The lambda return G0λ generalizes n-step returns by taking a weighted average over all n-step returns from n=1 to T. The weights form a geometric series in λ, with the final term (the full Monte-Carlo return) receiving weight λT−1. This construction ensures that Gλ interpolates smoothly between one-step TD (λ=0) and Monte-Carlo (λ=1), providing a tunable bias-variance trade-off.
A critical detail is the discounting convention. Rewards are discounted by γk where k is the step index relative to time 0. The bootstrap term at step n carries a factor of γn, not γn−1, because it represents the value of the state after n transitions. Confusing these exponents is the most frequent source of bugs in this problem family.
2. Algorithm Approach
The solution follows a direct computation pattern with two independent passes:
- Pass 1 (n-step): Iterate k from 0 to min(n,T)−1, accumulating γkRk+1. Then add the bootstrap term γmin(n,T)V(smin(n,T)).
- Pass 2 (lambda return): Iterate n from 1 to T, computing each G0(n) (or reusing a running sum) and accumulating (1−λ)λn−1G0(n) for n<T, plus λT−1G0(T) for the final term.
A key optimization insight: you can compute all G0(n) values in a single forward pass by maintaining a running discounted reward sum and updating the bootstrap term incrementally, avoiding redundant recomputation.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.