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

Emulated termination criteria #363

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
387ff78
add pc term criteria
yallup Feb 29, 2024
8fd2ac1
cover no dead points
yallup Feb 29, 2024
ad1d358
sign change
yallup Feb 29, 2024
01ce2a9
Added read_csv for weighted pandas
williamjameshandley Mar 1, 2024
365b85c
Added labelled pandas testing
williamjameshandley Mar 1, 2024
9708866
Added weighted_labelled_pandas read_csv
williamjameshandley Mar 1, 2024
17505fa
Added read_csv for NestedSamples
williamjameshandley Mar 1, 2024
4ee2b73
Added read_csv to anesthetic
williamjameshandley Mar 1, 2024
6fc79b5
Updated pydocstyle
williamjameshandley Mar 1, 2024
4016ccf
bump version to 2.7.1
williamjameshandley Mar 1, 2024
43fa01d
bump version to 2.8.0
williamjameshandley Mar 1, 2024
2888a4f
updated documentation
williamjameshandley Mar 1, 2024
f9d3e68
Removed inheritance from documentation
williamjameshandley Mar 2, 2024
41ada6c
Merge branch 'master' into read_csv
williamjameshandley Mar 2, 2024
77e6cd9
Merge branch 'master' into read_csv
williamjameshandley Mar 2, 2024
97e248e
Merge branch 'master' into termination
williamjameshandley Mar 2, 2024
849d629
bump version to 2.8.0
williamjameshandley Mar 2, 2024
0ec4a2c
Merge branch 'master' into read_csv
williamjameshandley Mar 2, 2024
999526f
remove formatting
yallup Mar 4, 2024
51097bd
Merge branch 'termination' of github.com:handley-lab/anesthetic into …
yallup Mar 4, 2024
75d8628
remove formatting
yallup Mar 4, 2024
c662252
hallucinated ref
yallup Mar 4, 2024
8a1117f
Merge branch 'read_csv' into termination
yallup Mar 4, 2024
5ef5cd9
Merge branch 'master' into termination
williamjameshandley Mar 4, 2024
0fec842
Updated to include chain reading
williamjameshandley Mar 4, 2024
fdb5b24
Merge remote-tracking branch 'origin/read_csv' into termination
yallup Mar 4, 2024
8dc9b87
Merge branch 'termination' of github.com:handley-lab/anesthetic into …
yallup Mar 5, 2024
e414992
Merge branch 'master' into termination
yallup Mar 5, 2024
a34b3d6
use inbuilts
yallup Mar 6, 2024
26c0603
Merge branch 'master' into termination
yallup Mar 6, 2024
4a5141c
split out criteria
yallup Mar 7, 2024
bf4c30c
Merge branch 'termination' of github.com:handley-lab/anesthetic into …
yallup Mar 7, 2024
a878699
Suggested refactor
williamjameshandley Mar 18, 2024
68a3043
Updated documentation
williamjameshandley Mar 18, 2024
9a70d46
Merge branch 'master' into termination
williamjameshandley Mar 18, 2024
b1cda3a
bump version to 2.9.0
williamjameshandley Mar 18, 2024
27f5ddb
Updated out-of-date docs
williamjameshandley Mar 18, 2024
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.7.3
:Version: 2.8.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.7.3'
__version__ = '2.8.0'
37 changes: 37 additions & 0 deletions anesthetic/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,43 @@ def truncate(self, logL=None):
index = np.concatenate([dead_points.index, live_points.index])
return self.loc[index].recompute()

def terminate(self, eps=1e-3, logL=None, n=None):
yallup marked this conversation as resolved.
Show resolved Hide resolved
"""Check if a set of samples has reached a termination criterion.

Uses the termination criterion of
[Handley et al. 2015](https://arxiv.org/abs/1506.00171).
yallup marked this conversation as resolved.
Show resolved Hide resolved
computes if the ratio of evidence in the live to dead points is less
than some precision.

Parameters
----------
eps : float, optional
The precision of the criteria.
default: 1e-3

logL : float or int, optional
Loglikelihood or iteration number to truncate run.
If not provided, truncate at the last set of dead points.
default: None

n : int, optional
Number of samples to draw when computing the volume estimates
default: None
yallup marked this conversation as resolved.
Show resolved Hide resolved

"""
logL = self.contour(logL)
i_live = ((self.logL >= logL) & (self.logL_birth < logL)).to_numpy()
yallup marked this conversation as resolved.
Show resolved Hide resolved
i_dead = ((self.logL < logL)).to_numpy()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In hindsight, do we need the dead points?
self.logX()[i_live[0]] + logZ_live vs self.logZ() would probably also do the trick.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I see this, if I am taking the ratio, self.logZ() is the union whereas I want the self.iloc[dead].logZ()? So I think I need dead idx? I will implement all the other suggestions and resend for comments

if np.any(i_dead):
logZ_dead = self[i_dead].recompute().logZ(n).mean()
logX_dead = self[i_dead].recompute().logX(n).iloc[-1]
else:
# logZ if no dead points
logZ_dead = -1e30
logX_dead = -1e30
yallup marked this conversation as resolved.
Show resolved Hide resolved
logZ_live = self[i_live].recompute().logZ(n).mean() + logX_dead
return logZ_live - np.logaddexp(logZ_live, logZ_dead) < np.log(eps)

def posterior_points(self, beta=1):
"""Get equally weighted posterior points at temperature beta."""
return self.set_beta(beta).compress('equal')
Expand Down
15 changes: 15 additions & 0 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,21 @@ def test_truncate(cut):
assert_array_equal(pc, truncated_run)


def test_terminate():
np.random.seed(4)
pc = read_chains("./tests/example_data/pc")

assert not pc.terminate(logL=0)

assert not pc.terminate(logL=200)
assert not pc.terminate(logL=0.0)
assert pc.terminate(logL=None)

assert pc.terminate(logL=200, eps=1.0)
assert pc.terminate(logL=0.0, eps=1.0)
assert pc.terminate(logL=None, eps=1.0)


def test_hist_range_1d():
"""Test to provide a solution to #89"""
np.random.seed(3)
Expand Down
Loading