# Nested sampling algorithm

The nested sampling algorithm is a computational approach to the Bayesian statistics problems of comparing models and generating samples from posterior distributions. It was developed in 2004 by physicist John Skilling.

## Background

Bayes' theorem can be applied to a pair of competing models $M_{1}$ and $M_{2}$ for data $D$, one of which may be true (though which one is unknown) but which both cannot be true simultaneously. The posterior probability for $M_{1}$ may be calculated as:
    ${\begin{aligned}P(M_{1}\mid D)&={\frac {P(D\mid M_{1})P(M_{1})}{P(D)}}\\&={\frac {P(D\mid M_{1})P(M_{1})}{P(D\mid M_{1})P(M_{1})+P(D\mid M_{2})P(M_{2})}}\\&={\frac {1}{1+{\frac {P(D\mid M_{2})}{P(D\mid M_{1})}}{\frac {P(M_{2})}{P(M_{1})}}}}\end{aligned}}$
The prior probabilities $M_{1}$ and $M_{2}$ are already known, as they are chosen by the researcher ahead of time. However, the remaining Bayes factor $P(D\mid M_{2})/P(D\mid M_{1})$ is not so easy to evaluate, since in general it requires marginalizing nuisance parameters. Generally, $M_{1}$ has a set of parameters that can be grouped together and called $\theta$, and $M_{2}$ has its own vector of parameters that may be of different dimensionality, but is still termed $\theta$. The marginalization for $M_{1}$ is
    $P(D\mid M_{1})=\int d\theta \,P(D\mid \theta ,M_{1})P(\theta \mid M_{1})$
and likewise for $M_{2}$. This integral is often analytically intractable, and in these cases it is necessary to employ a numerical algorithm to find an approximation. The nested sampling algorithm was developed by John Skilling specifically to approximate these marginalization integrals, and it has the added benefit of generating samples from the posterior distribution $P(\theta \mid D,M_{1})$. It is an alternative to methods from the Bayesian literature such as bridge sampling and defensive importance sampling.
Here is a simple version of the nested sampling algorithm, followed by a description of how it computes the marginal probability density $Z=P(D\mid M)$ where $M$ is $M_{1}$ or $M_{2}$:
At each iteration, $X_{i}$ is an estimate of the amount of prior mass covered by the hypervolume in parameter space of all points with likelihood greater than $\theta _{i}$. The weight factor $w_{i}$ is an estimate of the amount of prior mass that lies between two nested hypersurfaces $\{\theta \mid P(D\mid \theta ,M)=P(D\mid \theta _{i-1},M)\}$ and $\{\theta \mid P(D\mid \theta ,M)=P(D\mid \theta _{i},M)\}$. The update step $Z:=Z+L_{i}w_{i}$ computes the sum over $i$ of $L_{i}w_{i}$ to numerically approximate the integral
    ${\begin{aligned}P(D\mid M)&=\int P(D\mid \theta ,M)P(\theta \mid M)\,d\theta \\&=\int P(D\mid \theta ,M)\,dP(\theta \mid M)\end{aligned}}$
In the limit $j\to \infty$, this estimator has a positive bias of order $1/N$ which can be removed by using $(1-1/N)$ instead of the $\exp(-1/N)$ in the above algorithm.
The idea is to subdivide the range of $f(\theta )=P(D\mid \theta ,M)$ and estimate, for each interval $[f(\theta _{i-1}),f(\theta _{i})]$, how likely it is a priori that a randomly chosen $\theta$ would map to this interval. This can be thought of as a Bayesian's way to numerically implement Lebesgue integration.

## Choice of MCMC algorithm

The original procedure outlined by Skilling (given above in pseudocode) does not specify what specific Markov chain Monte Carlo algorithm should be used to choose new points with better likelihood.
Skilling's own code examples (such as one in Sivia and Skilling (2006), available on Skilling's website) chooses a random existing point and selects a nearby point chosen by a random distance from the existing point; if the likelihood is better, then the point is accepted, else it is rejected and the process repeated. Mukherjee et al. (2006) found higher acceptance rates by selecting points randomly within an ellipsoid drawn around the existing points; this idea was refined into the MultiNest algorithm which handles multimodal posteriors better by grouping points into likelihood contours and drawing an ellipsoid for each contour.

## Implementations

Example implementations demonstrating the nested sampling algorithm are publicly available for download, written in several programming languages.
* Simple examples in C, R, or Python are on John Skilling's website.
* A Haskell port of the above simple codes is on Hackage.
* An example in R originally designed for fitting spectra is described on Bojan Nikolic's website and is available on GitHub.
* A NestedSampler is part of the Python toolbox BayesicFitting for generic model fitting and evidence calculation. It is available on GitHub.
* An implementation in C++, named DIAMONDS, is on GitHub.
* A highly modular Python parallel example for statistical physics and condensed matter physics uses is on GitHub.
* pymatnest is a package designed for exploring the energy landscape of different materials, calculating thermodynamic variables at arbitrary temperatures and locating phase transitions is on GitHub
* The MultiNest software package is capable of performing nested sampling on multi-modal posterior distributions. It has interfaces for C++, Fortran and Python inputs, and is available on GitHub.
* PolyChord is another nested sampling software package available on GitHub. PolyChord's computational efficiency scales better with an increase in the number of parameters than MultiNest, meaning PolyChord can be more efficient for high dimensional problems. It has interfaces to likelihood functions written in Python, Fortran, C, or C++.
* NestedSamplers.jl, a Julia package for implementing single- and multi-ellipsoidal nested sampling algorithms is on GitHub.
* Korali is a high-performance framework for uncertainty quantification, optimization, and deep reinforcement learning, which also implements nested sampling.

## Applications

Since nested sampling was proposed in 2004, it has been used in many aspects of the field of astronomy. One paper suggested using nested sampling for cosmological model selection and object detection, as it "uniquely combines accuracy, general applicability and computational feasibility." A refinement of the algorithm to handle multimodal posteriors has been suggested as a means to detect astronomical objects in extant datasets. Other applications of nested sampling are in the field of finite element updating where the algorithm is used to choose an optimal finite element model, and this was applied to structural dynamics. This sampling method has also been used in the field of materials modeling. It can be used to learn the partition function from statistical mechanics and derive thermodynamic properties.

## Dynamic nested sampling

Dynamic nested sampling is a generalisation of the nested sampling algorithm in which the number of samples taken in different regions of the parameter space is dynamically adjusted to maximise calculation accuracy. This can lead to large improvements in accuracy and computational efficiency when compared to the original nested sampling algorithm, in which the allocation of samples cannot be changed and often many samples are taken in regions which have little effect on calculation accuracy.
Publicly available dynamic nested sampling software packages include:
* dynesty - a Python implementation of dynamic nested sampling which can be downloaded from GitHub.
* dyPolyChord: a software package which can be used with Python, C++ and Fortran likelihood and prior distributions. dyPolyChord is available on GitHub.
Dynamic nested sampling has been applied to a variety of scientific problems, including analysis of gravitational waves, mapping distances in space and exoplanet detection.