Abstract
Automatic differentiation (AD) is a cornerstone of modern gradient-based optimization, yet it fails when programs involve discrete stochastic choices, such as branching on a Bernoulli trial. This paper introduces "stochastic derivatives," a framework that enables unbiased, low-variance gradient estimation for discrete stochastic programs. By tracking infinitesimal probabilities of state "jumps" through a new data structure called the Stochastic Triple, the method allows for automated differentiation of complex simulations. We evaluate the approach on various benchmarks, demonstrating significant variance reduction over traditional score-function estimators and the ability to handle hard discrete logic in models like the Stochastic Game of Life.
1. Introduction
Automatic differentiation is the "magic" behind backpropagation in deep learning and gradient-based optimization in scientific computing, providing exact gradients for continuous functions. However, when a program $X(p)$ involves discrete randomness—for instance, returning 1 with probability $p$ and 0 otherwise—standard AD fails. The individual output is a step function whose derivative is zero almost everywhere and undefined at the jump, making it useless for optimizing the expected value $\mathbb{E}[X(p)]$.
Existing solutions typically fall into two categories: "score-function" estimators (e.g., REINFORCE), which are unbiased but suffer from high variance, and "continuous relaxations" (e.g., Gumbel-Softmax), which reduce variance at the cost of introducing bias. This work proposes a middle ground: a pathwise gradient approach that maintains the unbiased nature of discrete models while achieving the low variance typically associated with the reparameterization trick.
2. Methodology: Stochastic Triples
The core of the proposed method is the Stochastic Triple, a generalization of the dual numbers used in forward-mode AD. A stochastic triple for a variable consists of three components:
- Primal Value ($x$): The result of the original computation.
- Infinitesimal Component ($\delta$): The standard derivative of continuous program elements.
- Discrete Change Component ($\Delta s$): A potential "jump" to an alternative state occurring with an infinitesimal probability $w\epsilon$.
The core innovation lies in how these triples are propagated. When a program encounters a discrete distribution, the triple records how a small change in input parameters might trigger a state flip. To maintain $O(1)$ computational complexity relative to the original program, the authors introduce Pruning, which probabilistically selects a single alternative path to follow. For reverse-mode AD, Smoothing is employed to transform discrete jumps into continuous ones, facilitating efficient backpropagation.
3. Experimental Evaluation
The method was implemented in the StochasticAD.jl library and tested on several stochastic simulations, including inhomogeneous random walks, the Stochastic Game of Life, and differentiable particle filters.
3.1. Variance Reduction
A key result of this work is the dramatic reduction in variance compared to traditional methods. In the random walk benchmark, the stochastic triple estimator maintained significantly lower standard deviation as the number of steps $n$ increased, outperforming even score-function estimators augmented with control variates.
3.2. Handling Complex Logic
Unlike continuous relaxations, which often struggle with hard discrete logic, stochastic triples successfully differentiated through the Game of Life. This demonstrates the method's robustness to programs involving non-differentiable operations like array indexing based on random variables, which typically break standard AD systems.
4. Discussion and Future Work
Stochastic derivatives provide a powerful tool for automating the optimization of discrete stochastic programs. By leveraging tight coupling between primal and perturbed paths, the method achieves exact, unbiased gradients with low variance.
However, limitations remain. Handling complex branching logic (e.g., if statements) still requires some manual implementation effort. Additionally, while variance is reduced, it still scales with program complexity, and a truly general-purpose, unbiased reverse-mode AD for all discrete programs remains an open challenge for future research.
Summary updated