NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Expectation Programming

Tim Reichelt, Adam Golinski, Luke Ong, Tom RainfortharXiv 2021

TL;DR

Standard probabilistic programming systems are highly inefficient at estimating expectations because they approximate full probability distributions before considering the target function. This paper introduces expectation programming, a paradigm that directly targets the expected return values of programs by automatically restructuring them using target-aware inference. Implemented as an extension to the Turing language, this approach delivers massive gains in accuracy and computational efficiency.

What Problem Does This Paper Address?

In many scientific and engineering workflows, the ultimate goal of probabilistic modeling isn't to find the full posterior distribution, but rather to compute an expectation—such as calculating expected financial risk, predicting the average cost of an epidemic, or computing a decision-theoretic loss function.

Current Probabilistic Programming Systems (PPSs) handle this via a two-step, "target-blind" pipeline:

  1. Approximate the conditional distribution $\pi(x)$ (usually via MCMC or Importance Sampling).
  2. Compute the average of a target function $f(x)$ over those samples.

This is highly suboptimal. If the target function $f(x)$ has a high value in regions where the probability density $\pi(x)$ is very low, standard inference engines will completely miss those regions. This leads to extremely high-variance estimates or outright failures. While you might try to manually adjust your model to account for $f(x)$, doing so mathematically requires calculating complex corrective factors, which is incredibly difficult to do by hand.

Methods

The authors introduce Expectation Programming, where an expectation program is defined as a probabilistic program whose primary quantity of interest is the expected value of its return values.

To automate this efficiently, they develop Expectation Programming in Turing (EPT), which leverages the Target-Aware Bayesian Inference (TABI) framework. EPT works via automated source-code transformations:

  • Users define their model using a new @expectation macro instead of the standard @model macro.
  • Behind the scenes, EPT automatically splits the target expectation into three separate components: the positive part of the function $\gamma_1^+(x)$, the negative part $\gamma_1^-(x)$, and the original unnormalized density $\gamma_2(x)$.
  • EPT compiles these into three distinct, valid Turing programs and uses a backend inference engine to estimate the normalization constant (marginal likelihood) of each independently.

To make this work well, the authors also implemented a new Annealed Importance Sampling (AnIS) inference engine within Turing, which is highly effective at estimating normalization constants in high dimensions.

Datasets

The authors evaluated EPT on three distinct benchmarks:

  1. Gaussian Posterior Predictive: A synthetic 10-dimensional numerical problem that serves as a baseline because it has an exact analytical solution.
  2. SIR Epidemiology Model: A simulated disease outbreak scenario based on differential equations, where the goal is to estimate the expected economic/societal cost of an epidemic.
  3. Hierarchical Radon Model: A real-world dataset (radon.csv from Gelman & Hill, 2006) containing indoor radon measurements across US households, used to predict whether radon levels across multiple counties fall below an acceptable threshold.

Key Insights and Ideas

  • Target-Awareness Changes the Game: By exposing the target function $f(x)$ to the inference engine during sampling, the system can focus its computational budget on regions of the parameter space that actually matter to the final expectation.
  • Divide and Conquer via Normalization: Breaking an expectation down into separate normalization constant estimations (via TABI) fundamentally bounds the expected error. This avoids the severe variance issues that plague single Monte Carlo estimators.
  • Compiler-Level Automation: The beauty of EPT is its modularity. Users write clean, intuitive probabilistic code, and the compiler handles the complex math of splitting and re-weighting the models automatically, allowing any standard PPS marginal likelihood estimator to be plugged in as a backend.

Key Results

EPT consistently and substantially outperformed standard target-blind Annealed Importance Sampling (AnIS) and traditional MCMC (like HMC/NUTS):

  • Gaussian Experiment: EPT achieved a significantly lower Relative Squared Error (RSE) than both AnIS and MCMC for any given computational budget.
  • SIR Model: Traditional MCMC completely failed to sample the high-cost regions of the epidemic, underestimating the expected cost. EPT successfully isolated the critical regions, providing highly accurate estimates.
Figure 4: RSE and ESS for the SIR experiment. Conventions as in Figure 3; results computed over 5 runs.
  • Radon Model: EPT beat standard AnIS by several orders of magnitude in terms of Effective Sample Size (ESS). Quantitatively, EPT provided a stable estimate ($3.74\times 10^{-8} \pm 2.39\times 10^{-9}$), whereas MCMC failed completely, returning an estimate near zero ($7.79\times 10^{-18}$) because it never visited the target region.

Limitations and Future Work

The primary limitation of EPT is its dependency on the backend marginal likelihood estimator. If the underlying estimator (like AnIS) struggles to scale to extremely high-dimensional spaces, EPT's performance will degrade accordingly.

Additionally, EPT currently focuses exclusively on automating the TABI framework. The authors note that the overarching concept of expectation programming opens the door to automating other target-aware paradigms, such as the automatic synthesis of control variates directly from probabilistic program code.

Summary updated

BibTeX
@misc{reichelt2021expectation,
  author = {Tim Reichelt and Adam Golinski and Luke Ong and Tom Rainforth},
  title = {{Expectation Programming}},
  year = {2021},
  eprint = {2106.04953},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2106.04953},
}
Figure