Using Autodiff to Estimate Posterior Moments, Marginals and Samples

Sam Bowyer, Thomas Heap, Laurence Aitchison · DOI

TL;DR

This paper introduces a way to perform high-quality Bayesian inference in complex models without the headache of manual coding. By combining "massively parallel" importance sampling with a clever "source term trick" from physics, the authors show that you can compute posterior expectations, marginals, and samples simply by using automatic differentiation (autodiff) on a slightly modified marginal likelihood estimator.

What Problem Does This Paper Address?

Bayesian inference—the process of updating our beliefs about latent (hidden) variables based on observed data—is notoriously difficult to scale. A common tool is Importance Sampling (IS), which uses samples from a "proposal" distribution to approximate the true posterior. However, standard IS fails in high-dimensional models because the number of samples needed grows exponentially with the number of latent variables.

To fix this, researchers recently developed Massively Parallel Importance Sampling (MPIS). Instead of drawing one joint sample for all variables, MPIS draws $K$ samples for each of the $n$ variables, effectively reasoning over all $K^n$ possible combinations. While this is theoretically powerful and tractable for graphical models, it is a nightmare to implement. In practice, it requires writing complex "backward traversals" through the model to compute specific quantities like posterior means or marginals. These backward passes are different for every task and incredibly difficult to optimize for GPUs.

Methods

The authors bypass the need for hand-written backward passes by exploiting the source term trick, a technique borrowed from Quantum Field Theory.

  1. The Modified Estimator: They modify the standard marginal likelihood estimator (the "forward pass") by adding a small "source term" (an exponential factor $e^{Jm(z)}$) involving a dummy variable $J$ and the function $m(z)$ they want to compute the expectation of.
  2. The Autodiff Magic: They prove that the gradient of the log of this modified estimator with respect to $J$ (evaluated at $J=0$) is exactly equal to the posterior expectation.
  3. Generalization: They extend this logic to compute marginal importance weights (by using a vector-valued $J$) and posterior samples (by using a tensor-valued $J$ to compute conditional distributions).

Because modern libraries like PyTorch and JAX are built for autodiff, the complex "backward traversal" is handled automatically. The inference engine inherits all the optimizations (like opt-einsum for tensor products) used in the forward pass.

Datasets

The method was evaluated on four diverse hierarchical probabilistic models:

  • NYC Bus Breakdown: Predicting school bus delays based on year, borough, and company.
  • Chimpanzee Prosociality: A behavioral dataset modeling whether chimpanzees choose to share food.
  • MovieLens 100K: A classic recommendation system dataset (binarized ratings).
  • North American Breeding Bird Survey (Occupancy): A complex multi-species occupancy model tracking bird sightings across thousands of routes.

Key Insights and Ideas

  • Autodiff as a Compiler for Inference: The "aha moment" is realizing that we don't need to derive specific algorithms for marginals or moments. If we can write the forward pass for the marginal likelihood, autodiff acts as a compiler that automatically generates the correct, optimized inference algorithm for us.
  • Physics Meets Machine Learning: The source term trick provides a mathematically elegant bridge between a global property (the marginal likelihood) and local properties (posterior moments).
  • One-Shot Effectiveness: Unlike iterative methods like Variational Inference (VI) which require many steps to "warm up," MPIS provides high-quality results in a single "one-shot" pass, making it much faster for a given accuracy budget in many scenarios.

Key Results

  • Superior Accuracy: In all four datasets, MPIS achieved significantly higher Evidence Lower Bounds (ELBO) and predictive log-likelihoods than standard "global" importance sampling for the same number of samples $K$.
  • Speed-Accuracy Tradeoff: While MPIS is slower per-iteration than global IS, it reaches much better performance in a fraction of the time required by iterative methods like VI, IWAE, or Reweighted Wake-Sleep (RWS). For example, MPIS often reached a better ELBO in seconds than VI could reach in minutes.
  • Implementation Simplicity: The authors demonstrated that complex quantities that previously required specialized, hundreds-of-lines-of-code algorithms can now be computed in just a few lines of standard autodiff code.

Limitations and Future Work

  • Memory Overhead: The primary limitation is memory. Reasoning over $K^n$ combinations, even efficiently, involves large intermediate tensors. This limits the number of samples $K$ that can be used per variable on current hardware.
  • Proposal Quality: As a "one-shot" method, it still depends on the initial proposal distribution. If the proposal is extremely far from the truth, even $K^n$ combinations might not be enough.
  • Future Directions: The authors suggest integrating this approach into an iterative framework (like a massively parallel version of RWS) and incorporating it into general-purpose probabilistic programming languages to make high-performance Bayesian inference accessible to non-experts.

Summary updated

BibTeX
@inproceedings{bowyer2024using,
  author = {Sam Bowyer and Thomas Heap and Laurence Aitchison},
  title = {{Using Autodiff to Estimate Posterior Moments, Marginals and Samples}},
  booktitle = {Conference on Uncertainty in Artificial Intelligence},
  year = {2024},
  url = {https://openreview.net/forum?id=QUMZJgrjN0},
}
Figure