# Nested sampling algorithm

{{ safesubst:#invoke:Unsubst||$N=Context |date=__DATE__ |$B= {{#invoke:Message box|ambox}} }}

The nested sampling algorithm is a computational approach to the problem of comparing models in Bayesian statistics, developed in 2004 by physicist John Skilling.[1]

## Background

Bayes' theorem can be applied to a pair of competing models ${\displaystyle M1}$ and ${\displaystyle M2}$ for data ${\displaystyle D}$, one of which may be true (though which one is not known) but which both cannot simultaneously be true, as follows:

{\displaystyle {\begin{aligned}P(M1|D)&{}={\frac {P(D|M1)P(M1)}{P(D)}}\\&{}={\frac {P(D|M1)P(M1)}{P(D|M1)P(M1)+P(D|M2)P(M2)}}\\&{}={\frac {1}{1+{\frac {P(D|M2)}{P(D|M1)}}{\frac {P(M2)}{P(M1)}}}}\end{aligned}}}

Given no a priori information in favor of ${\displaystyle M1}$ or ${\displaystyle M2}$, it is reasonable to assign prior probabilities ${\displaystyle P(M1)=P(M2)=1/2}$, so that ${\displaystyle P(M2)/P(M1)=1}$. The remaining ratio ${\displaystyle P(D|M2)/P(D|M1)}$ is not so easy to evaluate since in general it requires marginalization of nuisance parameters. Generally, ${\displaystyle M1}$ has a collection of parameters that can be lumped together and called ${\displaystyle \theta }$, and ${\displaystyle M2}$ has its own vector of parameters that may be of different dimensionality but is still referred to as ${\displaystyle \theta }$. The marginalization for ${\displaystyle M1}$ is

${\displaystyle P(D|M1)=\int d\theta P(D|\theta ,M1)P(\theta |M1)}$

and likewise for ${\displaystyle M2}$. 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 ${\displaystyle P(\theta |D,M1)}$.[2] It is an alternative to methods from the Bayesian literature[3] 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 ${\displaystyle Z=P(D|M)}$ where ${\displaystyle M}$ is ${\displaystyle M1}$ or ${\displaystyle M2}$:

  Start with ${\displaystyle N}$ points ${\displaystyle \theta _{1},...,\theta _{N}}$ sampled from prior.
for ${\displaystyle i=1}$ to ${\displaystyle j}$ do        % The number of iterations j is chosen by guesswork.
${\displaystyle L_{i}:=\min(}$current likelihood values of the points${\displaystyle )}$;
${\displaystyle X_{i}:=\exp(-i/N);}$
${\displaystyle w_{i}:=X_{i-1}-X_{i}}$
${\displaystyle Z:=Z+L_{i}*w_{i};}$
Save the point with least likelihood as a sample point with weight ${\displaystyle w_{i}}$.
Update the point with least likelihood with some Markov Chain
Monte Carlo steps according to the prior, accepting only steps that
keep the likelihood above ${\displaystyle L_{i}}$.
end
return ${\displaystyle Z}$;


At each iteration, ${\displaystyle 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 ${\displaystyle \theta _{i}}$. The weight factor ${\displaystyle w_{i}}$ is an estimate of the amount of prior mass that lies between two nested hypersurfaces ${\displaystyle \{\theta |P(D|\theta ,M)=P(D|\theta _{i-1},M)\}}$ and ${\displaystyle \{\theta |P(D|\theta ,M)=P(D|\theta _{i},M)\}}$. The update step ${\displaystyle Z:=Z+L_{i}*w_{i}}$ computes the sum over ${\displaystyle i}$ of ${\displaystyle L_{i}*w_{i}}$ to numerically approximate the integral

${\displaystyle {\begin{array}{lcl}P(D|M)&=&\int P(D|\theta ,M)P(\theta |M)d\theta \\&=&\int P(D|\theta ,M)dP(\theta |M)\\\end{array}}}$

The idea is to chop up the range of ${\displaystyle f(\theta )=P(D|\theta ,M)}$ and estimate, for each interval ${\displaystyle [f(\theta _{i-1}),f(\theta _{i})]}$, how likely it is a priori that a randomly chosen ${\displaystyle \theta }$ would map to this interval. This can be thought of as a Bayesian's way to numerically implement Lebesgue integration.

## Applications

Since nested sampling was proposed in 2004, it has been used in multiple settings within 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."[4] A refinement of the nested sampling algorithm to handle multimodal posteriors has also been suggested as a means of detecting astronomical objects in existing datasets.[5]