Automatic Differentiation of Programs with Discrete Randomness

Gaurav Arya, Moritz Schauer, Frank Schäfer, Chris Rackauckas · DOI

TL;DR

This paper introduces a novel method for automatically differentiating programs that involve discrete random processes, a capability previously limited in standard automatic differentiation (AD) systems. Their approach, "stochastic derivatives," allows for unbiased and low-variance gradient estimation in complex simulations like agent-based models and particle filters, opening doors for more robust optimization in fields relying on discrete stochasticity.

What Problem Does This Paper Address?

Gradient-based optimization, powered by automatic differentiation (AD), is a cornerstone of modern machine learning and scientific computing. However, traditional AD systems struggle with programs that contain discrete random elements. Imagine trying to differentiate a coin flip where the probability of heads is a parameter $p$. The outcome (heads or tails) is fundamentally discrete, making the derivative undefined in the usual sense. This limitation prevents gradient-based optimization from being applied to a vast array of important models, such as discrete-time Markov chains, agent-based simulations, and certain types of statistical models, where parameters influence discrete outcomes. The authors aim to bridge this gap by developing an AD framework that can handle these discrete stochastic behaviors.

Methods

The core contribution is the development of "stochastic derivatives," a generalization of the pathwise gradient estimator used for continuous random variables. Instead of relying on continuous relaxations or approximations, their method directly captures the effect of infinitesimal changes in parameters on discrete outcomes. This is achieved by:

  • Stochastic Triples: A generalization of dual numbers, which carry not only the primal value and its infinitesimal perturbation but also information about finite perturbations that occur with infinitesimal probability.
  • Reparameterization Trick for Discrete Variables: Adapting the reparameterization trick, commonly used for continuous distributions, to discrete ones by carefully considering how parameter changes affect the probability of discrete events.
  • Coupling Randomness: Ensuring that the primal computation and the derivative computation share the same random number sequence to reduce variance, a key insight from continuous AD extended to the discrete case.
  • Pruning and Smoothing: Developing strategies to manage the complexity of propagating stochastic derivatives, including "pruning" to follow a single path and "smoothing" to enable reverse-mode AD.

Datasets

Not discussed in the paper. The paper focuses on developing a methodology and demonstrating its application on simulated examples and well-known models rather than on specific datasets.

Key Insights and Ideas

  • Discrete Randomness is Differentiable (with the right tools): The paper shows that even fundamentally discrete processes can be differentiated in a meaningful way by considering how infinitesimal parameter changes affect the probability of discrete outcomes.
  • Stochastic Derivatives Generalize Pathwise Gradients: The proposed "stochastic derivative" is a principled extension of the pathwise gradient estimator to discrete random variables, preserving its low-variance properties.
  • Composability is Key: The method is designed to be composable, meaning it can handle complex programs built from many elementary discrete random components, mirroring the composability of traditional AD.
  • Unbiasedness Without Relaxation: Unlike some prior methods that rely on continuous approximations, this approach provides provably unbiased gradient estimates by preserving the discrete structure.
  • Stochastic Triples as a Computational Primitive: The introduction of stochastic triples provides a concrete computational object that can be propagated through programs, enabling automated differentiation.

Key Results

  • Unbiased Forward-Mode AD: Demonstrated for discrete-time Markov chains and agent-based models like Conway's Game of Life, showing that their method can automatically compute unbiased gradients.
  • Unbiased Reverse-Mode AD: Applied to a particle filter, recovering a previously known technique and showing its unbiased nature.
  • Low Variance: The method achieves lower variance compared to score-function estimators, especially in scenarios like inhomogeneous random walks, by effectively coupling randomness.
  • Efficiency: The approach aims for computational overhead comparable to deterministic AD, particularly through techniques like pruning.
  • Code Availability: The authors provide an open-source Julia package, StochasticAD.jl, to facilitate the use and exploration of their method.

Limitations and Future Work

  • Handling of Discrete Control Flow: The authors acknowledge that handling discrete constructs like if statements with discrete random inputs is an area for improvement, suggesting workarounds like array indexing.
  • Automatic Handling of Mixed Discrete/Continuous Transitions: Developing methods to automatically handle functions that deterministically convert continuous random variables into discrete ones (e.g., inequalities) is a future direction.
  • Variance Reduction: While the method offers low variance, there's still room for improvement, particularly in complex scenarios like the Game of Life or particle filter resampling, where the coupling of primal and derivative computations can be challenging.
  • Pruning vs. Smoothing Trade-offs: The paper notes that pruning can introduce variance in certain cases, while smoothing can introduce bias for non-linear functions. Future work could explore better ways to balance these.
  • Scalability: While aiming for efficiency, further work is needed to scale the method to very large-scale machine learning applications.

Summary updated

BibTeX
@inproceedings{arya2022automatic,
  author = {Gaurav Arya and Moritz Schauer and Frank Schäfer and Chris Rackauckas},
  title = {{Automatic Differentiation of Programs with Discrete Randomness}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2022},
  url = {https://openreview.net/forum?id=553UpaAPDb},
}
Figure