Hamiltonian Monte Carlo for Probabilistic Programs with Discontinuities

Bradley Gram-Hansen, Yuan Zhou, Tobias Kohn, Tom Rainforth, Hongseok Yang, Frank WoodarXiv 2018

TL;DR

This paper introduces a new framework for performing Hamiltonian Monte Carlo (HMC) inference on probabilistic programs that contain discontinuities, such as those induced by if-else statements. By designing a restricted "Simple Probabilistic Programming Language" (SPPL) and a specialized compiler, the authors automatically extract the boundary information needed to use advanced HMC variants like Discontinuous HMC (DHMC), making inference in complex, non-differentiable models more robust and automated.

What Problem Does This Paper Address?

Hamiltonian Monte Carlo (HMC) is the "gold standard" for inference in modern Probabilistic Programming Languages (PPLs) like Stan and PyMC3. It works by treating the target probability density as a physical landscape and simulating a particle sliding across it. However, HMC has a major Achilles' heel: it relies on derivatives (gradients).

When a program contains discrete choices or if-else logic, the resulting probability density function can have "jumps" or discontinuities. Standard HMC often fails or becomes extremely inefficient in these regions because the gradient is either undefined or doesn't capture the global structure of the jump. While specialized algorithms like Discontinuous HMC (DHMC) exist to handle these jumps, existing PPL compilers aren't designed to "unearth" the necessary boundary information from the source code to support them.

Methods

The authors propose a three-part solution:

  1. SPPL (Simple PPL): A Lisp-style language (similar to Anglican or Church) that restricts primitives to analytic functions (like exp, +, log). This restriction is a "mathematical safety net" that ensures the resulting probability densities are "piecewise smooth" and that discontinuities only occur on sets of measure zero.
  2. A New Compilation Scheme: Instead of just calculating gradients, this compiler automatically identifies if predicates. It assigns each predicate a unique name and tracks its boolean state. At runtime, the system can detect exactly when a random variable "crosses a boundary" (i.e., when an if condition changes from true to false).
  3. DHMC Integration: The compiler passes this boundary-crossing information to a Discontinuous HMC engine. DHMC treats these boundaries like surfaces where the "particle" can reflect or refract, allowing the sampler to transition smoothly across discontinuities that would stall a standard HMC sampler.

Datasets

The authors evaluated their approach using a synthetic Gaussian Mixture Model (GMM).

  • Task: Estimate the means of two clusters and the cluster assignments for a set of data points.
  • Complexity: This model is a classic example of a "mixed" distribution containing both continuous variables (the cluster means) and discrete variables (the assignments), the latter of which are modeled using if-else logic in SPPL.
  • Comparison: They compared their unoptimized DHMC implementation against the highly optimized NUTS (No-U-Turn Sampler) with Metropolis-within-Gibbs (MwG) provided by PyMC3.

Key Insights and Ideas

  • Syntax as Geometry: The authors treat if-else statements not just as control flow, but as geometric boundaries in the latent variable space. By tracking these boundaries, they turn a logical problem into a geometric one that HMC can handle.
  • Analytic Primitives are Key: By restricting the language to analytic functions, the authors provide a formal proof (Theorem 1) that the set of points where the density is non-differentiable is negligible (measure zero). This guarantees that the physics-based assumptions of HMC variants remain valid.
  • Automation is Essential: Manually deriving the boundary conditions for complex models is error-prone and difficult. The primary contribution here is the automated extraction of this information via the compiler, lowering the barrier for users to model discontinuous systems.

Key Results

The researchers found that their unoptimized DHMC implementation, powered by the SPPL compiler, performed comparably to the industry-standard, highly optimized NUTS-MwG implementation in PyMC3.

As shown in the Mean Squared Error (MSE) plot below, both methods converge at similar rates as the sample size increases. This demonstrates that the automated boundary detection successfully enables HMC-style inference on models that would typically require manual "workarounds" or less efficient sampling schemes.

Figure 2. Mean Squared Error for the posterior estimates to true posterior of the cluster means µ1:2. We compare the results from DHMC and NUTS with Metropolis-within- Gibbs(MwG). The median (dashed lines) and 20%/80% confi- dence intervals(shaded) over 20 independent runs are shown.

Limitations and Future Work

  • Boundary Detection vs. Solving: The current runtime checker can only detect when a boundary has been crossed during a simulation step. It cannot yet analytically solve for the exact point of intersection on the boundary. The authors note that while detection is sufficient for many engines, analytical solving is a goal for future work.
  • Language Restrictions: SPPL is currently a "Simple" PPL with no recursion and a limited set of primitives. Expanding the language's expressivity while maintaining the mathematical guarantees of Theorem 1 remains a challenge.
  • Optimization: The DHMC implementation used was unoptimized; the authors suggest that a tuned version could potentially outperform standard hybrid methods.

Summary updated

Abstract

Hamiltonian Monte Carlo (HMC) is arguably the dominant statistical inference algorithm used in most popular "first-order differentiable" Probabilistic Programming Languages (PPLs). However, the fact that HMC uses derivative information causes complications when the target distribution is non-differentiable with respect to one or more of the latent variables. In this paper, we show how to use extensions to HMC to perform inference in probabilistic programs that contain discontinuities. To do this, we design a Simple first-order Probabilistic Programming Language (SPPL) that contains a sufficient set of language restrictions together with a compilation scheme. This enables us to preserve both the statistical and syntactic interpretation of if-else statements in the probabilistic program, within the scope of first-order PPLs. We also provide a corresponding mathematical formalism that ensures any joint density denoted in such a language has a suitably low measure of discontinuities.
BibTeX
@misc{gramhansen2018hamiltonian,
  author = {Bradley Gram-Hansen and Yuan Zhou and Tobias Kohn and Tom Rainforth and Hongseok Yang and Frank Wood},
  title = {{Hamiltonian Monte Carlo for Probabilistic Programs with Discontinuities}},
  year = {2018},
  eprint = {1804.03523},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1804.03523},
}

Loading related content...

Figure