R2: An Efficient MCMC Sampler for Probabilistic Programs

Aditya V. Nori, Chung-Kil Hur, Sriram K. Rajamani, Selva Samuel

Abstract

Probabilistic programming languages (PPLs) allow for the expression of complex statistical models using standard programming constructs augmented with random sampling and conditioning. A primary bottleneck in PPLs is inference, typically performed using Markov Chain Monte Carlo (MCMC) sampling, which can be highly inefficient due to frequent sample rejections when conditioning on rare events. This paper introduces R2, a tool that combines formal program analysis with MCMC sampling to eliminate wasted computation. By propagating observation constraints backward through the program, R2 ensures that every execution produces a valid sample. Experimental results demonstrate that R2 achieves significant speedups, often exceeding 50x, over existing tools like Church and Stan.

Introduction

Probabilistic programming empowers developers to model uncertainty by incorporating random distributions and conditioning statements (observe) into traditional code. For instance, a model of a burglar alarm system can be conditioned on the observation that a neighbor called to infer the probability of a burglary.

The standard approach to inference is MCMC sampling, which involves running the program repeatedly to approximate the posterior distribution. However, if the observed condition is unlikely, a naive sampler will reject the vast majority of runs, leading to slow convergence and excessive CPU usage. R2 addresses this inefficiency by transforming the program such that it only explores states that satisfy the observations.

Methods: Rejection-Free Sampling via Program Analysis

The R2 framework employs a two-stage process to optimize inference:

1. Pre-image Propagation (PRE Analysis)

R2 utilizes techniques from formal verification, specifically Dijkstra’s weakest precondition analysis, to perform a backward pass over the program. This analysis identifies the necessary conditions at each line of code to satisfy subsequent observe statements. The program is then transformed to include specific observe statements immediately following every probabilistic assignment.

Syntax of the probabilistic programming language supported by R2, including deterministic and probabilistic assignments.

2. Modified Metropolis-Hastings

Unlike standard Metropolis-Hastings (MH) which samples from a distribution and then checks validity, R2’s modified MH sampler uses the hoisted conditions to sample directly from truncated distributions. By construction, this ensures that every program execution satisfies all observations, effectively eliminating rejections.

Evaluation and Results

R2 was evaluated against CHURCH and STAN using a suite of seven benchmarks ranging from toy models (e.g., BurglarAlarm) to real-world applications (e.g., HIV health data, Chess and Halo skill rating systems).

Execution time comparison (log scale) between Church, R2, and Stan across various benchmarks. R2 consistently shows lower execution times.

As shown in the performance data, R2 consistently outperforms its competitors. Notably, on the Halo benchmark, R2 completed inference in seconds, whereas both CHURCH and STAN failed to finish within an hour. R2 was also found to be at least 50 times faster than QI, a previous program-analysis-based sampler, due to its more flexible path exploration. While STAN performed better on the HIV model—likely due to its use of Hamiltonian Monte Carlo (HMC) for high-dimensional continuous spaces—R2 excelled in models involving discrete latent variables where STAN often struggles.

Conclusion

R2 demonstrates the power of marrying formal methods with statistical inference. By leveraging weakest precondition analysis to hoist observations, R2 transforms probabilistic programs into a form that is inherently efficient for MCMC sampling. Future work includes automating the generation of loop invariants for complex programs and integrating R2’s PRE transformation into other PPL frameworks to provide broader rejection-free benefits.

Summary updated

BibTeX
@inproceedings{nori2014r,
  author = {Aditya V. Nori and Chung-Kil Hur and Sriram K. Rajamani and Selva Samuel},
  title = {{R2: An Efficient MCMC Sampler for Probabilistic Programs}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2014},
  doi = {10.1609/aaai.v28i1.9060},
}
Figure