hardPROPolicy Gradients and Advantage EstimationReinforcement LearningRL 1: Foundations→
Problem Statement
Compute GAE(gamma, lambda) advantages for a full trajectory. Given rewards R_1..R_T and values values of length T+1, the advantages satisfy the backward recursion:
Generalized Advantage Estimation (GAE) is a technique used in policy gradient reinforcement learning to balance the bias and variance of advantage estimates. The advantage function A^t measures how much better an action is compared to the baseline value estimate V(st). A naive Monte Carlo return has low bias but high variance, while a single-step temporal difference (TD) error has high bias but low variance. GAE interpolates between these extremes using a decay parameter λ∈[0,1].
The core building block is the TD residual (or TD error) at time step t:
δt=Rt+1+γV(st+1)−V(st)
where γ is the discount factor, Rt+1 is the reward received after taking an action in state st, and V(st), V(st+1) are the value estimates for the current and next states. The GAE advantage is then a weighted sum of future TD residuals:
A^t=∑l=0T−t(γλ)lδt+l
This can be computed efficiently via the backward recursion A^t=δt+γλA^t+1, with the boundary condition A^T=0 (assuming the episode terminates at step T).
In practice, you are given a full trajectory of rewards and pre-computed value estimates. The values array has length T+1 because it includes the value of the terminal state V(sT+1), which is typically 0 for episodic tasks but may be non-zero if the episode is truncated. The rewards array has length T, corresponding to rewards R1,…,RT.
2. Algorithm Approach
The problem is a classic backward dynamic programming or backward recursion pattern. Since A^t depends on A^t+1, you must compute advantages from the end of the trajectory backward to the beginning.
The algorithm follows these principles:
Initialize the advantage at the final step to 0.
Iterate backward from t=T−1 down to t=0.
At each step, compute the TD residualδt using the current reward, the next state's value, and the current state's value.
Update the advantage using the recursion A^t=δt+γλA^t+1.
Store the computed advantage in the output list.
This approach avoids the O(T2) cost of directly summing the geometric series for each t by reusing the previously computed advantage, resulting in an O(T) solution.
3. Step-by-Step Strategy
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.