The previous chapter differentiated a deterministic actor through a learned critic. Stochastic policies and stochastic objectives add a sampled variable between the parameters and the outcome. How can the derivative of an expectation be estimated from those samples?
Two constructions answer the question. Score-function estimators differentiate the log density of the sample, while reparameterization differentiates a sampled outcome written as a function of parameter-free noise.
Learning Goals¶
After reading this chapter, you should be able to:
derive score-function and reparameterization estimators for a stochastic objective;
identify the assumptions that make each estimator unbiased;
compare estimator bias and variance in a controlled numerical experiment.
Prerequisites¶
The chapter assumes familiarity with expectations, probability densities, and multivariate differentiation. Monte Carlo Bellman Estimation reviews sample averages and their variance.
Derivative Estimation for Stochastic Optimization¶
When an objective averages over a parameter-dependent distribution, can its gradient be moved inside the expectation without differentiating the sampling operation directly?
Consider optimizing an objective that involves an expectation:
For concreteness, consider a simple example where and . The derivative we seek is:
While we can compute this exactly for the Gaussian example, this is often impossible for more general problems. We might then be tempted to approximate our objective using samples:
Then differentiate this approximation:
However, this naive approach ignores that the samples themselves depend on . The correct derivative requires the product rule:
While the first term could be numerically integrated using Monte Carlo, the second one cannot as it is not in the form of an expectation.
To transform our objective so that the Monte Carlo estimator for the objective could be differentiated directly while ensuring that the resulting derivative is unbiased, there are two main solutions: a change of measure, or a change of variables.
The Likelihood Ratio Method¶
One solution comes from rewriting our objective using a proposal distribution that does not depend on :
Define the likelihood ratio , where we treat as a separate argument. The objective becomes:
When we differentiate , we take the partial derivative with respect to while holding fixed (since does not depend on ):
The partial derivative of with respect to (treating as fixed) is:
Now fix any reference parameter and choose the proposal distribution . This is a fixed distribution that does not change as varies. We simply evaluate the family at the specific point . With this choice, evaluating the gradient at gives . The gradient formula becomes:
Since is arbitrary, we can drop the subscript and write the score function estimator as:
The Reparameterization Trick¶
An alternative approach eliminates the -dependence in the sampling distribution by expressing through a deterministic transformation of the noise:
Therefore if we want to sample from some target distribution , we can do so by first sampling from a simple base distribution (like a standard normal) and then transforming those samples through a carefully chosen function . If is invertible, the change of variables formula tells us how these distributions relate:
For example, if we want to sample from any multivariate Gaussian distributions with covariance matrix and mean , it suffices to be able to sample from a standard normal noise and compute the linear transformation:
where is the matrix square root obtained via Cholesky decomposition. In the univariate case, this transformation is simply:
where is the standard deviation (square root of the variance).
Common Examples of Reparameterization¶
The Truncated Normal Distribution¶
When we need samples constrained to an interval , we can use the truncated normal distribution. To sample from it, we transform uniform noise through the inverse cumulative distribution function (CDF) of the standard normal:
Here:
is the CDF of the standard normal distribution
is its inverse (the quantile function)
is the error function
The resulting samples follow a normal distribution restricted to , with the density properly normalized over this interval.
The Kumaraswamy Distribution¶
When we need samples in the unit interval [0,1], a natural choice might be the Beta distribution. However, its inverse CDF doesn’t have a closed form. Instead, we can use the Kumaraswamy distribution as a convenient approximation, which allows for a simple reparameterization:
where:
are shape parameters that control the distribution
determines the concentration around 0
determines the concentration around 1
The distribution is similar to Beta(α,β) but with analytically tractable CDF and inverse CDF
The Kumaraswamy distribution has density:
The Gumbel-Softmax Distribution¶
When sampling from a categorical distribution with probabilities , one approach uses noise combined with the argmax of log-perturbed probabilities:
This approach, known in machine learning as the Gumbel-Max trick, relies on sampling Gumbel noise from uniform random variables through the transformation where . To see why this gives us samples from the categorical distribution, consider the probability of selecting category :
Since the difference of two Gumbel random variables follows a logistic distribution, , and these differences are independent for different (due to the independence of the original Gumbel variables), we can write:
The last equality requires some additional algebra to show, but follows from the fact that these probabilities must sum to 1 over all .
While we have shown that the Gumbel-Max trick gives us exact samples from a categorical distribution, the argmax operation isn’t differentiable. For stochastic optimization problems of the form:
we need to be differentiable with respect to . This leads us to consider a continuous relaxation where we replace the hard argmax with a temperature-controlled softmax:
As , this approximation approaches the argmax:
The resulting distribution over the probability simplex is called the Gumbel-Softmax (or Concrete) distribution. The temperature parameter controls the discreteness of our samples: smaller values give samples closer to one-hot vectors but with less stable gradients, while larger values give smoother gradients but more diffuse samples.
Numerical Analysis of Gradient Estimators¶
Let us examine the behavior of our three gradient estimators for the stochastic optimization objective:
To get an analytical expression for the derivative, first note that we can factor out to obtain where . By definition of the variance, we know that , which we can rearrange to . Since , we have and , therefore . This gives us:
Now differentiating with respect to using the product rule yields:
For concreteness, we fix and analyze samples drawn using Monte Carlo estimation with batch size 1000 and 1000 independent trials. Evaluating at gives us , which serves as our ground truth against which we compare our estimators:
First, we consider the naive estimator that incorrectly differentiates the Monte Carlo approximation:
For , we have and . We should therefore expect a bias of about -2 in our experiment.
Then we compute the score function estimator:
This estimator is unbiased with
Finally, through the reparameterization where , we obtain:
This estimator is also unbiased with .
Source
%config InlineBackend.figure_format = 'retina'
import jax
import jax.numpy as jnp
import altair as alt
import numpy as np
import pandas as pd
key = jax.random.PRNGKey(0)
# Define the objective function f(x,θ) = x²θ where x ~ N(θ, 1)
def objective(x, theta):
return x**2 * theta
# Naive Monte Carlo gradient estimation
@jax.jit
def naive_gradient_batch(key, theta):
samples = jax.random.normal(key, (1000,)) + theta
# Use jax.grad on the objective with respect to theta
grad_fn = jax.grad(lambda t: jnp.mean(objective(samples, t)))
return grad_fn(theta)
# Score function estimator (REINFORCE)
@jax.jit
def score_function_batch(key, theta):
samples = jax.random.normal(key, (1000,)) + theta
# f(x,θ) * ∂logp(x|θ)/∂θ + ∂f(x,θ)/∂θ
# score function for N(θ,1) is (x-θ)
score = samples - theta
return jnp.mean(objective(samples, theta) * score + samples**2)
# Reparameterization gradient
@jax.jit
def reparam_gradient_batch(key, theta):
eps = jax.random.normal(key, (1000,))
# Use reparameterization x = θ + ε, ε ~ N(0,1)
grad_fn = jax.grad(lambda t: jnp.mean(objective(t + eps, t)))
return grad_fn(theta)
# Run trials
n_trials = 1000
theta = 1.0
true_grad = 1 + 3 * theta**2
keys = jax.random.split(key, n_trials)
naive_estimates = jnp.array([naive_gradient_batch(k, theta) for k in keys])
score_estimates = jnp.array([score_function_batch(k, theta) for k in keys])
reparam_estimates = jnp.array([reparam_gradient_batch(k, theta) for k in keys])
# Print statistics
methods = {
'Naive': naive_estimates,
'Score Function': score_estimates,
'Reparameterization': reparam_estimates
}
for name, estimates in methods.items():
bias = jnp.mean(estimates) - true_grad
variance = jnp.var(estimates)
print(f"\n{name}:")
print(f"Mean: {jnp.mean(estimates):.6f}")
print(f"Bias: {bias:.6f}")
print(f"Variance: {variance:.6f}")
print(f"MSE: {bias**2 + variance:.6f}")
gradient_data = pd.concat(
[
pd.DataFrame({
"Estimator": name,
"Gradient estimate": np.asarray(estimates),
})
for name, estimates in methods.items()
],
ignore_index=True,
)
estimator_pick = alt.selection_point(fields=["Estimator"], bind="legend")
density = (
alt.Chart(gradient_data)
.transform_density(
"Gradient estimate",
as_=["Gradient estimate", "Density"],
groupby=["Estimator"],
)
.mark_area(opacity=0.45, line=True)
.encode(
x=alt.X("Gradient estimate:Q", title="Gradient estimate"),
y=alt.Y("Density:Q", stack=None),
color=alt.Color("Estimator:N", legend=alt.Legend(orient="top")),
opacity=alt.condition(estimator_pick, alt.value(0.55), alt.value(0.08)),
tooltip=["Estimator:N"],
)
.add_params(estimator_pick)
)
truth = (
alt.Chart(pd.DataFrame({"True gradient": [true_grad]}))
.mark_rule(color="#b91c1c", strokeDash=[6, 4], size=2)
.encode(x="True gradient:Q")
)
(density + truth).properties(
height=340,
title=f"Gradient estimator distributions (θ={theta}, true gradient={true_grad:.2f})",
)
Naive:
Mean: 2.000417
Bias: -1.999583
Variance: 0.005933
MSE: 4.004266
Score Function:
Mean: 3.996162
Bias: -0.003838
Variance: 0.057295
MSE: 0.057309
Reparameterization:
Mean: 3.999940
Bias: -0.000060
Variance: 0.017459
MSE: 0.017459
The numerical experiments corroborate our theory. The naive estimator consistently underestimates the true gradient by 2.0, though it maintains a relatively small variance. This systematic bias would make it unsuitable for optimization despite its low variance. The score function estimator corrects this bias but introduces substantial variance. While unbiased, this estimator would require many samples to achieve reliable gradient estimates. Finally, the reparameterization trick achieves a much lower variance while remaining unbiased. While this experiment is for didactic purposes only, it reproduces what is commonly found in practice: that when applicable, the reparameterization estimator tends to perform better than the score function counterpart.
Summary and Outlook¶
The score-function identity differentiates a log density and applies even when the sampled variable is discrete, but its variance can be large. Reparameterization differentiates the sampled outcome with respect to its parameters and usually has lower variance, but it requires a differentiable sampling path. The numerical comparison separates those variance and bias properties directly.
Entropy-regularized continuous control requires both sampled actions and a learned value signal. Can these estimators train a stochastic actor while avoiding an intractable integral over actions? Regularized and residual-based policy learning develops SAC, path consistency, and related constructions.
Self-checks¶
Solution to Exercise 1
A policy with discrete actions is the standard example: its samples are not differentiable functions of continuous noise, while their log probabilities remain differentiable in the policy parameters.