PIXELBANKv9.1.0
Menu

Problem Statement

Generalized Advantage Estimation is built from one-step TD residuals. Given rewards R_1..R_T, value estimates values of length T+1 (values[T] is the bootstrap/terminal value), and discount gamma, compute the residual at each step:

δt=Rt+1+γV(st+1)−V(st)\delta_t = R_{t+1} + \gamma V(s_{t+1}) - V(s_t)

Implement td_residuals(rewards, values, gamma) returning a list of length T.

Example:

Input:
td_residuals([1.0, 1.0], [0.0, 0.0, 0.0], 0.9)
Output:
[1.0, 1.0]
Reasoning:
  • Identify the parameters from the input: rewards are [1.0,1.0][1.0, 1.0], values are [0.0,0.0,0.0][0.0, 0.0, 0.0], and the discount factor is γ=0.9\gamma = 0.9.
  • Compute the residual for the first step (t=0t=0) using the formula δ0=R0+γV1−V0\delta_0 = R_0 + \gamma V_1 - V_0: substitute the values to get 1.0+0.9(0.0)−0.0=1.01.0 + 0.9(0.0) - 0.0 = 1.0.
  • Compute the residual for the second step (t=1t=1) using the formula δ1=R1+γV2−V1\delta_1 = R_1 + \gamma V_2 - V_1: substitute the values to get 1.0+0.9(0.0)−0.0=1.01.0 + 0.9(0.0) - 0.0 = 1.0.
  • The final output is [1.0, 1.0]

Constraints:

  • len(values) == len(rewards) + 1.
  • Return a list of T floats.
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.
TD Residuals for GAE - Medium | PixelBank