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

Implemented credibility_interval() #188

Open
wants to merge 89 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
00c61e5
Implemented Samples.credibility_interval
Mar 22, 2022
8098bfd
Improve documentation
Mar 22, 2022
f6afe0a
Formatting / make flake8 happy
Mar 22, 2022
6c816eb
pydocstyle compliance
Mar 22, 2022
e3c1f21
Fix typo made while flake8-ing
Mar 22, 2022
c22e304
Merge branch 'williamjameshandley:master' into credibility-interval
Stefan-Heimersheim Jun 8, 2022
3f8f2cb
Merge branch 'master' into credibility-interval
williamjameshandley Jul 12, 2022
a39c038
That will teach me to try and use github to manually merge
williamjameshandley Jul 12, 2022
be0eb50
Move credibility_interval to utils
Jul 19, 2022
3b20f1c
Formatting
Jul 19, 2022
4a66d74
flake8 wants def rather than lambda
Jul 19, 2022
0d53bce
Remove unneeded variable
Jul 19, 2022
d6b8bb9
Remove imports no longer needed
Jul 19, 2022
da7ebaf
docstring format
Jul 19, 2022
92f24be
Updated CI checks to be in line with github requirements
williamjameshandley Jul 27, 2022
19b3e1a
Moving to full coverage
williamjameshandley Jul 27, 2022
e97526e
Added a link to the fastCL repo
williamjameshandley Jul 27, 2022
12a6bc8
Unified quantiles, cdfs and credibility-intervals
williamjameshandley Jul 27, 2022
bcf2ae1
Added some CDF tests
williamjameshandley Jul 28, 2022
095a64a
Merge branch 'master' into credibility-interval
williamjameshandley Aug 3, 2022
cf95f46
Merge branch 'master' into credibility-interval
williamjameshandley Aug 5, 2022
7f56e78
Merge branch 'master' into credibility-interval
lukashergt Aug 6, 2022
d9c4076
Merge branch 'master' into credibility-interval
lukashergt Aug 9, 2022
25e3843
Merge branch 'master' into credibility-interval
lukashergt Aug 10, 2022
693abef
Implemented compress_weights-based credibility_interval with uncertai…
Nov 3, 2022
f6e9b40
Merge remote-tracking branch 'upstream/master' into credibility-interval
Apr 6, 2023
7d634b5
Remove cdf() function and quantile changes
Apr 6, 2023
f33decf
Implement covariance and clean up code
Apr 6, 2023
0b938e1
Fix indexing
Apr 10, 2023
3d26a41
Update tests
Apr 10, 2023
e5cb4b6
flake8 compliance
Apr 10, 2023
30c70bf
Merge branch 'master' into credibility-interval
Apr 10, 2023
36c58d2
Merge branch 'master' into credibility-interval
lukashergt Apr 11, 2023
4f79a09
version bump from 2.0.0-beta.28 to 2.0.0-beta.29
lukashergt Apr 11, 2023
dccfe4b
fix docstring formatting of `utils.credibility_interval`
lukashergt Apr 12, 2023
84ed5b6
Add tests for other methods
Apr 26, 2023
d1aaf02
Added tests for compress_samples
Apr 26, 2023
2527895
Updated docstring
Apr 26, 2023
b466d1a
Removed u kw argument
Apr 26, 2023
0d59b7c
n_iter to nsamples
Apr 26, 2023
2a0d3b8
Added CDF fill_value; added tests
Apr 26, 2023
ed4607f
Typo
Apr 26, 2023
b4b78e6
Renamed methods to full names
Apr 26, 2023
9911dd2
flake8 compatibility
Apr 26, 2023
5ac5fca
Implemented Samples.credibility_interval
Apr 26, 2023
86d3836
flake8 compatibility
Apr 26, 2023
dbd1303
Implemented suggestions
May 15, 2023
7ce8b59
Improved tests with Lukas' suggestions
May 15, 2023
f016fa5
Improve tests
May 15, 2023
fb42c59
bump version number to 2.0.0b30
lukashergt May 15, 2023
5c14599
Merge branch 'master' into credibility-interval
lukashergt May 15, 2023
1997ffd
Update _version.py
lukashergt May 15, 2023
b0862e1
Merge branch 'master' into credibility-interval
lukashergt May 25, 2023
a92f822
Update _version.py
lukashergt May 27, 2023
4d16340
Update README.rst
lukashergt May 27, 2023
1e78eb7
Merge branch 'master' into credibility-interval
lukashergt Jun 7, 2023
7fdd99a
remove `verbose` kwarg which is now unused
lukashergt Jun 7, 2023
8a44f17
remove also docstring of `verbose` kwarg
lukashergt Jun 7, 2023
1327055
rewrite `credibility_interval` _method_ to automatically do all DataF…
lukashergt Jun 9, 2023
16a0d42
version bump to 2.0.0-beta.35
lukashergt Jun 14, 2023
49a3d1b
version bump to 2.0.0-beta.36
lukashergt Jun 14, 2023
48444c4
Merge branch 'master' into credibility-interval
lukashergt Jun 14, 2023
d1cdd8c
fix flake8: blank line contains whitespace
lukashergt Jun 14, 2023
f641a40
Merge branch 'master' into credibility-interval
lukashergt Jun 14, 2023
5fb72f1
version bump to 2.0.0b37
lukashergt Jun 14, 2023
bad654c
update from `ncompress=-1` to `ncompress='equal'`
lukashergt Jun 14, 2023
26ce56a
Merge branch 'master' into credibility-interval
lukashergt Jun 14, 2023
39337dc
update another instance of `ncompress=-1` to `ncompress='equal'`
lukashergt Jun 14, 2023
010a02e
implement `return_covariance` option for lower-limit and upper-limit …
lukashergt Jun 15, 2023
f05f3fd
update `test_credibility_interval` to new dataframe return values
lukashergt Jun 15, 2023
f1e4d11
Merge branch 'master' into credibility-interval
williamjameshandley Jun 29, 2023
707376b
Merge branch 'master' into credibility-interval
lukashergt Jul 19, 2023
463ae88
Merge branch 'master' into credibility-interval
williamjameshandley Jul 26, 2023
49863cc
newline
williamjameshandley Jul 26, 2023
1ec5b8e
move `credibility_interval` method from `samples.py` to `weighted_pan…
lukashergt Jul 27, 2023
0c63e6e
add tests for Series method of `credibility_interval`
lukashergt Jul 27, 2023
54bf848
Merge branch 'master' into credibility-interval
lukashergt Jul 27, 2023
292aba5
Merge branch 'master' into credibility-interval
lukashergt Jul 31, 2023
b85b84d
Merge branch 'master' into credibility-interval
williamjameshandley Aug 1, 2023
525381c
Merge branch 'master' into credibility-interval
williamjameshandley Aug 4, 2023
9fabe8d
Remove unnecessary assertion
Aug 4, 2023
3da3115
Merge branch 'master' into credibility-interval
lukashergt Aug 15, 2023
56a0c6c
version bump to 2.3.0
lukashergt Aug 15, 2023
8aa2bb8
Merge branch 'master' into credibility-interval
lukashergt Aug 16, 2023
5832781
Merge branch 'master' into credibility-interval
lukashergt Aug 16, 2023
48b1a8b
version bump to 2.4.0
lukashergt Aug 16, 2023
202d754
Merge branch 'master' into credibility-interval
lukashergt Sep 30, 2023
fe25aeb
version bump to 2.5.0
lukashergt Sep 30, 2023
3e6bd4b
Merge branch 'master' into credibility-interval
williamjameshandley May 15, 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: 2 additions & 0 deletions anesthetic/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import pandas
import copy
import warnings
from scipy.interpolate import interp1d
from scipy.optimize import minimize_scalar
williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved
from collections.abc import Sequence
from anesthetic.plot import (make_1d_axes, make_2d_axes, fastkde_plot_1d,
kde_plot_1d, hist_plot_1d, scatter_plot_2d,
Expand Down
64 changes: 64 additions & 0 deletions anesthetic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pandas
from scipy import special
from scipy.interpolate import interp1d
from scipy.optimize import minimize_scalar
from scipy.stats import kstwobign
from matplotlib.tri import Triangulation
import contextlib
Expand Down Expand Up @@ -85,6 +86,69 @@ def quantile(a, q, w=None, interpolation='linear'):
return quant


def credibility_interval(samples, weights=None, level=0.68, method="hpd"):
"""Compute the credibility interval of weighted samples. Based on
linear interpolation of the cumulative density function, thus expected
discretization errors on the scale of distances between samples.

Parameters
----------
samples: array
Samples to compute the credibility interval of.
samples: array, optional
Weights corresponding to samples. Default: ones
level: float, optional
Credibility level (probability, <1). Default: 0.68
method: str, optional
Which definition of interval to use. Default: 'hpd'
* hpd: Calculate highest posterior density (HPD) interval,
equivalent to iso-pdf interval (interval with same probability
density at each end) if distribution is unimodal.
* ll, ul: Lower limit, upper limit. One-sided limits for which
`level` fraction of the samples lie above / below the limit.
* et: Equal-tailed interval with the same fraction of samples
below and above the interval region.

Returns
-------
limit: tuple or float
Tuple [lower, upper] for hpd/et, or float for ll/ul
"""
if level >= 1:
raise ValueError('level must be <1, got {0:.2f}'.format(level))
if len(np.shape(samples)) != 1:
raise ValueError('Support only 1D arrays for samples')
if weights is not None and np.shape(samples) != np.shape(weights):
raise ValueError('Shape of samples and weights differs')

weights = np.ones(len(samples)) if weights is None else weights
# Sort and normalize
order = np.argsort(samples)
samples = np.array(samples)[order]
weights = np.array(weights)[order]/np.sum(weights)
# Compute inverse cumulative distribution function
S = np.array([np.min(samples), *samples, np.max(samples)])
CDF = np.append(np.insert(np.cumsum(weights), 0, 0), 1)
invCDF = interp1d(CDF, S)

if method == "hpd":
# Find smallest interval
def distance(Y, level=level):
return invCDF(Y+level)-invCDF(Y)
res = minimize_scalar(distance, bounds=(0, 1-level), method="Bounded")
return np.array([invCDF(res.x), invCDF(res.x+level)])
elif method == "ll":
# Get value from which we reach the desired level
return invCDF(1-level)
elif method == "ul":
# Get value to which we reach the desired level
return invCDF(level)
elif method == "et":
return np.array([invCDF((1-level)/2), invCDF((1+level)/2)])
else:
raise ValueError("Method '{0:}' unknown".format(method))


def check_bounds(d, xmin=None, xmax=None):
"""Check if we need to apply strict bounds."""
if len(d) > 0:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from anesthetic import MCMCSamples, NestedSamples, make_1d_axes, make_2d_axes
from anesthetic.samples import merge_nested_samples
from anesthetic.samples import merge_samples_weighted
from anesthetic.utils import credibility_interval
from numpy.testing import (assert_array_equal, assert_array_almost_equal,
assert_array_less)
from pandas.testing import assert_frame_equal
Expand Down Expand Up @@ -887,6 +888,23 @@ def test_plotting_with_integer_names():
samples_1['0']


def test_credibility_interval():
np.random.seed(3)
samples = NestedSamples(root='./tests/example_data/pc')
assert np.allclose(credibility_interval(samples["x0"], level=0.68,
weights=samples.weights, method="hpd"),
[-0.1, 0.1], atol=0.02)
assert np.allclose(credibility_interval(samples["x0"], level=0.95,
weights=samples.weights, method="et"),
[-0.2, 0.2], atol=0.02)
assert np.isclose(credibility_interval(samples["x0"], level=0.975,
weights=samples.weights, method="ul"),
0.2, atol=0.02)
assert np.isclose(credibility_interval(samples["x0"], level=0.5,
weights=samples.weights, method="ll"),
0, atol=0.001)


def test_logL_list():
np.random.seed(5)
default = NestedSamples(root='./tests/example_data/pc')
Expand Down