[Paper Review] Automatic Differentiation of Programs with Discrete Randomness
This paper introduces a reparameterization-based automatic differentiation method for programs with discrete randomness, enabling unbiased, low-variance gradient estimation by propagating infinitesimal perturbations through stochastic paths. The approach supports both forward- and reverse-mode AD in stochastic simulations like Markov chains, agent-based models, and particle filters, with an open-source Julia implementation achieving O(1) computational overhead via pruning and smoothing techniques.
Automatic differentiation (AD), a technique for constructing new programs which compute the derivative of an original program, has become ubiquitous throughout scientific computing and deep learning due to the improved performance afforded by gradient-based optimization. However, AD systems have been restricted to the subset of programs that have a continuous dependence on parameters. Programs that have discrete stochastic behaviors governed by distribution parameters, such as flipping a coin with probability $p$ of being heads, pose a challenge to these systems because the connection between the result (heads vs tails) and the parameters ($p$) is fundamentally discrete. In this paper we develop a new reparameterization-based methodology that allows for generating programs whose expectation is the derivative of the expectation of the original program. We showcase how this method gives an unbiased and low-variance estimator which is as automated as traditional AD mechanisms. We demonstrate unbiased forward-mode AD of discrete-time Markov chains, agent-based models such as Conway's Game of Life, and unbiased reverse-mode AD of a particle filter. Our code package is available at https://github.com/gaurav-arya/StochasticAD.jl.
Motivation & Objective
- To address the challenge of differentiating programs with discrete stochastic behaviors—such as Bernoulli trials—where classical automatic differentiation fails due to non-differentiable, discontinuous outcomes.
- To develop a method that preserves the discrete structure of stochastic programs while enabling gradient computation for statistical expectations.
- To achieve unbiased and low-variance gradient estimators comparable to continuous-pathwise AD, without relying on continuous relaxations or tunable parameters.
- To support both forward- and reverse-mode AD in complex stochastic systems, including Markov chains and particle filters, with minimal computational overhead.
- To provide a composable, end-to-end differentiable framework that integrates seamlessly with existing AD tools and enables gradient-based optimization in probabilistic modeling.
Proposed method
- Introduces the concept of a 'stochastic derivative'—a pathwise estimator that tracks the proportional change in outcome probabilities due to infinitesimal parameter perturbations.
- Uses reparameterization to express discrete random variables as deterministic functions of uniform random variables and parameters, enabling gradient propagation through the stochastic path.
- Applies a 'smoothed stochastic derivative' technique to handle discrete events, allowing unbiased gradient estimation via correlated sampling paths.
- Employs an online pruning strategy to maintain O(1) computational overhead in forward-mode AD by dynamically eliminating inactive or low-impact paths.
- Combines smoothing and pathwise estimation to derive a reverse-mode AD mechanism, recovering the straight-through gradient estimator as a special case.
- Implements the method in StochasticAD.jl, a Julia package that integrates with ForwardDiff.jl, Zygote.jl, and ChainRulesCore.jl for seamless forward- and reverse-mode differentiation.
Experimental results
Research questions
- RQ1Can automatic differentiation be extended to programs with discrete stochastic components, such as Bernoulli or Poisson draws, where classical derivatives are undefined?
- RQ2How can one construct an unbiased and low-variance gradient estimator for the expectation of a stochastic program with discrete randomness?
- RQ3Can forward-mode AD be efficiently applied to complex stochastic simulations like agent-based models or Markov chains without incurring high computational cost?
- RQ4Is it possible to derive a reverse-mode AD mechanism for discrete stochastic programs that matches the performance of continuous-pathwise methods?
- RQ5How can correlated sampling paths be systematically generated to minimize variance in gradient estimators for discrete stochastic programs?
Key findings
- The proposed method achieves unbiased gradient estimation for the expectation of discrete stochastic programs, with variance that remains bounded as the perturbation size approaches zero.
- The framework supports forward-mode AD of discrete-time Markov chains and agent-based models like Conway’s Game of Life with O(1) computational overhead via pruning.
- The method enables end-to-end reverse-mode AD of a particle filter, recovering a known technique from prior work as a special case.
- The smoothed stochastic derivative technique allows for both forward- and reverse-mode differentiation, overcoming limitations of traditional score-function estimators.
- Empirical evaluation shows that the method outperforms finite differences in variance and is competitive with continuous relaxation methods, without requiring tunable or learned parameters.
- The open-source StochasticAD.jl package demonstrates the practicality of the approach across diverse applications, including stochastic simulations and probabilistic inference.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.