Skip to content

Commit

Permalink
Neater code
Browse files Browse the repository at this point in the history
  • Loading branch information
williamjameshandley committed Mar 13, 2024
1 parent 88ca90e commit a45c438
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions anesthetic/examples/perfect_ns.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from scipy.optimize import minimize, NonlinearConstraint


def new_gaussian(nlive, ndims, mean, cov, logLmax, mu, Sigma,
def new_gaussian(nlive, mean, cov, logLmax, mu, Sigma,
*args, **kwargs):
"""Perfect nested sampling run for a correlated gaussian likelihood.
Expand All @@ -19,22 +19,19 @@ def new_gaussian(nlive, ndims, mean, cov, logLmax, mu, Sigma,
nlive : int
minimum number of live points across the run
ndims : int
dimensionality of the gaussian
mean : 1d array-like, shape (ndims,)
mean : 1d array-like
mean of gaussian in parameters.
cov : 2d array-like, shape (ndims, ndims)
cov : 2d array-like
covariance of gaussian in parameters
logLmax : float
maximum loglikelihood
mu : 1d array-like, shape (ndims,)
mu : 1d array-like
mean of gaussian prior in parameters.
Sigma : 2d array-like, shape (ndims, ndims)
Sigma : 2d array-like
covariance of gaussian prior in parameters
The remaining arguments are passed to the
Expand All @@ -60,12 +57,13 @@ def logLike(x):
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 sol
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
Expand All @@ -81,18 +79,12 @@ def maximise_prior(x, logLs):
points = random_ellipsoid(mean, cov*2*(logLmax - logLs), nlive)
logL = logLike(points)
logpi = prior.logpdf(points)

sol = maximise_prior(points[np.argmax(logpi)], logLs)
if sol.success:
logpimax = -sol.fun
else:
logpimax = prior.logpdf(prior.mean)

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
samples.gui()

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

Expand Down Expand Up @@ -215,7 +207,7 @@ def logLike(x):
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 @@ -235,6 +227,7 @@ def logLike(x):
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

0 comments on commit a45c438

Please sign in to comment.