Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perfect Nested Sampling: Gaussian Likelihood and Gaussian Priors #371

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
anesthetic: nested sampling post-processing
===========================================
:Authors: Will Handley and Lukas Hergt
:Version: 2.8.1
:Version: 2.9.0
:Homepage: https://github.com/handley-lab/anesthetic
:Documentation: http://anesthetic.readthedocs.io/

Expand Down
2 changes: 1 addition & 1 deletion anesthetic/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.8.1'
__version__ = '2.9.0'
94 changes: 90 additions & 4 deletions anesthetic/examples/perfect_ns.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,92 @@
"""Perfect nested sampling generators."""
import numpy as np
from scipy.stats import multivariate_normal
from anesthetic.examples.utils import random_ellipsoid
from anesthetic import NestedSamples
from anesthetic.samples import merge_nested_samples
from scipy.optimize import minimize, NonlinearConstraint


def new_gaussian(nlive, mean, cov, logLmax, mu, Sigma,
*args, **kwargs):
"""Perfect nested sampling run for a correlated gaussian likelihood.

This produces a perfect nested sampling for a likelihood with a gaussian
profile with a gaussian prior.

Parameters
----------
nlive : int
minimum number of live points across the run

mean : 1d array-like
mean of gaussian in parameters.

cov : 2d array-like
covariance of gaussian in parameters

logLmax : float
maximum loglikelihood

mu : 1d array-like
mean of gaussian prior in parameters.

Sigma : 2d array-like
covariance of gaussian prior in parameters

The remaining arguments are passed to the
:class:`anesthetic.samples.NestedSamples` constructor.

Returns
-------
samples : :class:`anesthetic.samples.NestedSamples`
Nested sampling run
"""

def logLike(x):
dist = multivariate_normal(mean, cov)
return logLmax + dist.logpdf(x) - dist.logpdf(mean)

Check warning on line 48 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L46-L48

Added lines #L46 - L48 were not covered by tests

prior = multivariate_normal(mu, Sigma)
points = prior.rvs(2*nlive)

Check warning on line 51 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L50-L51

Added lines #L50 - L51 were not covered by tests

samples = NestedSamples(points, logL=logLike(points), logL_birth=-np.inf,

Check warning on line 53 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L53

Added line #L53 was not covered by tests
*args, **kwargs)

invcov = np.linalg.inv(cov)
invSigma = np.linalg.inv(Sigma)

Check warning on line 57 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L56-L57

Added lines #L56 - L57 were not covered by tests

def maximise_prior(x, logLs):

Check warning on line 59 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L59

Added line #L59 was not covered by tests
"""Maximise the prior subject to the likelihood constraint."""
constraints = NonlinearConstraint(logLike, logLs, np.inf,

Check warning on line 61 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L61

Added line #L61 was not covered by tests
jac=lambda x: 2 * invcov @ (x-mean))
sol = minimize(lambda x: -prior.logpdf(x), x,

Check warning on line 63 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L63

Added line #L63 was not covered by tests
jac=lambda x: 2 * invSigma @ (x-mu),
constraints=constraints)
return prior.logpdf(sol.x) if sol.success else prior.logpdf(prior.mean)

Check warning on line 66 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L66

Added line #L66 was not covered by tests

while -samples.logX().iloc[-nlive] < samples.D_KL()*2:
logLs = samples.logL.iloc[-nlive]

Check warning on line 69 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L68-L69

Added lines #L68 - L69 were not covered by tests

# prior round
points = prior.rvs(nlive)
logL = logLike(points)
i = logL > logLs
samps_1 = NestedSamples(points[i], logL=logL[i], logL_birth=logLs,

Check warning on line 75 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L72-L75

Added lines #L72 - L75 were not covered by tests
*args, **kwargs)

# Ellipsoidal round
points = random_ellipsoid(mean, cov*2*(logLmax - logLs), nlive)
logL = logLike(points)
logpi = prior.logpdf(points)
logpimax = maximise_prior(points[np.argmax(logpi)], logLs)
i = logpi > logpimax + np.log(np.random.rand(nlive))
samps_2 = NestedSamples(points[i], logL=logL[i], logL_birth=logLs,

Check warning on line 84 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L79-L84

Added lines #L79 - L84 were not covered by tests
*args, **kwargs)

samples = merge_nested_samples([samples, samps_1, samps_2])

Check warning on line 87 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L87

Added line #L87 was not covered by tests

return samples

Check warning on line 89 in anesthetic/examples/perfect_ns.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/examples/perfect_ns.py#L89

Added line #L89 was not covered by tests


def gaussian(nlive, ndims, sigma=0.1, R=1, logLmin=-1e-2, logLmax=0,
Expand Down Expand Up @@ -43,7 +127,8 @@
"""

def logLike(x):
return logLmax - (x**2).sum(axis=-1)/2/sigma**2
dist = multivariate_normal(np.zeros(ndims), sigma**2*np.eye(ndims))
return logLmax + dist.logpdf(x) - dist.logpdf(dist.mean)

def random_sphere(n):
return random_ellipsoid(np.zeros(ndims), np.eye(ndims), n)
Expand Down Expand Up @@ -106,10 +191,10 @@
"""
mean = np.array(mean, dtype=float)
cov = np.array(cov, dtype=float)
invcov = np.linalg.inv(cov)

def logLike(x):
return logLmax - 0.5 * ((x - mean) @ invcov * (x - mean)).sum(axis=-1)
dist = multivariate_normal(mean, cov)
return logLmax + dist.logpdf(x) - dist.logpdf(dist.mean)

ndims = len(mean)

Expand All @@ -122,7 +207,7 @@
samples = NestedSamples(points, logL=logLike(points), logL_birth=-np.inf,
*args, **kwargs)

while (1/samples.nlive.iloc[:-nlive]).sum() < samples.D_KL()*2:
while -samples.logX().iloc[-nlive] < samples.D_KL()*2:
logLs = samples.logL.iloc[-nlive]

sig = np.diag(cov*2*(logLmax - logLs))**0.5
Expand All @@ -142,6 +227,7 @@
i = ((points > bounds.T[0]) & (points < bounds.T[1])).all(axis=1)
samps_2 = NestedSamples(points[i], logL=logL[i], logL_birth=logLs,
*args, **kwargs)

samples = merge_nested_samples([samples, samps_1, samps_2])

return samples
Expand Down
Loading