Policy Gradients
Learning policies directly — REINFORCE, the policy gradient theorem, and variance reduction
One-Sentence Summary
Policy gradients optimize a parameterized policy directly by estimating gradients of expected return through sampled trajectories, avoiding the need for explicit value function maximization.
Why Direct Policy Optimization?
Limitations of Value-Based Methods
Value-based methods (Q-learning, DQN) learn an action-value function
This approach has fundamental limitations:
| Issue | Description |
|---|---|
| Discrete actions only | Argmax requires enumeration over all actions |
| Deterministic policies | Cannot represent stochastic behavior |
| High-dimensional actions | Intractable for continuous control |
| Indirect optimization | Optimizing |
Advantages of Policy Gradients
Policy gradient methods parameterize the policy directly as
- Continuous actions: Natural representation via Gaussian policies
- Stochastic policies: Enable exploration and handle partial observability
- Direct optimization: Gradient ascent on the true objective
- Smoother optimization: Small
changes yield small policy changes

The animation shows how a policy evolves during training, with action probabilities shifting toward the optimal action as the agent learns.
Policy Parameterization
The Policy Network
A parameterized policy
Discrete Actions: Softmax Policy
For discrete action spaces, output logits and apply softmax:
where
Continuous Actions: Gaussian Policy
For continuous actions, output mean and variance of a Gaussian:
The network outputs
Log probability for gradients:
The Policy Gradient Theorem
Objective Function
We want to maximize expected cumulative reward (return):
where
The Gradient
The key insight: we can compute the gradient of
Derivation Intuition
The derivation uses the log-derivative trick:
Starting from the objective:
Taking the gradient:
Applying the log-derivative trick:
Since
This yields the policy gradient theorem:
Key insight: The gradient is an expectation, so we can estimate it by sampling trajectories.
Policy gradient ascent in parameter space: arrows show gradient directions pointing toward regions of higher expected return.
REINFORCE Algorithm
Algorithm Overview
REINFORCE (Williams, 1992) is the simplest policy gradient algorithm:
Algorithm: REINFORCE
─────────────────────────────────────
1. Initialize policy parameters θ
2. For each episode:
a. Generate trajectory τ = (s₀, a₀, r₀, ..., s_T)
b. Compute return R(τ) = Σᵢ γⁱrᵢ
c. For each timestep t:
- Compute gradient: ĝ = R(τ) ∇_θ log π_θ(aₜ|sₜ)
- Update: θ ← θ + α ĝMonte Carlo Estimation
The gradient estimate uses complete episodes (Monte Carlo):
Properties:
- Unbiased:
- High variance: Single trajectory estimates are noisy
- On-policy: Requires fresh samples after each update
Causality Improvement
Actions at time
This uses

REINFORCE in action: the policy updates based on episode returns, gradually concentrating probability on better actions.
The Variance Problem
Why High Variance?
Policy gradient estimates suffer from high variance because:
- Stochastic actions: Randomness in action sampling
- Stochastic transitions: Environment dynamics
- Rare events: Important but infrequent rewards
Consequence: Gradient estimates swing wildly between updates, causing unstable learning.
Baseline Subtraction
We can subtract any function
Why this works: The subtracted term has zero expectation:
Common Baselines
| Baseline | Formula | Variance Reduction |
|---|---|---|
| Constant | Moderate | |
| State-dependent | High | |
| Moving average | Moderate |
Optimal baseline: The value function
The Advantage Function
Definition
The advantage function measures how much better an action is compared to the average:
where:
= expected return starting from following = expected return starting from following
Advantage Actor-Critic Gradient
Using advantage as the weighting term:
Intuition:
: Action better than average — increase probability : Action worse than average — decrease probability : Action is average — no change
Advantage Estimation
Since we do not know the true advantage, we estimate it:
TD(0) Advantage Estimate:
Generalized Advantage Estimation (GAE):
where
GAE interpolates between high-bias/low-variance (
Policy Gradients for Continuous Actions
Gaussian Policy Implementation
For continuous control, we typically use a diagonal Gaussian:
The network outputs:
- Mean:
— deterministic component - Log std:
— learned or fixed
Score Function for Gaussian
The gradient of log probability:
Interpretation:
- If
: Increase to make more likely - Magnitude scales inversely with
(more certain = larger gradients)
Exploration-Exploitation
| Approach | Method | Trade-off |
|---|---|---|
| Entropy bonus | Add | Encourages exploration |
| Fixed variance | Simpler but less adaptive | |
| Learned variance | Network outputs | Adapts exploration over time |
Visual Summary
Interview Questions
Q1: "Why do we need the log-derivative trick in policy gradients?"
Answer:
The log-derivative trick converts a gradient of a probability distribution into an expectation we can sample from.
The problem: We want
, but the expectation itself depends on : The solution: Using
: Now we can estimate this gradient by sampling actions from our policy. Without this trick, we would need to compute gradients through the sampling process itself, which is not differentiable.
Q2: "What is the advantage function and why does it reduce variance?"
Answer:
The advantage function
measures how much better action is compared to the average action under policy in state . Variance reduction mechanism:
Centering: By subtracting
, we center the reward signal around zero. Good actions get positive weights, bad actions get negative weights. Credit assignment: Instead of weighting by absolute return (which includes rewards unrelated to the current action), we weight by relative performance.
Reduced scale: Returns can be very large or vary wildly across episodes. Advantage values are typically smaller and more consistent.
Example: If baseline return is 100 and we get 102, raw weighting uses 102 while advantage uses +2. The smaller magnitude means less variance in gradient estimates.
Q3: "Compare REINFORCE to Actor-Critic. When would you use each?"
Answer:
Aspect REINFORCE Actor-Critic Value function None Learned critic Bias Unbiased Biased (critic error) Variance High (Monte Carlo) Lower (TD estimates) Sample efficiency Low Higher Complexity Simple Two networks to train Use REINFORCE when:
- Episodes are short (low variance naturally)
- Simplicity is prioritized
- Debugging/understanding the algorithm
Use Actor-Critic when:
- Episodes are long (need variance reduction)
- Sample efficiency matters
- Continuous control tasks
- Building toward PPO/A3C
Key insight: Actor-Critic trades unbiasedness for variance reduction. The bias introduced by an imperfect critic is usually preferable to the high variance of REINFORCE in practice.
Quick Reference Card
POLICY GRADIENT CORE
─────────────────────────────────────────────────────────────
Objective: J(θ) = E_τ[R(τ)]
Gradient: ∇J = E[R(τ) Σ_t ∇log π_θ(aₜ|sₜ)]
Log-trick: ∇π = π ∇log π
REINFORCE
─────────────────────────────────────────────────────────────
Update: θ ← θ + α R(τ) ∇log π_θ(a|s)
Reward-to-go: Use Gₜ = Σ_{t'≥t} γ^(t'-t) r_{t'}
Properties: Unbiased, high variance, on-policy
VARIANCE REDUCTION
─────────────────────────────────────────────────────────────
Baseline: Subtract b(s) — zero expected gradient
Best baseline: V^π(s) — value function
Advantage: A(s,a) = Q(s,a) - V(s)
CONTINUOUS ACTIONS
─────────────────────────────────────────────────────────────
Gaussian: π(a|s) = N(μ_θ(s), σ²)
Log-prob: -½[(a-μ)²/σ²] - log σ - ½log(2π)
Exploration: Entropy bonus, learned σ
ALGORITHMS HIERARCHY
─────────────────────────────────────────────────────────────
REINFORCE → Actor-Critic → A2C/A3C → PPO/TRPO
(simple) (baseline) (parallel) (constrained)Key Takeaways
Policy gradients optimize policies directly by computing gradients of expected return through sampled trajectories, enabling continuous actions and stochastic policies.
The policy gradient theorem provides an unbiased gradient estimate using the log-derivative trick: weight each action's score function by its return.
Variance is the central challenge — REINFORCE estimates are unbiased but noisy. Baselines and advantage functions dramatically reduce variance without introducing bias.
The advantage function measures relative action quality, providing both variance reduction and intuitive credit assignment.
Actor-Critic methods trade a small amount of bias (from critic errors) for significant variance reduction, forming the foundation for modern algorithms like PPO and A3C.