Deriving Generalized Advantage Estimation From TD Residuals
A config file hands you lambda=0.95. You change it to 0.99, the run gets worse, you change it back, and you still cannot say what the number means. That is…

Key topics
A config file hands you lambda=0.95. You change it to 0.99, the run gets worse, you change it back, and you still cannot say what the number means. That is the symptom. The weak model underneath is "lambda is a smoothing knob." The stronger model is this: lambda is a geometric weight over temporal-difference residuals, and the whole estimator is a telescoping rewrite of a weighted average of k-step advantage estimates. Let's derive it rather than define it.
Notation and the Estimator We Are Trying to Build
Fix the symbols first, because every later step depends on them.
Let be the policy, the discount factor, the state value under , the action value, and
the advantage. The policy gradient needs the advantage because it measures how much better an action is than the policy's average behavior at that state. A baseline subtracted from the return leaves the gradient unbiased in expectation while shrinking its variance.
Define the temporal-difference residual at time :
Read that plainly: it is the surprise between what the critic predicted and what one step actually revealed. If the critic were perfect, would be zero in expectation.
Three assumptions carry the derivation:
- The policy is fixed while the batch is sampled. The advantage we estimate is for the that generated the data.
- The critic is an approximation, not . Every bias term below traces back to this gap.
- Trajectories are finite, so the infinite sum we are about to write must eventually be truncated.
The policy-gradient estimator this plugs into is the standard one:
Everything that follows is about . Given a biased critic, which combination of residuals gives the least damaging advantage estimate?
The k-Step Advantage Estimates and Their Bias-Variance Ladder
Start with the shortest possible estimate. The one-step advantage is exactly the TD residual:
Its bias comes entirely from the critic's error at . Its variance is low because it touches one reward and one value prediction.
Now extend to two steps. Write the two-step return, subtract the baseline , and watch the telescoping:
Add and subtract :
The general form follows the same pattern:
A sum of residuals collapses into a discounted return minus a baseline. That is the telescoping: intermediate value predictions cancel, leaving only the reward stream and the two endpoints.
The endpoints define the ladder:
| Estimator | Bias | Variance | What it trusts |
|---|---|---|---|
| High, from critic error | Low | The critic's one-step prediction | |
| Low under the true policy | High, grows with horizon | The sampled trajectory |
No single is right, because the correct depends on how wrong the critic is. A well-fit critic makes short estimates attractive; a poorly fit critic makes long estimates attractive, at the cost of variance.
Knowledge check
Check your understanding
Answer this question before you continue.
Deriving GAE as a Geometric Average of k-Step Estimates
Instead of picking one , average all of them with exponentially decaying weights:
The factor normalizes the weights so they sum to one: .
Expand the first few terms in residuals:
Now collect by residual index. The residual appears in every estimate, so its coefficient is . The residual appears in all but the first, so its coefficient is . And so on.
Apply the geometric series to each coefficient. The leading cancels the denominators, and the whole expression collapses to:
That is the derivation. The estimator is an exponentially weighted sum of TD residuals, with decay rate .
One structural point worth pausing on: and are not redundant even though they multiply. discounts reward across time — it is a property of the problem. discounts the reliability of the critic's future predictions — it is a property of your trust in the critic. They answer different questions and happen to share a product.
Knowledge check
Check your understanding
Answer this question before you continue.
The Backward Recursion and Why Implementation Looks Nothing Like the Sum
The infinite sum is not computable directly. Factor out the first term:
That is the recursion. Sweep backward over a batch of length , initialize the tail advantage to zero, and you compute every advantage in instead of :
advantages = np.zeros(T)
last_advantage = 0.0
for t in reversed(range(T)):
delta = rewards[t] + gamma * values[t + 1] - values[t]
last_advantage = delta + gamma * lam * last_advantage
advantages[t] = last_advantage
The truncation is honest but biased. The infinite sum is cut at the batch boundary, and the tail advantage is replaced by zero. That is equivalent to assuming the critic is perfect beyond the horizon — which it is not.
Episode termination needs a mask. When an episode ends, the recursion must not bootstrap across the terminal state, because there is no next state to predict. Conflating termination with time-limit truncation corrupts the target: a time-limit cutoff is an artifact of the batch, not a property of the environment.
Here is a five-step worked example. Let , , so . Rewards are , and critic values are .
| 0 | 1 | 0.5 | 0.6 | |
| 1 | 0 | 0.6 | 0.4 | |
| 2 | 2 | 0.4 | 0.8 | |
| 3 | 0 | 0.8 | 0.3 | |
| 4 | 1 | 0.3 | 0.0 |
Sweep backward with :
Verify against the sum: . The recursion and the sum agree.
Knowledge check
Check your understanding
Answer this question before you continue.
What the Critic's Error Does to the Estimator
Write the residual as the true residual plus the critic's error. Let . Then
Substitute into the GAE sum. The error terms accumulate under the same weighting as the residuals:
Lambda controls how far the critic's mistakes propagate into the advantage estimate. That is the real meaning of the parameter.
State the consequence plainly: lambda near 1 does not remove bias, it relocates it. The estimator stops trusting the critic and starts trusting the sampled trajectory. That trajectory is only unbiased if the samples come from the current policy — which is why off-policy reuse breaks the argument for large lambda.
Two failure modes follow:
- Lambda too low with an undertrained critic. The policy receives a systematically wrong signal. It learns slowly, or it learns the wrong thing, and the critic loss falling does not save you.
- Lambda too high with a badly undertrained critic. Advantage estimates become high-variance noise, and the policy gradient happily amplifies it. The run improves, then collapses.
Knowledge check
Check your understanding
Answer this question before you continue.
Choosing Lambda: A Decision Rule Instead of a Default
Lambda around 0.95 is a common default because it keeps the effective horizon of the residual sum finite while retaining most of the Monte Carlo signal. The weights decay as , so the estimator effectively looks about steps ahead. With and , that is roughly 100 steps.
Use the derivation as a diagnostic:
- Critic loss falling, policy not improving. Lambda is probably too low. The policy is being fed the critic's bias.
- Policy improving then collapsing. Lambda is probably too high. Advantage estimates are too noisy.
- Lambda = 0 reduces to one-step TD. Only sensible with a well-fit critic.
- Lambda = 1 is Monte Carlo. Only sensible with short episodes and an accurate policy sample.
One interaction that catches people: a short rollout truncates the sum earlier, so the same lambda behaves differently at different rollout lengths. If you change n_steps and keep lambda fixed, you changed the estimator.
Where the Derivation Stops Being Exact
The derivation assumes a fixed policy during the batch. Off-policy data or heavily reused batches break the unbiasedness argument for large lambda. It also treats as a function of state only; action-dependent critics and distributional critics require a different residual definition.
Most importantly: GAE is a variance-reduction device, not a correctness guarantee. It changes the variance of the gradient estimate, not the direction the gradient points in expectation. If the advantage is biased, GAE does not fix it — it redistributes the bias across the horizon.
Here is your next move. Instrument a training run to log the mean absolute TD residual alongside the policy loss. Sweep lambda across a few values and watch how the two move together. When the residual is large and lambda is low, the policy is learning from a critic that does not know what it is talking about. When the residual is small and lambda is high, you are paying variance for a signal you already had. The math becomes something you can observe rather than something you memorized.
Knowledge check
Final check
Finish the article by checking the ideas you just learned.
References
Build a stronger RL mental model
Use structured resources to connect equations, algorithms, and observable agent behavior.


