Skip to content

Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF006 · Algorithm

Expectation propagation (EP)

Approximates a factorized probability model by replacing difficult factors with tractable sites, repeatedly matching moments of local tilted distributions.

Explained here Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Approximates a factorized probability model by replacing difficult factors with tractable sites, repeatedly matching moments of local tilted distributions. EP is an inference algorithm for a specified model, not a neural architecture or an exact general-purpose posterior calculation.

Question / hypotheses

Can tractable site factors reproduce selected moments of each original factor combined with the current approximation to the remaining model?

Architecture / computational structure

Remove one site to form a cavity, insert the exact factor to form a tilted distribution, match its sufficient-statistic moments, and divide the projected approximation by the cavity. Damping blends old and proposed site natural parameters. The example uses an analytic probit tilted distribution.

Assumptions

The posterior must factor into manageable terms, cavity and tilted distributions must be well-defined, and required tilted moments must be evaluable or accurately approximated. Convergence is not guaranteed for arbitrary models; invalid cavity variances and unstable sites require careful handling.

Formula / computational definition

q(θ)∝p0(θ)∏if~i(θ),q−i∝q/f~i,p^i∝q−ifi,f~inew∝Proj⁡(p^i)/q−i.q(\theta)\propto p_0(\theta)\prod_i\tilde f_i(\theta),\quad q_{-i}\propto q/\tilde f_i,\quad \hat p_i\propto q_{-i}f_i,\quad \tilde f_i^{new}\propto \operatorname{Proj}(\hat p_i)/q_{-i}.

Parameters and notation

Estimated site natural parameters, such as Gaussian precision and precision-weighted mean, define q. They are distinct from fixed model parameters and priors. Hyperparameters include approximation family, update order, damping fraction, quadrature precision, and stopping criteria. Individual Gaussian sites need not themselves be normalized densities.

Fitting, fine-tuning, and adaptation

Refit sites when data, likelihood factors, or prior change. Warm-starting existing sites can help, but cavity validity must be rechecked. If neural embeddings define the factors, freezing them makes inference conditional on that representation; changing the embeddings generally requires another EP solve.

How to use and calibrate

Monitor changes in site parameters and moments, check covariance definiteness, and compare against quadrature or MCMC on reduced cases. Plot predictive probabilities and sensitivity to damping. If evidence is used, compute the complete EP normalization approximation, not just a product of site constants.

Example application

Approximate a Gaussian latent-variable classifier with probit likelihood factors while preserving a tractable multivariate Gaussian posterior representation.

Interpretation / pitfalls

Watch out

Moment matching minimizes a local forward-KL projection; it is not global minimization of the same reverse KL used by ordinary mean-field VI. Damping can improve stability without proving convergence. Clipping every negative site precision can change the intended algorithm.

Python and R code examples

Scope: One damped Gaussian-site update for a N(0,1) cavity multiplied by Phi(theta); the tilted moments are analytic. No iterative multi-site EP solver.

Implementation route: Both implementations use native scalar arithmetic. A full EP solver in either language needs model-specific cavity, moment, damping, and normalization routines; no general neural EP package is assumed.

Dependencies and validation: Python — executed successfully on the supplied synthetic inputs; imports identify NumPy/SciPy dependencies. R — base R, reviewed but not executed because Rscript is unavailable. Random-number streams differ between languages; matching seed integers do not imply matching draws.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — One damped Gaussian-site update for a N(0,1) cavity multiplied by Phi(theta); the tilted moments are analytic. No iterative multi-site EP solver.

import math
# phi(theta)*Phi(theta), normalized by 1/2: mean=1/sqrt(pi), variance=1-1/pi.
cavity_precision=1.; cavity_eta=0.; m=1/math.sqrt(math.pi); v=1-1/math.pi
proposed_precision=1/v-cavity_precision; proposed_eta=m/v-cavity_eta
damping=.5; site_precision=damping*proposed_precision; site_eta=damping*proposed_eta
posterior_precision=cavity_precision+site_precision
print('site natural parameters',site_precision,site_eta)
print('damped q mean and variance',site_eta/posterior_precision,1/posterior_precision)
R

Component demo, not a trained model — One damped Gaussian-site update for a N(0,1) cavity multiplied by Phi(theta); the tilted moments are analytic. No iterative multi-site EP solver.

cavity.precision <- 1; cavity.eta <- 0; m <- 1/sqrt(pi); v <- 1-1/pi
proposed.precision <- 1/v-cavity.precision; proposed.eta <- m/v-cavity.eta
damping <- .5; site.precision <- damping*proposed.precision; site.eta <- damping*proposed.eta
posterior.precision <- cavity.precision+site.precision
print(c(site.precision=site.precision,site.eta=site.eta))
print(c(q.mean=site.eta/posterior.precision,q.variance=1/posterior.precision))

Sources

From the CodeSplash reference, entry INF006.