Skip to content

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

Nested sampling

Rewrites Bayesian evidence as a one-dimensional integral over remaining prior mass, estimated through increasingly restrictive likelihood contours. Posterior weighted samples arise as a by-product;

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

Rewrites Bayesian evidence as a one-dimensional integral over remaining prior mass, estimated through increasingly restrictive likelihood contours. Posterior weighted samples arise as a by-product; this is structurally different from AIS temperature bridges and ordinary posterior MCMC.

Question / hypotheses

How does likelihood increase as the prior mass allowed by a likelihood threshold shrinks, and what integral does that imply for model evidence?

Architecture / computational structure

Initialize live points from the prior, remove the lowest-likelihood point, replace it from the prior subject to a higher likelihood threshold, and accumulate evidence contributions. Track remaining evidence and shrinkage uncertainty. Dynamic nested sampling reallocates effort, requiring its own weight/bookkeeping rules.

Assumptions

The prior must be proper and sampleable, likelihoods must have the required normalization for evidence comparison, and constrained-prior sampling must be accurate. Standard shrinkage results presume an appropriately sampled live-point population; missed modes or correlated replacement samples can invalidate simple uncertainty calculations.

Formula / computational definition

X(λ)=∫1{L(θ)>λ}π(θ)dθ,Z=∫01L(X)dX≈∑iLi(Xi−1−Xi),Xi/Xi−1∼Beta(Nlive,1).X(\lambda)=\int\mathbf1\{L(\theta)>\lambda\}\pi(\theta)d\theta,\quad Z=\int_0^1 L(X)dX\approx\sum_iL_i(X_{i-1}-X_i),\quad X_i/X_{i-1}\sim Beta(N_{live},1).

Parameters and notation

Live points, discarded points, and evidence weights are sampling state. Hyperparameters include live-point count, constrained sampler, bounding strategy, termination tolerance, and allocation of additional samples. E[log X_i] is approximately -i/N_live in the basic fixed-live-point scheme; this is not E[X_i].

Fitting, fine-tuning, and adaptation

Refit when priors, likelihood normalization, or observations change. Pilot runs can identify inadequate live-point counts or constrained proposals; do not combine incompatible runs by simply concatenating posterior points. Neural likelihood surrogates alter the target evidence and need validation against the original simulator or likelihood.

How to use and calibrate

Compare evidence across independent runs, inspect mode discovery and weighted posterior summaries, and validate on analytic examples. Plot likelihood versus log prior mass and accumulated log evidence. Report numerical uncertainty separately from scientific sensitivity to the prior.

Example application

Compare probabilistic models in a multimodal scientific inverse problem where evidence and posterior uncertainty are both required, provided constrained-prior exploration is practical.

Interpretation / pitfalls

Watch out

Evidence is strongly prior-dependent and is not a p-value. A deterministic mean-log-shrinkage quadrature does not implement live-point sampling or quantify evidence uncertainty. An apparently converged integral can omit an undiscovered isolated mode.

Python and R code examples

Scope: Quadrature for the known curve L(X)=1-X using a deterministic illustrative shrinkage grid, including a remaining-mass bound; not nested-sampling simulation.

Implementation route: The numerical component is native NumPy/base R. Python dynesty and R ernest provide actual nested-sampling workflows; constrained sampling settings and their diagnostic requirements remain essential.

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 — Quadrature for the known curve L(X)=1-X using a deterministic illustrative shrinkage grid, including a remaining-mass bound; not nested-sampling simulation. · needs numpy

import numpy as np
nlive=20; X=np.exp(-np.arange(201)/nlive); likelihood=1-X
dead=np.sum(.5*(likelihood[:-1]+likelihood[1:])*(X[:-1]-X[1:]))
remaining_upper=X[-1]  # L<=1
print('quadrature lower/upper bounds',dead,dead+remaining_upper,'exact evidence',.5)
assert dead<=.5<=dead+remaining_upper
R

Component demo, not a trained model — Quadrature for the known curve L(X)=1-X using a deterministic illustrative shrinkage grid, including a remaining-mass bound; not nested-sampling simulation.

nlive <- 20; X <- exp(-(0:200)/nlive); likelihood <- 1-X
dead <- sum(.5*(head(likelihood,-1)+tail(likelihood,-1))*(head(X,-1)-tail(X,-1)))
remaining.upper <- tail(X,1)
print(c(lower=dead,upper=dead+remaining.upper,exact=.5))
stopifnot(dead<=.5,.5<=dead+remaining.upper)

Sources

From the CodeSplash reference, entry INF019.