Tutorials › Econometrics (Graduate) › Smooth Simulated Likelihood and Gradient Computation

Simulation-Based Estimation · Tutorial 899 of 952

Smooth Simulated Likelihood and Gradient Computation

Learn to compute and assess gradients of simulated likelihoods using pathwise differentiation and finite-difference methods.

Advanced 12 min read

What You'll Learn

  • Define a simulated likelihood contribution using a fixed set of simulation draws.
  • Derive the pathwise gradient and state conditions for exchanging differentiation and expectation.
  • Differentiate a simulated log likelihood and distinguish its gradient from an unbiased score estimate.
  • Use central finite differences with common random numbers and recognize their numerical trade-offs.
  • Explain why indicator-based simulators can have unusable gradients and how conditional smoothing can help.

Why Smoothness Matters

A simulated likelihood replaces an integral that is difficult to evaluate with a calculation based on random draws. Estimation then requires optimizing that calculated likelihood over model parameters. The quality of this optimization depends not only on how accurately the likelihood is simulated, but also on how it changes as the parameters change. A simulator that produces a jagged objective or a gradient that is zero over broad regions can make optimization unreliable, even when the likelihood values look plausible.

The central practical idea is to use the same underlying simulation draws across nearby parameter values and, where possible, to construct the simulator as a differentiable function of the parameters. This makes it possible to compute gradients directly or to approximate them with finite differences. Smoothness does not by itself guarantee valid inference, but it makes optimization and the calculation of derivative-based quantities more workable.

Simulated Likelihood and Its Gradient

Let \(y_i\) denote the observed outcome for unit \(i\), and let \(\theta\) be a vector of model parameters. Suppose the likelihood contribution is an expectation over latent randomness \(U\), with distribution that does not depend on \(\theta\):

$$ p_i(\theta)=\mathbb{E}\left[h_i(\theta,U)\right]. $$

Here \(h_i(\theta,U)\) is the contribution produced by a draw \(U\). With \(R\) draws \(u_1,\ldots,u_R\), the simulated contribution is

$$ \widehat p_{i,R}(\theta)=\frac{1}{R}\sum_{r=1}^{R}h_i(\theta,u_r). $$

The same draws \(u_1,\ldots,u_R\) should be used when evaluating this expression at nearby parameter values. For independent observations, the simulated log likelihood is

$$ \widehat\ell_R(\theta)=\sum_{i=1}^{n}\log \widehat p_{i,R}(\theta), $$

provided every simulated contribution is positive. The logarithm is applied to each simulated likelihood contribution; in general, averaging log contributions is not equivalent to taking the log of their average.

Definition: A smooth simulated likelihood is a simulated likelihood whose contribution changes differentiably, or at least smoothly enough for the intended numerical method, as the parameter vector changes while the simulation draws are held fixed. Smoothness concerns the simulated function being optimized; it does not mean that every possible simulator or every finite-draw approximation is automatically smooth.

When \(h_i\) is differentiable in \(\theta\), a direct pathwise gradient is available. Under conditions that allow differentiation to pass through the expectation,

$$ \nabla_\theta p_i(\theta) = \mathbb{E}\left[\nabla_\theta h_i(\theta,U)\right], \qquad \nabla_\theta \widehat p_{i,R}(\theta) = \frac{1}{R}\sum_{r=1}^{R}\nabla_\theta h_i(\theta,u_r). $$

A standard sufficient condition is that \(h_i(\theta,U)\) is differentiable in a neighborhood of the parameter value and its derivative is bounded in magnitude by an integrable function of \(U\) throughout that neighborhood. This permits interchange of differentiation and expectation. The resulting simulated gradient averages the derivatives evaluated at the draws; it is not obtained by differentiating a fresh, independently randomized simulation at each parameter value.

Sometimes the latent variable itself depends on the parameter. If it can be written as \(Z=T(\theta,U)\), where \(U\) has a parameter-independent distribution, then the chain rule gives

$$ \nabla_\theta h_i\bigl(\theta,T(\theta,U)\bigr) = \partial_\theta h_i\bigl(\theta,T(\theta,U)\bigr) + D_z h_i\bigl(\theta,T(\theta,U)\bigr)D_\theta T(\theta,U). $$

The first term differentiates the function directly with respect to the parameters; the second accounts for the parameter dependence of the simulated latent variable. This is often called pathwise differentiation or reparameterization differentiation.

Worked Example: A Pathwise Gradient for a Simulated Probability

Suppose a hypothetical binary-outcome model assigns probability \(p(\theta)=\mathbb{E}[\Phi(\theta+U)]\) to the observed outcome, where \(U\) is a simulation variable and \(\Phi\) is the standard normal cumulative distribution function. Use the three fixed draws \(-1,0,1\) to calculate the simulated probability and its gradient at \(\theta=0\).

The simulated probability is the average of the three draw-specific probabilities:

$$ \widehat p_{3}(0) = \frac{\Phi(-1)+\Phi(0)+\Phi(1)}{3} \approx \frac{0.1587+0.5000+0.8413}{3} =0.5000. $$

Since the derivative of \(\Phi(x)\) is the standard normal density \(\phi(x)\), the pathwise derivative is

$$ \frac{\partial \widehat p_3(0)}{\partial\theta} = \frac{\phi(-1)+\phi(0)+\phi(1)}{3} \approx \frac{0.2420+0.3989+0.2420}{3} =0.2943. $$

For this one-observation log likelihood, the simulated log-likelihood gradient is the probability gradient divided by the probability:

$$ \frac{\partial\log\widehat p_3(0)}{\partial\theta} = \frac{0.2943}{0.5000} =0.5886. $$

Thus, at \(\theta=0\), the simulated outcome probability is \(0.5000\), and its simulated log-likelihood gradient is positive. The calculation uses the same three draws for the probability and its derivative.

From Probability Gradients to Log-Likelihood Gradients

If the simulated likelihood contribution is positive, the chain rule yields

$$ \nabla_\theta\widehat\ell_R(\theta) = \sum_{i=1}^{n} \frac{\nabla_\theta\widehat p_{i,R}(\theta)} {\widehat p_{i,R}(\theta)}. $$

This formula is useful in computation, but it also highlights a distinction important for interpretation. Even if the simulated probability and its pathwise derivative are unbiased estimates of their respective expectations, their ratio is generally not an unbiased estimate of \(\nabla_\theta\log p_i(\theta)\) at finite \(R\). The derivative of the simulated log likelihood is the gradient of the finite-draw objective; it need not equal the exact log-likelihood gradient.

Result: For positive simulated contributions, differentiate the simulated log likelihood by summing each simulated probability gradient divided by its corresponding simulated probability. The result is the exact gradient of the fixed-draw simulated objective, not necessarily an unbiased estimate of the exact likelihood score.

The gradient formula follows by applying the ordinary derivative rule for a logarithm to each contribution:

$$ \nabla_\theta\log\widehat p_{i,R}(\theta) = \frac{1}{\widehat p_{i,R}(\theta)} \nabla_\theta\widehat p_{i,R}(\theta). $$

Adding this identity over \(i\) gives the stated log-likelihood gradient. If a simulated contribution is zero, its logarithm and this formula are undefined. Positive contributions are therefore essential, not merely convenient.

Finite Differences as an Alternative

When an analytic or pathwise derivative is unavailable, finite differences approximate a derivative by evaluating the simulated objective at nearby parameter values. For a scalar parameter, the central difference is

$$ \frac{\partial \widehat p_R(\theta)}{\partial\theta} \approx \frac{\widehat p_R(\theta+\delta)-\widehat p_R(\theta-\delta)}{2\delta}, $$

where \(\delta>0\) is a small step size. For a sufficiently smooth function, the central difference has truncation error of order \(\delta^2\). A forward difference, using \(\{\widehat p_R(\theta+\delta)-\widehat p_R(\theta)\}/\delta\), typically has truncation error of order \(\delta\).

In a simulated objective, use common random numbers: hold the draws fixed in both evaluations in the numerator. Otherwise, differences between the two objective values include simulation noise unrelated to the parameter change. Dividing that noise by \(2\delta\) can make the derivative estimate extremely variable as \(\delta\) becomes small. A step that is too large, on the other hand, can create substantial approximation error. Finite differences therefore involve a trade-off between truncation error, numerical precision, and simulation noise.

Worked Example: Central Differences With Common Draws

Use the simulated probability from the previous example, with the same draws \(-1,0,1\), to approximate its derivative at \(\theta=0\) using a central difference with \(\delta=0.01\).

At \(\theta=0.01\), the three arguments of \(\Phi\) are \(-0.99,0.01,1.01\). At \(\theta=-0.01\), they are \(-1.01,-0.01,0.99\). Using standard normal cumulative probabilities rounded to four decimal places gives

$$ \widehat p_3(0.01) \approx \frac{0.1611+0.5040+0.8438}{3} =0.5030, \qquad \widehat p_3(-0.01) \approx \frac{0.1562+0.4960+0.8389}{3} =0.4970. $$

The central-difference estimate is therefore

$$ \frac{0.5030-0.4970}{2(0.01)} \approx 0.300. $$

The estimate is close to the pathwise derivative of \(0.2943\), though rounding the probabilities to four decimal places affects the result. Crucially, both evaluations use the same three draws. Replacing either set of draws with fresh random values would add simulation variation to the difference.

Vector Parameters and the Chain Rule

For a vector parameter, pathwise differentiation produces a gradient vector. Consider the hypothetical probability simulator

$$ \widehat p_3(\theta_1,\theta_2) = \frac{1}{3}\sum_{u\in\{-1,0,1\}} \Phi(\theta_1+\theta_2u). $$

At \((\theta_1,\theta_2)=(0,0)\), the derivative with respect to \(\theta_1\) is the average of \(\phi(u)\), while the derivative with respect to \(\theta_2\) is the average of \(u\phi(u)\). Thus

Worked Example: A Two-Parameter Pathwise Gradient

For the simulator above, use the same three draws and calculate both gradient components at \((0,0)\).

$$ \frac{\partial\widehat p_3}{\partial\theta_1} = \frac{\phi(-1)+\phi(0)+\phi(1)}{3} \approx \frac{0.2420+0.3989+0.2420}{3} =0.2943. $$
$$ \frac{\partial\widehat p_3}{\partial\theta_2} = \frac{(-1)\phi(-1)+0\phi(0)+1\phi(1)}{3} \approx \frac{-0.2420+0+0.2420}{3} =0. $$

The gradient is approximately \((0.2943,0)\). The second component is zero because the contributions from draws \(-1\) and \(1\) cancel at this parameter value. This is a local derivative result, not evidence that the probability is independent of \(\theta_2\) everywhere.

When the Simulator Is Not Smooth

A common problem arises when a simulated likelihood contribution is built from an indicator. Consider a hypothetical model where an outcome equals one when a standard normal latent variable \(\varepsilon\) is at most \(\theta\). The event probability is \(P(\varepsilon\leq\theta)=\Phi(\theta)\), which is smooth. But a direct simulation might estimate it with the indicator \(1\{\varepsilon_r\leq\theta\}\). Holding a finite set of simulated values fixed makes the resulting probability a step function of \(\theta\): it is constant between simulated values and jumps when \(\theta\) crosses one.

Worked Example: A Fixed-Draw Indicator Has a Poor Gradient

Suppose three hypothetical standard normal simulation draws are \(-1,0,1\), and the parameter is \(\theta=0.2\). The direct indicator estimate is

$$ \widehat p_3(0.2) = \frac{1\{-1\leq0.2\}+1\{0\leq0.2\}+1\{1\leq0.2\}}{3} = \frac{2}{3}. $$

For every \(\theta\) between 0 and 1, these same indicators remain \(1,1,0\). The fixed-draw estimate is constant on that interval, so its derivative at \(0.2\) is zero. By contrast, the exact event probability has derivative \(\phi(0.2)\approx0.3910\). The zero simulated derivative does not describe the slope of the exact probability; it reflects the stepwise form of this finite-draw simulator.

When possible, one remedy is to replace the discontinuous indicator by a conditional probability, integrating out the threshold-crossing latent variable analytically. In this example that conditional probability is \(\Phi(\theta)\), whose derivative is \(\phi(\theta)\). More generally, conditional smoothing can turn a discontinuous draw-level contribution into a smooth expected contribution. Whether this is available depends on the model and the structure of the integral.

Practical Checks and Common Pitfalls

Before relying on a gradient, check that the simulated contributions remain positive, that the same draws are used for nearby evaluations, and that the derivative corresponds to the objective actually being optimized. In particular, an optimizer using fixed draws follows the gradient of a fixed-draw objective. If draws are changed at each evaluation, the reported objective and finite differences can fluctuate for reasons unrelated to changes in \(\theta\).

  • Using independent draws in a finite difference. The difference then mixes the parameter effect with simulation noise. Hold the draws fixed in both evaluations.
  • Assuming every finite-draw probability is smooth. Indicator-based contributions can make the simulated objective discontinuous or locally flat, even when the exact probability is smooth.
  • Making the step size arbitrarily small. Smaller \(\delta\) reduces truncation error but can amplify rounding error and simulation noise.
  • Calling the simulated log-likelihood gradient unbiased. It is the gradient of the finite-draw objective; the ratio of simulated derivatives to simulated probabilities is generally not unbiased for the exact score.
  • Confusing smoothness with inferential validity. A usable gradient helps computation, but does not by itself establish the accuracy or validity of subsequent statistical inference.

The main practical principle is to make the simulated objective vary regularly with the parameters: use differentiable probability contributions or pathwise transformations where justified, keep simulation draws fixed for local comparisons, and check finite-difference results against available analytic derivatives. These choices improve the information supplied to an optimizer without changing what the underlying statistical model asserts.

Check Your Understanding

Work through these questions to check your understanding of simulated likelihood gradients.

  1. Write the pathwise gradient of a simulated likelihood contribution based on \(R\) fixed draws.
  2. Why can the gradient of a simulated log likelihood be different from an unbiased estimate of the exact likelihood score?
  3. In a central finite difference, what problem is reduced by using common random numbers?
  4. Why can a fixed-draw indicator simulator have derivative zero over an interval even when the exact event probability has a nonzero derivative?
  5. Name one condition that can justify exchanging differentiation and expectation.
Clinical Biostats