**nested**¶

Nested sampling.

Warning

This module is much less tested than the rest of the package. Also the documentation does not exactly explain how nested sampling works (and this topic is not covered in our book). Thus, refer to e.g. the original papers of Skilling or Chopin and Robert (2010, Biometrika).

## Overview¶

This module contains classes that implement nested sampling:

`NestedSampling`

: base class;`Nested_RWmoves`

: nested sampling algorithm based on random walk Metropolis steps.

To use the latter, you need to define first a static model, in the same way as in the smc_samplers module. For instance:

```
from particles import smc_samplers as ssp
from particles import distributions as dists
class ToyModel(ssp.smc_samplers):
def logpyt(self, theta, t): # log-likelihood of data-point at time t
return stats.norm.logpdf(self.data[t], loc=theta)
my_prior = dists.Normal() # theta ~ N(0, 1)
y = random.normal(size=1) # y | theta ~ N(theta, 1)
toy_model = ToyModel(data=y, prior=my_prior)
```

Then, the algorithm may be set and run as follows:

```
from particles import nested
algo = nested.Nested_RWmoves(model=toy_model, N=1000, nsteps=3, eps=1e-8)
algo.run()
print('estimate of log-evidence: %f' % algo.lZhats[-1])
```

This will run a nested sampling algorithm which propagates N=1000 particles;
each time a point is deleted, it is replaced by another point obtained as
follows: another point is selected at random, and then moved through `nsteps`

steps of a Gaussian random walk Metropolis kernel (which leaves invariant the
prior constrained to the current likelihood contour). To make these steps
reasonably efficient, the covariance matrix of the random walk proposal is
dynamically adapted to the current sample of points. The algorithm is stopped
when the different between the two most recent estimates of the log-evidence
is below `eps`

.

To implement your own algorithm, you must sub-class `NestedSampling`

like
this:

```
from particles import nested
class MyNestedSampler(nested.NestedSampling):
def mutate(self, n, m):
# implement a MCMC step that replace point X[n] with the point
# obtained by starting at X[m] and doing a certain number of steps
return value
```

## Module summary¶

`NestedSampling` |
Base class for nested sampling algorithms. |

`Nested_RWmoves` |
Nested sampling with (adaptive) random walk Metropolis moves. |