Nested Sampling¶
A fit needs both the posterior and the evidence. Nested sampling computes the evidence directly, and returns the posterior as a by-product (Skilling 2004); it is the method of the default sampler of BayMorph, Nautilus.
The evidence as a one-dimensional integral¶
Call \(X(\lambda)\) the fraction of the prior volume where the likelihood exceeds \(\lambda\): \(X = 1\) for the lowest likelihood and \(X \to 0\) at its maximum. The evidence, an integral over all the parameters, becomes an integral over that single variable:
Nested sampling estimates \(L(X)\) by climbing the likelihood while keeping track of how much prior volume is left.
The algorithm¶
- Draw \(n\) live points from the prior.
- Remove the live point of lowest likelihood, \(L_i\), and replace it with a new point drawn from the prior with a higher likelihood.
- Each step shrinks the prior volume enclosed by the live points by a factor of about \(e^{-1/n}\), so after \(i\) steps \(\ln X_i \approx -i/n\).
- Repeat until the live points can no longer change the evidence appreciably.
The evidence and the posterior follow from the discarded points:
where \(p_i\) is the posterior weight of the \(i\)-th discarded point. Because the shrinkage of each step is random, \(\ln Z\) has an uncertainty of about \(\sqrt{H/n}\), where \(H = \sum_i p_i \ln(L_i/Z)\) measures how much the image narrows the prior. More live points make the evidence more precise and explore several posterior modes more reliably, at a higher cost.

A run with 300 live points on a circular Gaussian likelihood of width 0.5 in a uniform prior of side 10. Top: the discarded points, the later the stronger their colour, close in on the peak, while the likelihood rises as the prior volume shrinks. Bottom left: the posterior mass lies where the likelihood is high but the volume not yet negligible. Bottom right: the running evidence reaches the exact value within its uncertainty, shaded. The figure uses an ideal sampler written for it; real samplers have to construct the region of higher likelihood.
The hard part is step 2: drawing a new point from the prior inside the region of higher likelihood, which in many dimensions is small and of irregular shape. The samplers differ in how they do it.
The samplers of BayMorph¶
| Sampler | How it draws new points |
|---|---|
| Nautilus (default) | Importance nested sampling: neural networks learn the shape of the likelihood contours and propose points inside them, and every likelihood evaluation contributes to the posterior and the evidence (Lange 2023). |
| Dynesty | Bounding ellipsoids and random walks or slices within them (Speagle 2020); in dynamic mode, it adds live points where the posterior mass lies (Higson et al. 2019). |
| pocoMC | Not nested sampling but preconditioned Monte Carlo: a sequence of distributions from the prior to the posterior, sampled by sequential Monte Carlo after a normalizing flow has removed the correlations between the parameters; it also estimates the evidence (Karamanis et al. 2022). |
All three return weighted posterior samples and an evidence, which BayMorph stores in the same result object. The choice of sampler changes how the posterior is computed, never the scientific problem.
In practice¶
- The weights are the result. Summaries, intervals and evidences are computed from the weighted samples; the equal-weight samples BayMorph derives from them are for plots.
- Live points and stopping. For Nautilus,
n_livesets the number of live points, 2000 by default, andn_effthe effective posterior sample size at which the run stops, 10000 by default. A quick look with fewer live points is fine to check a configuration; a result to publish deserves the defaults or more. - The uncertainty of the evidence is reported as
posterior.logz_error. A difference of log-evidences much smaller than that uncertainty says nothing. - Several modes. A posterior with separate modes, such as the two
exchangeable components of
double_sersic, needs enough live points to find all of them. Repeating a run with anotherseedis the simplest check that a result is stable. - Interrupted runs resume from a checkpoint with
fit.sampler.init.filepath: autofor Nautilus; see The Runner.
References¶
- Higson E. et al., 2019, Statistics and Computing, 29, 891: dynamic nested sampling.
- Karamanis M. et al., 2022, MNRAS, 516, 1644: preconditioned Monte Carlo and pocoMC.
- Lange J. U., 2023, MNRAS, 525, 3181: importance nested sampling with neural networks and Nautilus.
- Skilling J., 2004, AIP Conf. Proc., 735, 395: nested sampling.
- Speagle J. S., 2020, MNRAS, 493, 3132: Dynesty.