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: (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):
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 (): With , we sum the first reward discounted by and add the bootstrap term . Using the inputs , , and , the calculation is . This is the first element of the output tuple.
- Compute the 2-step return (): Since the episode length , the 2-step return uses the full horizon. We sum both rewards ( and ) and add the bootstrap term . The calculation is .
- Calculate the -return (): The formula combines the 1-step and 2-step returns using . It is computed as . Substituting the values: .
- Finalize the -return value: Evaluating the expression from the previous step: . 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.