Source-linked AI summary

An Introduction to Probabilistic Programming

Jan-Willem van de Meent, Brooks Paige, Hongseok Yang, Frank Wood

arXiv:1809.10756v2stat.MLcs.AIcs.LGcs.PL

TL;DR

Bayesian inference can be difficult because conditioning over latent variables generally involves intractable integrals. This book introduces probabilistic programming through its language and inference foundations, covering design through evaluator implementation for readers who want to use or build these systems.

  • Problem

    Bayesian inference requires conditioning over latent variables, but its denominator is generally intractable and may require exponential-time computation.

    Source: §1.1.2 · p. 23
  • Method

    The book presents probabilistic programming as Bayesian inference combining programming-language model denotation with statistical algorithms for computing conditional distributions.

    Source: §1.2 · p. 26
  • Results

    The book covers probabilistic programming basics from language design to evaluator implementation, with the stated aim of supporting both system users and future designers.

    Source: Introduction · p. 13
  • Takeaways & Limitations

    The book aims to help readers adopt existing probabilistic programming systems and give future language designers and implementers a foundation for building systems.

    Source: Introduction · p. 13

    Most of the book assumes a fixed program and focuses on inference in the model specified by that program.

    Source: §1.2 · p. 27

Abstract

from arXiv · show

This book is a graduate-level introduction to probabilistic programming. It not only provides a thorough background for anyone wishing to use a probabilistic programming system, but also introduces the techniques needed to design and build these systems. It is aimed at people who have an undergraduate-level understanding of either or, ideally, both probabilistic machine learning and programming languages. We start with a discussion of model-based reasoning and explain why conditioning is a foundational computation central to the fields of probabilistic machine learning and artificial intelligence. We then introduce a first-order probabilistic programming language (PPL) whose programs correspond to graphical models with a known, finite, set of random variables. In the context of this PPL we introduce fundamental inference algorithms and describe how they can be implemented. We then turn to higher-order probabilistic programming languages. Programs in such languages can define models with dynamic computation graphs, which may not instantiate the same set of random variables in each execution. Inference requires methods that generate samples by repeatedly evaluating the program. Foundational algorithms for this kind of language are discussed in the context of an interface between program executions and an inference controller. Finally we consider the intersection of probabilistic and differentiable programming. We begin with a discussion of automatic differentiation, and how it can be used to implement efficient inference methods based on Hamiltonian Monte Carlo. We then discuss gradient-based maximum likelihood estimation in programs that are parameterized using neural networks, how to amortize inference using by learning neural approximations to the program posterior, and how language features impact the design of deep probabilistic programming systems.

Notation

The notation defines grammars for probabilistic and deterministic languages, then formalizes collections, graph models, and probability densities used to describe programs and their variables.

Grammars

  • Grammars specify FOPPL expressions, deterministic target expressions, HOPPL expressions, and programs, using constants, variables, and procedures.FOPPL syntax includes let, if, calls, sample, and observe; HOPPL also includes function expressions, and programs may define procedures.Source: Grammars · p. 9

Sets, Lists, Maps, and Expressions

  • Notation distinguishes sets from lists, maps variables to constants with domain and update operations, and defines expression literals, substitution, and free variables.The map update and its effect on an entry are described across two passages.Source: Sets, Lists, Maps, and Expressions · pp. 9–10

Directed Graphical Models

  • A directed graphical model G = (V, A, P, Y) represents variable nodes, directed parent-child edges, per-variable probability expressions, and observed values, partitioning variables into observed and unobserved sets.The parent set pa(v) consists of variables with edges into v.Source: Directed Graphical Models · p. 10

Factor Graphs

  • Factor graphs use variable nodes, factor nodes, undirected variable-factor edges, and target-language expressions for factor potentials.Source: Factor Graphs · p. 11

Probability Densities

  • Density notation defines joint, prior, likelihood, and posterior distributions, represents traces as maps from instantiated variables to values, and expresses parameterized mass or density functions as language expressions.A trace's density is evaluated at its recorded values.Source: Probability Densities · p. 11

Introduction

This introduction presents probabilistic programming as a way to express generative models and conditioning in programs and automate inference, supporting model-based reasoning. It covers model denotation, conditioning and posterior estimation, static and dynamic model languages, and sampling-based inference illustrated through constrained simulation, program induction, and evaluator designs.

Introduction

  • Probabilistic programming aims to automate inference through a toolchain that supports supervised, unsupervised, and semi-supervised learning and lowers the cost of Bayesian model iteration.The book introduces both language design and evaluator implementation, for readers adopting existing systems and building future ones.Source: Introduction · pp. 12–13

1.1 Model-based Reasoning

  • Models can simulate outcomes to test and falsify theories or support decisions, and probabilistic programming addresses interpretable abstractions, black-box generators, and explicit simulators.In a game, simulating possible actions and an opponent’s responses can guide selection of a move that maximizes the chance of winning.Source: §1.1 · pp. 15–16

1.1.1 Model Denotation

  • Probabilistic programs can denote joint distributions over structured latent variables and observations, and general-purpose inference can condition a generative program on observed output.In the Captcha example, conditioning yields a posterior over possible text strings that reflects ambiguity in noisy images.Source: §1.1.1 · p. 20

1.1.2 Conditioning

  • Conditioning requires integrating over latent variables, an operation that is generally intractable and may require exponential time, so the book focuses on sampling-based approximations to conditional distributions.Conditioned distributions often lack analytic forms, and their normalizing integrals may not be readily calculable numerically.Source: §1.1.2 · p. 23

1.1.3 Query

  • Posterior samples can answer queries by approximating expectations of functions under the posterior with sample averages.The same generated samples can be reused to evaluate different query functions.Source: §1.1.3 · p. 24

1.2 Probabilistic Programming

  • Probabilistic programming combines model-denoting languages with inference algorithms to characterize the conditional distribution of program choices given observed outputs.Its defining constructs include random sampling and conditioning through observations.Source: §1.2 · pp. 25–27
  • The book primarily studies inference for fixed programs, while later connecting probabilistic programming to learning the generative program from data.The later discussion uses semi-supervised learning in the variational autoencoder family as its lens.Source: §1.2 · p. 27

1.2.1 Existing Languages

  • The book organizes its language discussion around static programs with finite random variables versus dynamic programs that cannot compile to finite-node graphical models.It uses an abstract, Anglican-like Lisp language to make the transition between these cases, while emphasizing language-agnostic concepts.Source: §1.2.1 · pp. 29–30

1.3 Example Applications

  • Probabilistic programming offers expressive, readable generative models for applications including code-heavy scientific simulations and inverse reasoning over their outputs.The authors point to detector observations in large-hadron-collider simulation as a potential route to high-fidelity event reconstruction.Source: §1.3 · p. 30, Recursive Multi-Agent Reasoning · p. 32, §1.4 · p. 33

Constrained Simulation

  • Conditioning a procedural tree generator on hard scene constraints yields posterior trees that satisfy those constraints while retaining the model’s prior regularities.Each tree corresponds to a program execution path, and its intermediate random choices must permit a valid constrained outcome.Source: Figure 1.3 · p. 30, Constrained Simulation · p. 31

Program Induction

  • Program induction can invert a generative model of code, conditioning on example inputs and outputs to find programs that match a specification (Perov and Wood, 2016).A recursive program can generate abstract syntax trees from a learned prior over programs.Source: Program Induction · p. 32

1.5 A First Probabilistic Program Evaluator

  • A beta-Bernoulli program illustrates how an evaluator must infer its returned value conditioned on an observation, rather than merely run the program forward.One approach tracks weights across repeated executions; the book begins with an evaluator that interprets programs as graphical structures for conventional inference instead.Source: §1.4 · p. 33, §1.5 · pp. 34–35

A Probabilistic Language Without Recursion

This section introduces FOPPL, a first-order language that excludes higher-order functions and recursion to produce finite-variable models with static computation graphs that can be compiled to graphical models. It explains how fixed-count loops express statistical and machine-learning models and defines a deterministic sublanguage for link functions, with examples including mixture and hidden Markov models, Bayesian neural networks, and a BUGS translation.

A Probabilistic Language Without Recursion

  • FOPPL restricts user-defined functions to first order and nonrecursive programs, yielding finite-variable models with static computation graphs that can be compiled to graphical models.Conditional branches may leave some nodes inactive, but all possible control-flow paths can be enumerated at compile time.Source: A Probabilistic Language Without… · pp. 36–37

2.1 Syntax

  • The small core syntax includes `sample` for unobserved variables and `observe` for conditioning, and can express any graphical model while enabling translation with eight rules.The language has no input/output functionality, so data must be inlined.Source: §2.1 · p. 39, §2.2 · p. 42

2.2 Syntactic Sugar

  • Syntactic sugar improves readability and ease of use by providing alternate forms that preprocessing expands into equivalent core expressions.The expansion is often implemented as a macro rewrite rule.Source: §2.2 · p. 42

2.2.1 Let forms

  • Generalized let forms bind multiple variables and sequence expressions, while `_` expands to a fresh variable for ignored results.This avoids nested let expressions and dummy bindings when observation calls are used for their conditioning side effect.Source: §2.2.1 · pp. 42–43

2.2.2 For loops

  • `foreach` maps sequence bindings over a non-negative constant number of steps and returns a vector of results; a sequence shorter than that count causes a runtime error.It can replace repeated observation calls in the linear-regression example.Source: §2.2.2 · p. 44
  • Requiring a constant loop count guarantees that iterations are known at compile time, avoiding programs with no upper bound on repeated random sampling.Allowing an arbitrary expression for the count could make the number of evaluations of a sampling expression unbounded.Source: §2.2.2 · p. 45

2.2.3 Loop forms

  • Unlike foreach, loop accumulates results across fixed-index iterations, supporting sufficient statistics or data-structure construction and defining dependent variables rather than conditionally independent ones.The regression example accumulates squared residuals by passing the running sum to each iteration and returning its final value.Source: §2.2.3 · pp. 45–47

2.3.1 Gaussian mixture model

  • The three-component Gaussian mixture model samples component parameters and weights, assigns each observation a latent class, and observes it under that class’s likelihood.The program returns the sequence of latent class assignments, enabling queries about similarities between data points.Source: §2.3.1 · pp. 47–48

2.3.2 Hidden Markov model

  • The HMM example uses a loop over 16 observations to sample each state from the transition distribution of the preceding state, condition on the observation, and accumulate states for posterior inference.The accumulated latent-state sequence is the program’s return value and the object whose joint posterior is of interest.Source: §2.3.2 · p. 48

2.3.3 A Bayesian Neural Network

  • The FOPPL can express fixed-computation-graph neural networks, framing parameter estimation as inference over a posterior given priors; activations can also be stochastic.The passage notes that fixed-length recurrent and convolutional networks, as well as multilayer perceptrons, can be represented.Source: §2.3.3 · p. 51

2.3.4 Translating BUGS models

  • FOPPL can compile BUGS models and defines essentially equivalent families of programs, while making observations explicit and specifying the posterior marginal through the program’s return value.Both languages require a fixed finite loop count; the text also emphasizes that FOPPL’s eight expression forms make programs comparatively easy to reason about and manipulate.Source: §2.3.4 · pp. 52–53

2.4 A Simple Purely Deterministic Language

  • The purely deterministic sublanguage defines graphical-model link functions that map parent values to child distributions without containing random variables, and excludes sampling, observation, and user-defined procedure calls.Inlining user-defined functions makes the language cumbersome to program manually but simple to target and evaluate; these expressions support later translations to graphical models.Source: §2.4 · pp. 54–55

Graph-Based Inference

FOPPL programs specify models over finitely many random variables, and compilation translates them into finite graphical models that support adapting graphical-model inference algorithms to probabilistic programs.

3.1 Compilation to a Graphical Model

  • FOPPL programs compile into finite graphical models, enabling graphical-model inference algorithms to be adapted for probabilistic programs.The section introduces this translation as the central way to exploit the finite-variable structure of FOPPL models.Source: §3.1 · p. 56
  • The translation maps an expression e to a graphical model G and a deterministic expression E that describes its return value using G's random variables.The translation is specified by a ternary relation, with a procedure-definition mapping and a logical predicate for the flow-control context.Source: §3.1 · p. 56
  • Graph vertices represent random variables and arcs represent dependencies; observed variables also carry values and predicates indicating whether their observe expressions are on the control-flow path.The path predicate is conditioned on the values of latent variables.Source: §3.1 · p. 57

Definition of a Graphical Model

A graphical model G = (V, A, P, Y) represents random variables and their conditional dependencies, assigns probability expressions to variables, and records observed values. A simple FOPPL program illustrates how this representation supports reasoning about a posterior.

Definition of a Graphical Model

  • A graphical model G = (V, A, P, Y) comprises random-variable vertices, directed dependency arcs, per-variable density or mass expressions, and a partial map of observed values.The observed-value map associates each observed variable with a deterministic expression for its value.Source: Definition of a Graphical Model · p. 57
  • The example FOPPL program defines a two-component Gaussian mixture with one observation, yielding joint distribution p(y = 0.5, z) and posterior target p(z | y).Its graphical model has variables z and y, with a directed dependency from z to y; the observed-value map records y.Source: Definition of a Graphical Model · pp. 57–58

Assigning Symbols to Variable Nodes

Each evaluated sample or observe expression creates a graph node, but program variables do not always map clearly to those nodes, especially when procedures are called repeatedly. The compilation rules assign unique symbols to graph vertices as addresses needed by generic inference algorithms.

Assigning Symbols to Variable Nodes

  • Each evaluated sample and observe expression gets a graph node, but repeated procedure calls can make the mapping from program variables to random variables unclear.In the example, each sample expression in norm-gamma's body is evaluated twice.Source: Assigning Symbols to Variable Nodes · p. 59
  • Compilation assigns every graphical-model vertex a newly generated unique symbol, while tutorial examples name sample and observe returns for readability.Source: Assigning Symbols to Variable Nodes · p. 59
  • Unique vertex labels provide addresses for random variables that are important to the correctness and implementation of generic inference algorithms.A later mechanism addresses cases where control-flow paths cannot all be explored at compile time.Source: Assigning Symbols to Variable Nodes · pp. 59–60

if-expressions in Graphical Models

Translating if-expressions into graphical models requires choosing how branches are evaluated, because lazy and eager evaluation treat branch-local samples and observations differently. The section resolves this tension by assigning off-path samples and observations different roles in the model.

if-expressions in Graphical Models

  • Moving an if-expression outside a sample can add a graphical-model node while preserving the distribution over program return values.The example’s network grows from three to four nodes, while its return-value distribution remains the same.Source: if-expressions in Graphical Models · p. 61
  • Lazy branch-local samples complicate joint support: branches may have incompatible supports, while representing absent variables as nil can require complex unions.For example, a normal-valued branch and a Gamma-valued branch do not share the same support for a single variable.Source: if-expressions in Graphical Models · pp. 62, 64
  • Assigning probability 1 to samples in unevaluated branches effectively gives them improper priors, so marginalizing over the assumed real-valued support can fail to converge.The text identifies the incorrect support assumption as the reason the marginal-likelihood integral does not converge.Source: if-expressions in Graphical Models · p. 63
  • Eager evaluation incorporates samples from both branches into the joint, yielding a standard mixture, but evaluates both branch observations, unlike the more natural lazy interpretation.Lazy evaluation includes only the selected branch’s observation; eager evaluation observes both variables.Source: if-expressions in Graphical Models · pp. 64–65
  • The proposed treatment assigns the same probability to sampled variables regardless of branch, but assigns probability 1 to observed variables off the control-flow path.Off-path samples can be auxiliary variables without changing the return-value posterior; off-path observations can change it if eagerly evaluated.Source: if-expressions in Graphical Models · p. 65
  • Although forbidding observe expressions inside conditionals is an alternative, the FOPPL permits them because random observation counts can matter in reinforcement-learning models.This choice adds implementation complexity; refactoring can remove conditionals when branches contain the same number of observations.Source: if-expressions in Graphical Models · p. 66

Support-Related Subtleties

FOPPL programs can have potentially infinite discrete support without inducing unbounded random variables, while control-flow paths may vary a variable’s support and distribution type. These features require care in inference algorithms.

Support-Related Subtleties

  • Program 3.7 is allowed despite potentially infinite discrete support: sampled values cannot bound loops, so they cannot induce unbounded random variables.The example samples z from a Poisson distribution and then draws d from a discrete range determined by z.Source: Support-Related Subtleties · pp. 66–67
  • Program 3.8 shows that a random variable’s support and distribution type can differ between control-flow paths.Its branches choose between a normal and a gamma distribution based on a flip.Source: Support-Related Subtleties · p. 67
  • Large supports can make inference runtimes vary substantially with values, and implementations must robustly score values outside a distribution’s support.Source: Support-Related Subtleties · p. 67

Translation rules

The section develops translation rules for compiling FOPPL programs into graphical and factor-graph representations, including how conditioning and partial evaluation shape those representations. It also describes inference methods over the resulting models, their implementation choices, and constraints on the programs they can handle.

Translation rules

  • FOPPL expressions are translated recursively by type, with constants and variables mapping to themselves and the empty graph.The grammar has eight expression types, and rules for compound expressions assume their subexpressions have already been translated.Source: Translation rules · p. 68
  • Sample translation adds a fresh random-variable node scored by the argument distribution, and fails if that argument does not denote a distribution.The node’s parents are the random variables used by the translated distribution expression.Source: Translation rules · p. 70
  • Observe translation adds a scored node gated by the current path predicate, and requires its observed value to be fully deterministic.The observed value cannot reference other random variables; the predicate makes its score 1 when its branch is not followed.Source: Translation rules · p. 71

3.2 Evaluating the Density

  • The compiled Bayesian network represents a joint density whose local factors depend on each node and its parents, while observed nodes have no children.Observed values have no free variables, so the joint can be separated into observed and unobserved variables.Source: §3.2 · pp. 72–73

3.2.1 Conditioning with Factors

  • Conditioning can use a strictly positive potential instead of a likelihood, allowing inference to target an unnormalized density for reward or utility.A factor form can insert the potential as an observed node whose score is gated by the current path predicate.Source: §3.2.1 · pp. 74–75

3.2.2 Partial Evaluation

  • Partial evaluation precomputes expressions without free variables to prevent spurious graph edges and identify deterministic expressions.It simplifies conditionals and primitive calls, can remove an inactive branch’s variable dependency, and helps verify that observed values are deterministic.Source: §3.2.2 · pp. 76–77
  • Data-structure-aware partial evaluation removes spurious Markov-chain edges by simplifying vector construction, append, and access operations, yielding the correct dependency structure.Without these rules, later states can acquire arcs from all preceding states even though only the immediately previous state is a parent.Source: §3.2.2 · p. 79

3.3 Inference Algorithms

  • No inference algorithm is universally best: Monte Carlo methods trade sample efficiency against update cost, while expectation propagation and variational inference trade asymptotic guarantees for scalable parametric approximations.These approximations may estimate higher-order moments inaccurately, making them unsuitable when precise uncertainty estimates matter; the choice can depend on diagnostics. Gibbs can be slow for highly correlated variables, HMC uses costly gradient evaluations, and sequential Monte Carlo suits incrementally presented evidence.Source: §3.3 · pp. 80–81

3.4 Gibbs Sampling

  • Gibbs sampling updates one variable at a time from its full conditional, replacing a joint high-dimensional update with a sequence of lower-dimensional updates.It is especially effective when conditional independence or conjugacy makes the conditionals analytically tractable; Metropolis-within-Gibbs uses Metropolis–Hastings updates when such optimizations are unavailable.Source: §3.4 · p. 83

3.4.1 Evaluating the Metropolis-Hastings Acceptance Ratio

  • O(|Vx|) time suffices for an acceptance-ratio computation, and local-variable updates can make a full Gibbs sweep linear rather than quadratic in data size.Only probabilities for the updated variable and its dependents need recomputation; in mixture and state-space models, local-variable ratios can typically be computed in constant time.Source: §3.4.1 · p. 85

3.4.2 Choosing Proposals

  • A proposal map Q assigns each unobserved variable an expression defining a distribution used to generate candidate values and evaluate forward and reverse proposal densities.The implementation assumes sample and log-prob functions are available for those distributions.Source: §3.4.2 · p. 85
  • A straightforward proposal is the prior; for continuous variables, a small-variance Gaussian centered at the current value is symmetric, simplifying the acceptance probability.The Gaussian proposal's standard deviation can be tuned online during sampling.Source: §3.4.2 · pp. 86–87

3.4.3 Block Gibbs sampling

  • Block Gibbs updates can help when correlated variables trap single-variable updates, but naive prior-based block proposals may perform worse than independent proposals.Adaptive proposals can use estimated posterior covariance matrices to jointly propose values for multiple latent variables.Source: §3.4.3 · pp. 87–88

3.4.4 Implementation Considerations

  • Pattern matching can identify tractable complete conditionals for finite discrete or exponential-family variables, enabling exact enumeration; Metropolis-within-Gibbs is often inefficient without model-customized blocks.Hamiltonian Monte Carlo uses gradient information to make efficient high-dimensional block proposals.Source: §3.4.4 · p. 88

3.5 Compilation to a Factor Graph

  • FOPPL programs can be transformed into factor graphs by representing deterministic operations as Dirac-distributed variables, yielding an undirected model for inference.The transformation inserts such variables for if forms and primitive procedure applications; unnecessary Dirac factors and variables can also be eliminated.Source: §3.5 · pp. 91–92

3.6 Expectation Propagation

  • Expectation propagation approximates the target distribution with fully factorized exponential-family factors, using moment matching to update messages and estimate the normalizing constant.The estimate uses normalizing constants from the tilted distributions and the approximating distribution.Source: §3.6 · pp. 95, 96, 98

3.6.1 Implementation Considerations

  • EP update schedules are not guaranteed to converge, and finding a near-optimal schedule remains open; Infer.NET development has focused substantially on scheduling heuristics.The schedule implemented by schedule(G) is described as perhaps the most important design consideration.Source: §3.6.1 · p. 98
  • EP requires an exponential-family distribution for every variable and computable factor integrals Zf and message sufficient statistics; the latter two requirements impose substantial program restrictions.The first requirement is relatively easy to satisfy, while computing the integrals and sufficient statistics for each potential type constrains programs.Source: §3.6.1 · pp. 98–100
  • Representing each deterministic operation as its own factor avoids harder integrals over constraints spanning multiple operations.The example shows that separate primitive-operation factors can make individual integrals tractable, including closed-form Gaussian-product and numerical truncated-Gaussian cases.Source: §3.6.1 · p. 100
  • For sample and observe factors, the chosen message exponential family must be compatible with their densities, since products of densities are generally integrable only in limited cases such as conjugate prior–likelihood pairs.These stochastic factors require integration over products of densities.Source: §3.6.1 · pp. 100–101
  • For deterministic factors, if expressions can use gates to propagate messages via expected indicator values, while primitive-call integrals must depend on both the primitive and message-family types.For example, subtraction requires different integral implementations for Gaussian, Gamma, or mixed Gaussian–Gamma inputs.Source: §3.6.1 · p. 101

Inference with Evaluators

Dynamic-support models require inference beyond a graph compiled in advance: evaluators can instantiate variables as programs run, while likelihood weighting provides a simple evaluation-based method for approximating posterior expectations.

Inference with Evaluators

  • Dynamic-support models can use bounded variable sets with auxiliary on/off indicators, or inference can instantiate variables at run time by evaluating the program, potentially without a fixed bound.Evaluation-based methods need not know the dependency graph before execution; it can be built dynamically or left implicit.Source: Inference with Evaluators · pp. 102–103
  • Lazy evaluation of if-expressions lets program runs generate prior samples while recording evaluated observe probabilities, whereas eager evaluation can fail for recursive programs with unbounded random-variable counts.This chapter assumes FOPPL programs with lazy if-expressions, for which the set of sample and observe expressions is finite.Source: Inference with Evaluators · pp. 103–105

4.1.1 Background: Importance Sampling

  • Importance sampling estimates posterior expectations with weighted samples from a proposal, using the joint density for unnormalized weights and yielding a marginal-likelihood estimate useful for Bayesian model comparison.The proposal must cover the posterior's support.Source: §4.1.1 · pp. 105–107

4.1 Likelihood Weighting

  • Likelihood weighting is an importance-sampling method that uses the prior as its proposal, making each unnormalized importance weight the likelihood.It is introduced as one of the simplest evaluation-based inference methods.Source: §4.1 · p. 105, §4.1.1 · p. 107

4.1.2 Graph-based Implementation

  • In the graph-based likelihood-weighting procedure, sample each latent variable from its conditional prior given its parents, then compute each observed variable's likelihood weight.Source: §4.1.2 · p. 108

3. Return the weighted set of return values r(Xl)

Likelihood weighting can generate weighted return values by repeatedly evaluating a FOPPL program, sampling from the prior and accumulating observation log probabilities. The implementation avoids explicitly representing the dependency graph or tracking how many sample and observe statements execute.

3. Return the weighted set of return values r(Xl)

  • Prior sampling processes variables in topological order so each variable’s parents are sampled first.The likelihood terms can be calculated by evaluating the target-language expression P(y) with sampled values substituted for each parent of y.Source: §3 · p. 108

4.1.3 Evaluation-based Implementation

  • A recursive evaluator handles the FOPPL’s non-probabilistic forms, though matching expression templates requires additional implementation code.Its state map stores inference variables computed as side effects, while a local environment holds bindings; the pseudo-code is succinct but hides the extra matching work.Source: §4.1.3 · pp. 110–111
  • The evaluator draws from each prior distribution at sample expressions and adds each observed value’s log probability to the inference state’s log weight.The observe case evaluates its distribution and value, adds log-prob(d1, c2), and returns the observed value.Source: §4.1.3 · p. 112
  • Repeated program executions produce return values and their final log importance weights, without explicitly representing the dependency graph or tracking sample and observe counts.Each execution starts with log W = 0; the evaluator samples at sample expressions and accumulates log probabilities at observe expressions.Source: §4.1.3 · pp. 112–113

Aside 1: Relationship between Evaluation and Inference Rules

Evaluation and inference translation share a recursive structure: translate each subexpression first, then assemble the expression from the resulting values. For primitive calls, the translation combines the subexpression graphs and translates the call using their translated expressions.

Aside 1: Relationship between Evaluation and Inference Rules

4. Return the pair G, E

Inference rules specify translation behavior and provide a recipe for implementing recursive translation. They also define the evaluation-based likelihood-weighting method.

  • Inference rules specify how translation behaves and provide a recipe for implementing a recursive translate operation for each expression type.Source: §4 · p. 114
  • Inference rules define evaluation-based likelihood weighting, similarly to how they specify big-step semantics; the rules are shown in Figure 4.1.Source: §4 · p. 114

Aside 2: Side Effects and Referential Transparency

The aside distinguishes pure expressions from sampling and observation effects, while the following Metropolis–Hastings material reuses evaluation’s likelihood weights and develops proposals that handle changing sets of executed variables.

Aside 2: Side Effects and Referential Transparency

  • Pure expressions are referentially transparent, but sample expressions are opaque, while observe returns a stable value and changes the log weight as a side effect.Replacing observe with its observed value preserves the prior distribution on return values but leaves the log weight at zero; compilation also uses transparency to replace target expressions without free variables by their values and to ensure observed values are transparent.Source: Aside 2: Side Effects and Referential… · pp. 114–116

4.2 Metropolis-Hastings

  • With independent proposals from the program prior, the Metropolis–Hastings acceptance ratio reduces to a likelihood ratio computed using the evaluator’s log-weight side effect.The proposal likelihood weight W′ and previous weight W give the ratio W′/W, so the likelihood-weighting evaluator can be reused.Source: §4.2 · pp. 117–118

4.2.1 Single-Site Proposals

  • General Metropolis–Hastings proposals must condition on the previous sample and compute reverse-proposal probabilities, complicated when executions contain different subsets of sample expressions.In the lazy branching example, changing a branch can remove one latent variable and introduce another.Source: §4.2.1 · p. 118
  • Single-site Metropolis–Hastings selects one existing variable to resample, samples variables absent from the previous execution, and reuses other prior values where possible.The proposal is constructed by rerunning the program, sampling when the variable is the selected site or was absent before, and otherwise reusing its previous value.Source: §4.2.1 · p. 119

Addressing Transformation

Evaluation-based acceptance ratios assume each sample or observe expression has a unique address. For FOPPL programs, a source transformation inserts fresh variable identifiers into those expressions.

Addressing Transformation

  • Acceptance-ratio definition assumes a variable can be associated with every sample or observe expression; such a unique identifier is called an address.The text relates this assumption to assigning each sample and observe expression a unique variable during graphical-model compilation.Source: Addressing Transformation · p. 122
  • For FOPPL programs, transformation ρ inserts a newly created variable as an address in every sample and observe expression.It changes (sample e) to (sample v e) and (observe e1 e2) to (observe v e1 e2).Source: Addressing Transformation · pp. 122–123
  • Unique addresses can also be constructed dynamically at run time when needed.The text returns to this possibility in Chapter 6, Section 6.2.Source: Addressing Transformation · p. 122

Evaluating Proposals

This section develops evaluators and proposal mechanisms for single-site Metropolis–Hastings, sequential Monte Carlo, and black-box variational inference, including methods that condition or sequence program execution to construct and assess proposals. It also covers BBVI’s sample-based gradient evaluator, mean-field implementation, sample cost, and graph-based variance-reduction optimization.

Evaluating Proposals

  • Single-site MH tracks values, per-variable log densities, and a conditioning cache, reusing cached samples except at the proposed site and scoring sample and observe expressions.Each iteration randomly selects a current site, reruns the program to propose, then accepts or rejects by the Metropolis–Hastings ratio.Source: Evaluating Proposals · pp. 123–124

4.3 Sequential Monte Carlo

  • SMC mitigates likelihood weighting’s exponentially declining success by bridging low- to high-dimensional densities and resampling high-weight particles, especially when weights in successive densities are strongly correlated.Likelihood weighting can work well with few sample expressions, but generating a good proposal becomes exponentially less frequent as their number grows.Source: §4.3 · pp. 125–126

4.3.1 Defining Intermediate Densities with Breakpoints

  • Intermediate SMC densities come from successively larger truncated programs, whose breakpoints must be ordered and occur at expressions evaluated in every execution.Breakpoints may be user-specified, derived by program analysis, or defined at run time; breaking at every observe is an alternative when executions halt at the same point.Source: §4.3.1 · p. 127

4.3.2 Calculating the Importance Weight

  • With bootstrapped proposals sampled from the program prior, each generation’s importance weight is the joint probability of observes evaluated since the preceding breakpoint.The proposal is conditioned on the preceding particle, and the incremental weight reflects the observed variables newly included at that generation.Source: §4.3.2 · pp. 127–128

4.3.3 Evaluating Proposals

  • The SMC propose evaluator reuses prior particle values, samples new variables from the prior, accumulates observe log likelihoods, and stops at the specified breakpoint.It returns the values and log likelihood for the truncated program, or the program’s return value and log likelihood when no breakpoint is reached.Source: §4.3.3 · p. 128

4.3.4 Algorithm Implementation

  • At each iteration, log ˆZn−1 is calculated by evaluating the function.Source: §4.3.4 · p. 129

4.3.5 Computational Complexity

  • Rerunning the program at each generation makes this SMC implementation a proof of concept with O(LN^2) cost, rather than the O(LN) cost of constant-time proposal steps.A continuation-based execution model can avoid rerunning the first n −1 steps at every stage.Source: §4.3.5 · pp. 130–131

4.4 Black Box Variational Inference

  • BBVI learns an importance-sampling proposal by maximizing the ELBO to approximate the posterior, using a mean-field assumption that makes variables independent.Its noisy ELBO gradients use likelihood-ratio (REINFORCE-style) estimation; the evaluator proposes from Q(v) for each variable and tracks importance weights and proposal-density gradients.Source: §4.4 · pp. 131–132, §4.4.1 · p. 132, §4.4.2 · p. 133

4.4.2 Evaluator for Gradient Estimation

  • The evaluator estimates ELBO-gradient components for each variable from repeated executions, including gradients of the log proposal density, then passes them to an optimizer step.The optimizer can use stochastic gradient updates or methods such as Adam or Adagrad.Source: §4.4.2 · pp. 134–135

4.4.3 Computational Complexity and Statistical Efficiency

  • The mean-field approximation makes BBVI relatively simple to implement because proposal log-density gradients can be computed independently for each variable using primitive-distribution gradients.The implementation requires log-density gradients for each supported primitive distribution type.Source: §4.4.3 · p. 136
  • 10^6 or more samples may be needed before BBVI generates high-quality proposals.The estimator may require L values of order 10^2 or 10^3 per gradient step and T iterations of order 10^3 to 10^4; reparameterized variational-autoencoder methods can use L = 1 for many models.Source: §4.4.3 · p. 136
  • A graph-based optimization restricts each variable’s weight to its Markov blanket, reducing variance by omitting weight components unrelated to that variable.This is interpreted as a form of Rao-Blackwellization, and a graph-based BBVI implementation can construct each variable’s Markov blanket.Source: §4.4.3 · pp. 136–137

A Probabilistic Language With Recursion … Conclusion

This section develops higher-order probabilistic programming and surveys inference methods spanning execution-based control, gradient-based inference, and neural amortized and variational inference. It also covers implementation mechanisms and language-design constraints, including unresolved challenges for general-purpose inference.

A Probabilistic Language With Recursion

  • HOPPL permits unbounded random-variable counts, so infinite models cannot be compiled into finite graphs; evaluation-based inference can instead consider finitely many variables at a time.Stochastic recursion may also fail to terminate, as repeated random choices can continue indefinitely.Source: A Probabilistic Language With Recursion · pp. 138, 140

5.1 Syntax

  • HOPPL adds recursive procedures and first-class functions, while finite versus unbounded random-variable cardinality—not these specific features alone—marks the key language distinction.Functions can be created locally, passed as arguments, and called recursively; other mechanisms can also produce unbounded-cardinality models.Source: §5.1 · pp. 142–143

5.2 Syntactic sugar

  • Syntactic sugar defines let through nested functions and recreates the FOPPL loop, making every valid FOPPL program valid in HOPPL.The loop sugar is desugared before let, with a recursive helper implementing iteration.Source: §5.2 · pp. 143–144

5.3 Examples

  • HOPPL examples use functional-programming patterns to write more conventional code, including higher-order functions and revised or newly enabled probabilistic models.The examples revisit models from Chapter 2 and introduce models that rely on the new language features.Source: §5.3 · pp. 144–145

Examples of higher-order functions

  • Map recursively applies a function across a sequence, while reduce folds a sequence into one value and lets an HMM process data of runtime length.The HMM uses reduce to traverse input data and build a vector of latent states, avoiding a hard-coded iteration count.Source: Examples of higher-order functions · pp. 145–147

Open-universe Gaussian Mixtures

  • Recursive iteration enables open-universe Gaussian mixtures with an unbounded, Poisson-prior component count, so additional data need not rule out more clusters.Moving observe statements earlier, or delaying sample calls, leaves the joint distribution unchanged and can improve SMC efficiency.Source: Open-universe Gaussian Mixtures · pp. 147–149

Sampling with constraints

  • A higher-order rejection sampler repeatedly proposes values until a validity function accepts one, enabling constrained samplers that cannot be expressed in the FOPPL.The method can implement samplers for otherwise unsupported distributions, including constrained simulation-based models in physical sciences.Source: Sampling with constraints · p. 149

Program synthesis

  • Recursive HOPPL grammars can generate arithmetic-function source code of unbounded size, though sufficiently likely recursive expansion can make programs fail to terminate.Generated code must be evaluated to fit data; single-site Metropolis-Hastings can then produce posterior samples conditioned on three input-output pairs.Source: Program synthesis · pp. 152–153

Captcha-breaking

  • A HOPPL CAPTCHA model samples text and rendering distortions, renders a candidate image, and scores it against a target using an image-similarity measure.The renderer may be an external black box if its behavior is deterministic and side-effect-free and the HOPPL program can invoke it.Source: Captcha-breaking · pp. 154–155

Inference Across a Messaging Interface

  • Messaging-based inference treats each terminating HOPPL execution as a finite set of random variables and yields control to an inference controller at sample and observe expressions.This separates deterministic program execution from algorithm-specific inference operations, allowing the controller to treat execution as a black box and work independently of the program’s language or machine.Source: Inference Across a Messaging Interface · pp. 156–157, §6.1 · p. 158

6.1 Explicit separation of model and inference code

  • Execution pauses at sample and observe messages for controller responses and can fork from that point; transformed HOPPL code supports inference algorithms without an explicit evaluator.The interface separates program execution from the inference controller.Source: §6.1 · pp. 159, 162

6.2 Addressing Transformation

  • Dynamic addresses derived from function-call stacks uniquely identify sample and observe expressions, including in programs that instantiate an unbounded number of variables.The addressing transformation keeps the output in HOPPL, up to redefining sample and observe to accept an additional argument.Source: §6.2 · p. 165

6.3 Continuation-Passing-Style Transformation

  • CPS exposes a continuation at each sample and observe expression, allowing inference to continue an execution once or fork it by calling the continuation multiple times.The continuation represents the remaining computation and acts as a snapshot of an intermediate state.Source: §6.3 · p. 166

Variables, Procedure Names and Constants

  • CPS transforms variables, procedure names, and ground constants by passing their values to the continuation, while primitive functions and user-defined procedures are adapted to accept continuation and state arguments.Conditional branches are transformed under the continuation, then selected by a continuation that receives the predicate value.Source: Variables, Procedure Names and Constants · pp. 167–169
  • CPS-transformed procedure calls build nested continuations that capture pending computation and in-scope variables, enabling execution to be interrupted with a continuation-state snapshot.The continuation is constructed dynamically as arguments are evaluated; it closes over variables already defined and can be returned instead of called.Source: Variables, Procedure Names and Constants · pp. 169, 171

Observe and Sample

  • CPS analogues of sample and observe take a continuation and state, then suspend execution and return the address, parameters, continuation, and state to an inference algorithm.The translation assumes an addressing transformation has already added an address argument to sample and observe.Source: Observe and Sample · pp. 171–172

6.4 Message Interface Implementation

  • A client-server message protocol separates the execution server from the inference controller, allowing separate languages or machines if both serialize and deserialize the same distribution primitives.The controller sends requests to start, continue, and fork executions; the server reports sample, observe, and return events.Source: §6.4 · pp. 172–173

6.5 Likelihood Weighting

  • Likelihood weighting launches L asynchronous executions, samples at sample interrupts, accumulates observe log weights, and outputs weighted returns; multithreading can exploit its parallelism.At completion, each execution outputs its return value and accumulated log weight.Source: §6.5 · p. 176

6.6 Metropolis-Hastings

  • Dynamic addresses require address transformation; Metropolis–Hastings may rerun the program, but saved continuations and callsite caching can skip unchanged computation (Tolpin et al., 2016; Ritchie et al., 2016b).A stored continuation at an address skips computation preceding that address; callsite caching can skip re-execution when function arguments and the closed-over environment are identical.Source: §6.6 · pp. 177, 179

6.7 Sequential Monte Carlo

  • Sequential Monte Carlo uses fork-capable asynchronous executions and delays resampling at an observe until all L executions reach that same observe, tracking its address and the number arrived.The controller starts L executions in parallel and uses the fork message to support resampling.Source: §6.7 · p. 179
  • SMC resampling assumes every execution reaches observes in the same order; conditional observes violate this assumption, though resampling could instead target guaranteed observes or use ordered addresses.The described strategy throws an error if executions do not share the same observe sequence.Source: §6.7 · p. 181

Inference in Differentiable Models

  • Graph compilation exposes variable dependencies for inference but restricts programs to first-order functions and no stochastic control flow, while HOPPL supports dynamic variables at the cost of more complex inference.HOPPL can reuse deterministic library functions and separate the program language from the inference backend.Source: Inference in Differentiable Models · pp. 182–183

7.1 Higher-order Probabilistic Programs with Static Support

  • Stan-like languages allow dynamic computations while fixing random-variable support at compile time, avoiding execution-dependent variable sets.Stan, PyMC3, Rainier, and Infergo use this middle-ground design; the density is computed dynamically at run time.Source: Inference in Differentiable Models · pp. 183–184

Inference in Differentiable Models

  • Gradient-based Stan inference uses automatic differentiation to compute log-density gradients for HMC and related methods, but requires continuous variables and can be hindered by control-flow discontinuities.Stan leaves users to assess whether discontinuities create problems; language designs can instead account for them explicitly.Source: Inference in Differentiable Models · p. 184

7.1.1 Static Addressing in the HOPPL

  • Static addressing requires each sample to name its variable in source code, making the finite set of unobserved variables statically determinable.Programs can still use higher-order functions such as map and avoid inlining data, while explicit addresses simplify inference by preventing execution-dependent variable sets.Source: §7.1.1 · p. 187

7.1.2 Computing the Unnormalized Density as a Side Effect

  • A deterministic translation accepts free variables as inputs and computes the program’s return value and potential energy by side effect.Sample and observe calls subtract their log probabilities from energy; factor subtracts its supplied value.Source: §7.1.2 · pp. 188–189

7.1.3 Densities with Control Flow and Non-Unique Variables

  • A statically addressed sample inside unexecuted control flow can leave an unused variable and yield an improper constant density, because sample increments log density rather than defining a random variable.The passage notes that eager and lazy FOPPL interpretations differ in trace and density, though their posterior marginals on return values are equivalent.Source: §7.1.3 · p. 190
  • Stan-like programs have a well-defined prior when dynamic density computation evaluates exactly one sample expression for each random variable, a condition expected for most practical programs.Repeatedly sampling variables at non-unique addresses creates ambiguity about whether the program is valid.Source: §7.1.3 · pp. 191–192

7.2 Hamiltonian Monte Carlo

  • For continuous target densities, HMC uses momentum and Hamiltonian trajectories to propose large moves while preserving the target marginal; its Metropolis-Hastings step corrects for integration errors.The auxiliary momentum does not change the distribution over the original variables, and reversible trajectories are accepted with probability 1 when they preserve the Hamiltonian.Source: §7.2 · pp. 192, 194, 195

7.3 Automatic Differentiation

  • Reverse-mode automatic differentiation builds a computation graph during program evaluation, then propagates derivatives backward by the chain rule, enabling gradients for complex programs through AD-compatible primitives.The examples include data structures, control flow, disk access, and mutation; the approach makes primitive calls create graph nodes and edges while recording local derivatives.Source: §7.3 · pp. 197–199
  • Reverse-mode AD walks the computation graph backward, summing pathwise derivatives formed by multiplying partial derivatives, and requires AD-compatible primitives without guaranteeing differentiability under control flow.Discontinuities from control flow can still arise even when reverse-mode AD is implemented.Source: §7.3 · pp. 201–202

7.4 Implementation Considerations

  • HMC combines leapfrog integration with momentum Gibbs updates and Hamiltonian-based accept-or-reject decisions, while performance depends on trajectory length T, step size ϵ, and mass matrix M.Dynamic stopping and step-size adaptation can reduce dependence on T and target an acceptance ratio.Source: §7.4 · pp. 204–205
  • Discontinuities can lower HMC acceptance and mixing efficiency, but gradient-based methods can apply to broader programs when primitives are analytic and expressions real-valued.The text describes differentiability almost everywhere under suitable conditions, including for some higher-order probabilistic programs.Source: §7.4 · pp. 206, 208
  • For fundamentally discrete models, gradients may provide no guidance: the geometric-program example has an almost-everywhere differentiable energy whose gradient is zero wherever defined.Source: §7.4 · p. 208

7.5 Other Methods for Differentiable Models

  • Statically determinable latent variables paired with AD-based HMC form a flexible approach that often performs well for moderately high-dimensional distributions; reverse-mode AD also enables reparameterized gradients for ADVI and variational autoencoders.Source: §7.5 · p. 209

Deep Probabilistic Programming

  • Deep probabilistic programming combines probabilistic abstractions with differentiable-programming DSLs to address unrealistic hand-specified data models and large-dataset scalability, using static graphs in first-order systems or dynamic runtime graphs in higher-order systems.Source: Deep Probabilistic Programming · pp. 210–211
  • Neural-network-parameterized programs enable flexible deep generative models, while data-dependent guides amortize inference for faster test-time use.The chapter also surveys implementation strategies that connect generative and inference programs.Source: Deep Probabilistic Programming · pp. 211–212

8.1 Programs as Deep Generative Models

  • Deep generative programs may require estimating millions of parameters, so the chapter focuses on marginal-likelihood learning rather than high-dimensional Bayesian weight inference.Bayesian deep learning requires approximating a high-dimensional posterior whose local optima may represent distinct modes.Source: §8.1 · p. 214

8.2 Programs as Inference Models

  • A guide q(X|Y;φ) amortizes repeated posterior inference, while variable correspondences can be encoded with interleaved proposals or shared addresses.A neural network can predict variational parameters from each observation, avoiding separate variational parameters for every inference problem.Source: §8.2 · pp. 215, 217

8.3 Stochastic-Gradient Methods for Learning and Inference

  • Variational learning frames model-parameter estimation as ELBO maximization and can jointly train model parameters and amortized inference networks from observations.The variational framework also supports importance-sampling-based model learning and relates amortized inference to wake-sleep and variational autoencoders.Source: §8.3 · p. 218

8.3.1 Maximizing the Marginal Likelihood

  • Maximum-likelihood gradients are posterior expectations, enabling stochastic updates from posterior samples and requiring parameter-density gradients rather than HMC’s full automatic differentiation.The point-estimate justification has caveats: it does not address model misspecification and breaks down for infinite-dimensional parameter spaces.Source: §8.3.1 · pp. 219–221
  • Maximum-likelihood learning can use discrete latent variables, provided the parameters being learned are continuous.Source: §8.3.1 · p. 222

Maximum Likelihood Estimation using Importance Sampling

  • Importance sampling uses a learned posterior proposal to approximate marginal-likelihood gradients, avoiding restarting inference at every model-parameter update.The self-normalized estimator is biased but consistent, with bias diminishing as the sample count increases; jointly learning the proposal and model also requires tracking changing model parameters.Source: Maximum Likelihood Estimation using… · pp. 222–224

8.3.2 Combining Learning and Variational Inference

  • With an infinitely flexible variational family, maximizing the ELBO over θ and λ equals maximum-likelihood estimation and yields an unbiased gradient estimate; finite-capacity, incompletely optimized families differ from marginal-likelihood maximization.Factorized families cannot represent posterior correlations; when the posterior variables are correlated, this creates an approximation gap that can bias model-learning gradients.Source: §8.3.2 · pp. 225–226

8.3.3 Approximating Gradients of Variational Objectives

  • The section presents inclusive-KL gradients estimated by self-normalized importance sampling and reparameterized gradients as alternatives to BBVI’s exclusive-KL likelihood-ratio estimator.Reparameterized estimators were popularized in variational autoencoders.Source: §8.3.3 · pp. 226–227

Self-normalized Gradients of the Inclusive KL Divergence

  • Exclusive-KL approximations can seek one posterior mode and underestimate variance, while inclusive-KL approximations cover modes and may overestimate variance.Importance sampling can correct an overestimate by downweighting samples in low-density regions; correcting an underestimate requires high-weight outliers and often many more proposals. Learned proposals can generate posterior-weighted samples that then improve the proposal, and the same samples can support model and variational updates.Source: Self-normalized Gradients of the… · pp. 228–230

Reparameterized Gradients of the Exclusive KL Divergence

  • Reparameterized gradients tend to have lower variance than likelihood-ratio and importance-weighted estimators, sometimes yielding useful estimates from one sample.They rewrite expectations over a parameterized distribution as expectations over a parameter-free distribution, allowing gradients to pass through the sampling transformation by the chain rule.Source: Reparameterized Gradients of the… · pp. 231–233
  • Reparameterized gradients exclude discrete variables and require model densities differentiable with respect to latent variables, adding implementation demands but improving sample efficiency.These gradient requirements match those for Hamiltonian Monte Carlo and require automatic differentiation of the model density.Source: Reparameterized Gradients of the… · pp. 233–234

8.3.4 Amortized Inference

  • Amortized inference trains a shared network to map each input to variational parameters, trading upfront computation for fast inference on new data.Network weights can be learned with likelihood-ratio or reparameterized estimators for the exclusive KL, or a self-normalized estimator for the inclusive KL.Source: §8.3.4 · pp. 234–235

Variational Autoencoders

  • Structured probabilistic programs let VAEs combine neural networks with domain knowledge, but discrete variables and stochastic control flow can make continuous relaxations impractical.Mixed-variable models can instead combine reparameterization with likelihood-ratio estimation or minimize the inclusive KL.Source: Variational Autoencoders · p. 238

Helmholtz Machines

  • Reweighted wake-sleep uses importance weighting to address poor early inference-network learning, and wake-wake practice omits sleep updates that may impair convergence.Wake-wake uses training-data-conditioned updates and inclusive-KL self-normalized estimates, so it does not require reparameterization and applies to discrete variables and stochastic control flow.Source: Helmholtz Machines · pp. 240–241

8.4 Design of Deep Probabilistic Programming Systems

  • Deep probabilistic programming systems must decide how to structure generative and inference models, choose FOPPL or HOPPL, align variables, and handle mixed-variable programs.The design questions also include implementing parameter learning with complex control flow.Source: §8.4 · p. 242

8.4. Design of Deep Probabilistic Programming Systems

  • Backend and language-integrated amortized inference offer different tradeoffs: the former supports nondifferentiable programs, while the latter enables one differentiable program to define both models.Both approaches can constrain proposal generation to the order variables are instantiated, potentially mismatching inference dependencies with posterior independencies.Source: §8.4 · p. 243

8.5 Implementing Proposals in the Inference Backend

  • A messaging interface separates deterministic program execution from inference, allowing a backend to generate proposals and gradients, potentially in a different language.PyProb uses this separation to learn Python proposals for models written in other languages, including existing C or C++ simulation code.Source: §8.5 · p. 245
  • A lighter message-based backend supports amortized inference by dispatching sample and observe requests with unique runtime addresses, while requiring backend—but not modeling-language—automatic differentiation.Addresses combine lexical base addresses with counts of requests in each process.Source: §8.5 · pp. 245–246

8.5.1 Implementing Recurrent Proposals

  • Recurrent proposals process sample requests in execution order, using data, addresses, prior distributions, preceding samples, and hidden state to produce proposal parameters.The general architecture still needs program-specific encoders, especially for input-data modality.Source: §8.5.1 · pp. 246–247

8.5.2 Computing Gradients of the Variational Objective

  • Unboxing self-normalized importance weights makes their proposal-parameter gradients zero, removing unintended weight-gradient terms from the inclusive-KL gradient estimate.The naive loss otherwise introduces extra terms because the weights depend on the proposal parameters.Source: §8.5.2 · pp. 249–250

8.5.3 Implementing Reweighted “Wake-Wake”

  • The reweighted wake-wake procedure starts multiple executions, handles sample and observe requests, and averages their weights and model and proposal log-joints to compute objectives.Sample requests update the proposal hidden state and execution weight; observe requests update the weight and model log-joint.Source: §8.5.3 · p. 252

8.6 Integrating Proposals into Probabilistic Programs

  • Replacing sample expressions with propose expressions preserves program density through a prior-to-proposal correction and lets standard likelihood weighting account for user-specified proposals.The correction uses the log density ratio between the prior and proposal.Source: §8.6 · p. 255

8.6.1 Computing the Gradient of the Lower Bound

  • When proposals are defined in the modeling language, automatic differentiation also enables reparameterized ELBO gradients, which can be more efficient with fewer samples in some models.This provides an alternative to reweighted wake-wake inference.Source: §8.6.1 · p. 256
  • Continuous variables typically use reparameterized gradients to reduce variance, while discrete variables use likelihood-ratio estimators; one objective can combine both.Reparameterized and non-reparameterized proposal terms contribute through different gradient terms.Source: §8.6.1 · pp. 256, 258

8.6.2 Implementing Autoencoding Variational Inference

  • The inference computation runs L program executions, tracks each execution’s log importance weight and base-proposal log probability, and averages their losses.Reparameterized samples require automatic differentiation through the proposal transformation; using the same language runtime for execution and inference simplifies gradient computation, while cross-language implementations require serializing computation graphs.Source: §8.6.2 · pp. 260–261

8.7 Using Standalone Programs as Proposals

  • Standalone proposal programs let users choose proposal dependency structures empirically, including variable orderings that cannot condition only on upstream generative variables.The best ordering is often an empirical question, and both discussed orderings are described as faithful inverses.Source: §8.7 · pp. 261–262

8.7.1 Importance Sampling with Missing and Auxiliary Variables

  • Higher-order programs have dynamically instantiated variable sets, so explicit addresses identify corresponding variables, while proposal traces may contain missing or auxiliary addresses.A conservative weighting strategy assigns zero probability to traces with either discrepancy; an extended-space strategy can instead accommodate them.Source: §8.7.1 · pp. 263–265
  • Extending target and proposal densities to shared support yields valid importance weights, with the extended target marginal equal to the original target.In the example, shared terms cancel; generally, weights include observation probabilities and probability ratios for sample addresses present in both programs.Source: §8.7.1 · p. 266
  • Auxiliary variables can be sampled and discarded without changing the target marginal, so their extended-space importance weight is also valid for the original density.The arbitrary-program strategy reuses proposal values where available, samples missing target variables from their priors, and weights using observations and sample expressions shared by target and proposal.Source: §8.7.1 · pp. 266–267

8.7.2 Implementing Messaging with Algebraic Effect Handlers

  • Composable algebraic-effect handlers intercept sample and observe operations, with inference state tracked either immutably through requests or using mutable state.Immutable-state handlers require programs to pass updated state between calls; mutable state avoids that rewrite but must be reset for each execution, and mutable data structures in probabilistic programs can make samples non-identically distributed.Source: §8.7.2 · pp. 268, 270, 271, 272

8.7.3 Implementing Importance Sampling with Handlers

  • Importance sampling handlers evaluate the proposal, condition the target on its sampled trace, and compute the log weight and target and proposal joint probabilities for variational objectives.Composed handlers store traces and log probabilities, reuse proposal values in the target, and accumulate observed-variable likelihoods.Source: §8.7.3 · p. 274, Figure 8.2 · p. 275
  • Composable handlers separately manage traces, log probabilities, and conditioning, making importance-sampling code more modular than monolithic proposal algorithms.The handlers can be stacked to apply side effects during different parts of inference.Source: Figure 8.3 · p. 276, §8.7.3 · p. 277

8.7.4 Implementing Stochastic Variational Inference

  • Vectorized proposal generation is more GPU-friendly than L×B individual calls, but stochastic control flow can require program design compatible with the numerical backend.The implementation distinguishes sample size from event size and uses a handler to broadcast distributions to the requested batch size.Source: §8.7.4 · pp. 280–282
  • Self-normalized importance samples provide gradient objectives for the generative and inference models, including estimators for log marginal likelihood and inclusive KL divergence.The objectives are computed from importance log weights and the models’ log joint probabilities.Source: §8.7.4 · p. 283

8.8. Combining Differentiable and Probabilistic Programming

  • Deep probabilistic programming combines stochastic-gradient scalability and flexible neural proposals with generative programs that encode inductive biases for domain-specific learning.The chapter presents importance weights as central to stochastic-gradient learning and inference across variational, wake-wake, and standalone-proposal methods.Source: §8.8 · p. 285

Conclusion

  • Probabilistic programming translates programs into mathematical objects for generic inference, but inference hardness makes implementation knowledge important even for proficient users.Inference can be NP-hard for unrestricted discrete graphical models and harder with continuous variables or potentially infinite collections; evaluators therefore rely on approximate algorithms that work well only for some problems.Source: Conclusion · pp. 286–287
  • First-order inference advances for graphical models transfer largely to probabilistic programming, but higher-order languages face an unresolved infinite-dimensional parameter space and need more general-purpose inference.The conclusion presents this as an opportunity for foundational infrastructure research, particularly in approximate inference.Source: Conclusion · pp. 287–288
  • Special-purpose inference for a single model is usually a poor investment unless that model is exceptionally important, since a slower general algorithm may converge with less total effort.The authors compare one-model inference optimization to a compiler optimization for a single program.Source: Conclusion · p. 288
  • Flexible language design trades off against changing model parameter counts and inference difficulty, motivating languages that balance natural model expression, efficient forward calculation, and predictable compilation.The authors favor reusing existing language design and infrastructure where modeling flexibility is desired, while noting accumulated technical debt across language iterations.Source: Conclusion · p. 288
  • Model learning and rapid, approximate repeated inference are paramount for AI applications, so language design’s effects on user expressiveness and evaluator computation should shape future AI toolchains.The text’s focus was mostly on automating one-shot inference in known, fixed models.Source: Conclusion · pp. 288–289
Loading 1809.10756v2…