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 all 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.8.3
: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.3'
__version__ = '2.9.0'
2 changes: 1 addition & 1 deletion anesthetic/read/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def read_csv(filename, *args, **kwargs):
"""Read a CSV file into a :class:`Samples` object."""
"""Read a CSV file into a :class:`anesthetic.samples.Samples` object."""
filename = Path(filename)
kwargs['label'] = kwargs.get('label', filename.stem)
wldf = wl_read_csv(filename.with_suffix('.csv'))
Expand Down
21 changes: 21 additions & 0 deletions anesthetic/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from anesthetic.plot import (make_1d_axes, make_2d_axes,
AxesSeries, AxesDataFrame)
from anesthetic.utils import adjust_docstrings
from anesthetic import termination


class Samples(WeightedLabelledDataFrame):
Expand Down Expand Up @@ -1193,6 +1194,26 @@ def truncate(self, logL=None):
index = np.concatenate([dead_points.index, live_points.index])
return self.loc[index].recompute()

def terminated(self, criterion='logZ', *args, **kwargs):
"""Check if a simulated run has terminated.

Parameters
----------
criterion : str, optional
The termination criterion to choose.
This should be one of
{'logZ', 'D_KL', 'logX', 'ndead', 'logL'}.
Default is logZ.
See the documentation for the specific criterion in
``anesthetic.termination`` for more details.

The remaining arguments are passed to the termination criterion.
"""
try:
return termination.__dict__[criterion](self, *args, **kwargs)
except KeyError:
raise KeyError("Unknown termination criterion: %s" % criterion)

def posterior_points(self, beta=1):
"""Get equally weighted posterior points at temperature beta."""
return self.set_beta(beta).compress('equal')
Expand Down
41 changes: 41 additions & 0 deletions anesthetic/termination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Nested sampling termination criteria."""
from scipy.special import logsumexp
import numpy as np


def logZ(samples, eps=1e-3, nsamples=None, beta=None):
"""Terminate when evidence in the live points is fraction eps of total."""
i_live = samples.live_points().index
logw = samples.logw(nsamples, beta)
logZ_live = logsumexp(logw[i_live])
logZ = logsumexp(logw)
return logZ_live - logZ < np.log(eps)


def D_KL(samples, eps=1e-3, nsamples=None, beta=None):
"""Terminate when D_KL in the live points is fraction eps of total."""
i_live = samples.live_points().index
logw = samples.logw(nsamples, beta)
logZ = logsumexp(logw)
betalogL = samples._betalogL(beta)
S = (logw*0).add(betalogL, axis=0) - logZ
w = np.exp(logw-logZ)
D_KL_live = (S*w).loc[i_live].sum()
D_KL = (S*w).sum()
return D_KL_live / D_KL < eps


def logX(samples, max_logX, nsamples=None):
"""Terminate when the log-volume of the live points reaches a threshold."""
i_live = samples.live_points().index
return samples.logX(nsamples)[i_live[0]] < max_logX


def ndead(samples, max_ndead):
"""Terminate if the number of dead points exceeds a maximum."""
return len(samples.dead_points()) > max_ndead


def logL(samples, max_logL):
"""Terminate if the lowest live likelihood exceeds a threshold."""
return samples.contour() > max_logL
9 changes: 9 additions & 0 deletions docs/source/anesthetic.read.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ anesthetic.read.cobaya module
:show-inheritance:


anesthetic.read.csv module
--------------------------

.. automodule:: anesthetic.read.csv
:members:
:undoc-members:
:show-inheritance:


anesthetic.read.getdist module
------------------------------

Expand Down
9 changes: 9 additions & 0 deletions docs/source/anesthetic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ anesthetic.scripts module
:show-inheritance:


anesthetic.termination module
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. automodule:: anesthetic.termination
:members:
:undoc-members:
:show-inheritance:


anesthetic.testing module
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
18 changes: 18 additions & 0 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,24 @@ def test_truncate(cut):
assert_array_equal(pc, truncated_run)


@pytest.mark.parametrize("criterion,args,terminated",
[("logZ", (1e-3,), False),
("logZ", (1e-1,), True),
("D_KL", (1e-1,), True),
("D_KL", (1e-3,), False),
("logX", (-10.0), True),
("logX", (-15.0), False),
("ndead", (1000), True),
("ndead", (2000), False),
("logL", (5.0,), True),
("logL", (6.0,), False),
])
def test_terminated(criterion, args, terminated):
np.random.seed(4)
pc = read_chains("./tests/example_data/pc")
assert pc.terminated(criterion, *args) is terminated


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