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

Implementing tension statistics #333

Open
wants to merge 69 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
c0946bd
Added Will's example Class for tension calculator
DilyOng Aug 24, 2023
70c0aa4
Changed Version from 2.3.0 to 2.4.0
DilyOng Aug 24, 2023
ee1db5b
Added Class tension
DilyOng Aug 31, 2023
ffd5fae
Merge branch 'master' into tension
lukashergt Sep 30, 2023
a3d06b5
version bump to 2.5.0
lukashergt Sep 30, 2023
4a5d4b5
Created a function called tension_stats for tension statistics calcul…
DilyOng Oct 13, 2023
38d31c2
Added logLmax to the function anesthetic.examples.perfect_ns.correlat…
DilyOng Oct 13, 2023
932a201
Clean up old tension statistics files. Put the correlated guassian li…
DilyOng Oct 13, 2023
4046bc3
Merge branch 'tension' of github.com:handley-lab/anesthetic into tension
DilyOng Oct 13, 2023
de17f04
Updated logLmax
DilyOng Oct 13, 2023
6ab8e4f
remove DS_Store
AdamOrmondroyd Oct 14, 2023
3698669
Merge branch 'master' into tension
AdamOrmondroyd Oct 14, 2023
aa0c177
Added a file tension.py to anesthetic/anesthetic and it contains a fu…
DilyOng Oct 26, 2023
2f472b5
Updated the tests/test_tension_stats.py file for flake8 compliance. A…
DilyOng Oct 26, 2023
5209ad2
Updated tests/test_tension_stats.py
DilyOng Dec 5, 2023
6fb5fe0
Updated anesthetic/tests/test_tension_stats.py. Now it tests whether …
DilyOng Mar 4, 2024
151a91d
Merge branch 'master' into tension
williamjameshandley Mar 5, 2024
b0994b3
bump version to 2.9.0
williamjameshandley Mar 5, 2024
aec6a25
Deleted duplicate files.
DilyOng Mar 5, 2024
46ec095
Merge branch 'tension' of github.com:handley-lab/anesthetic into tension
DilyOng Mar 5, 2024
be72001
Merge branch 'master' into tension
williamjameshandley Mar 18, 2024
03b9b1c
Reorganised tests
williamjameshandley Mar 18, 2024
9dd5ca1
numpy linalg
williamjameshandley Mar 18, 2024
e82f125
For anesthetic.tension.tension_stats(), I 1) added docstrings, 2) add…
DilyOng Mar 20, 2024
979f621
Added docstring in public module.
DilyOng Mar 21, 2024
84c8e6e
Fixed pydocstyle issue.
DilyOng Mar 21, 2024
9909190
Fixed pydocstyle issue.
DilyOng Mar 21, 2024
6723f4c
In test_tension.py, 1) added a test for latex labels, and 2) changed …
DilyOng Apr 4, 2024
9739361
Changed V
DilyOng Apr 4, 2024
25ab2c1
typographical changes
williamjameshandley Apr 5, 2024
1691a02
both tests correct
williamjameshandley Apr 5, 2024
110029a
Correction to docstring kl
williamjameshandley Apr 5, 2024
89fab36
Further docstring updates
williamjameshandley Apr 5, 2024
8f2e60a
Corrected typo
williamjameshandley Apr 8, 2024
290f257
Merge branch 'master' into tension
lukashergt Apr 8, 2024
815a7e5
fix link in docstring
lukashergt Apr 8, 2024
ab2d14e
add anesthetic.read.csv module to documentation
lukashergt Apr 8, 2024
c11c5f6
add anesthetic.tension module to documentation
lukashergt Apr 8, 2024
d1f1c2a
fix links and some formatting issues in `tension.py` docstrings
lukashergt Apr 8, 2024
fbd733e
Merge branch 'master' into tension
williamjameshandley Apr 9, 2024
d4659bd
Merge branch 'master' into tension
lukashergt Apr 9, 2024
877d79d
Merge branch 'master' into tension
williamjameshandley Apr 9, 2024
e568bfd
Merge branch 'master' into tension
lukashergt Apr 9, 2024
4947c69
Updated the docstrings in anesthetic/tension.py.
DilyOng Apr 14, 2024
7a10785
Updated docstrings
williamjameshandley Sep 19, 2024
970f431
Merge branch 'master' into tension
williamjameshandley Sep 19, 2024
69c932a
Updated docstring to avoid Samples
williamjameshandley Sep 19, 2024
7e90ac5
replaced \\m
williamjameshandley Sep 19, 2024
75d3067
Further string corrections
williamjameshandley Sep 19, 2024
740eacf
Further debugging docstrings
williamjameshandley Sep 19, 2024
d236f34
Logarithmic -> Logarithm
williamjameshandley Sep 19, 2024
79838dd
Merge branch 'master' into tension
lukashergt Sep 27, 2024
73d6180
correct spelling of compatible
lukashergt Sep 27, 2024
5675e25
surround `@` by spaces
lukashergt Sep 27, 2024
a7b7fb5
remove occurences of missing leading 0, e.g. `.01`
lukashergt Sep 27, 2024
e3bf650
use `ln` rather than `log` for the latex labels
lukashergt Sep 27, 2024
9dae22f
streamline and speed up tension tests a bit
lukashergt Sep 27, 2024
8c70f2f
make tension docstring more readable
lukashergt Sep 27, 2024
88b7ec6
optionally allow for passing a pre-computed stats instance to tension…
lukashergt Sep 27, 2024
51c0975
simplify tension tests and add test for direct input of nested sampli…
lukashergt Sep 27, 2024
0649d53
Update README.rst
lukashergt Sep 27, 2024
6b07788
Update _version.py
lukashergt Sep 27, 2024
ce82e13
Merge branch 'master' into tension
lukashergt Sep 27, 2024
d31b6e0
Changed log I to I for suspiciousness. Corresponding files are change…
DilyOng Feb 3, 2025
d2db456
Restart Github pipeline
DilyOng Feb 3, 2025
7de2543
Merge branch 'master' into tension
williamjameshandley Feb 4, 2025
7717b04
Changed version number from 2.11.0 back to 2.10.0 in README.rst and a…
DilyOng Feb 4, 2025
71325d7
Redefined I = D_A + D_B - D_AB, and logS = logR - I, such that I is t…
DilyOng Feb 5, 2025
e42b048
Removed unused python package numpy in anesthetic/tension.py.
DilyOng Feb 5, 2025
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.0
: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.0'
__version__ = '2.9.0'
17 changes: 17 additions & 0 deletions anesthetic/tension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from anesthetic.samples import Samples


def tension_stats(A,B,AB,nsamples,beta):
williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved
# Compute Nested Sampling statistics
statsA = A.stats(nsamples,beta)
statsB = B.stats(nsamples,beta)
statsAB = AB.stats(nsamples,beta)

samples = Samples(index=statsA.index)
logR = statsAB.logZ-statsA.logZ-statsB.logZ
samples['logR']=logR
logI = statsA.D_KL + statsB.D_KL - statsAB.D_KL
samples['logI']=logI
samples['logS']=logR-logI
samples['d_G']=statsA.d_G + statsB.d_G - statsAB.d_G
williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved
return samples
27 changes: 27 additions & 0 deletions anesthetic/tension_pvalue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from anesthetic.samples import Samples
from scipy.stats import chi2

Check warning on line 2 in anesthetic/tension_pvalue.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/tension_pvalue.py#L1-L2

Added lines #L1 - L2 were not covered by tests


def tension_stats(A, B, AB, nsamples, beta):

Check warning on line 5 in anesthetic/tension_pvalue.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/tension_pvalue.py#L5

Added line #L5 was not covered by tests
# Compute Nested Sampling statistics
statsA = A.stats(nsamples, beta)
statsB = B.stats(nsamples, beta)
statsAB = AB.stats(nsamples, beta)

Check warning on line 9 in anesthetic/tension_pvalue.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/tension_pvalue.py#L7-L9

Added lines #L7 - L9 were not covered by tests

samples = Samples(index=statsA.index)
print(samples)
logR = statsAB.logZ-statsA.logZ-statsB.logZ
samples['logR'] = logR

Check warning on line 14 in anesthetic/tension_pvalue.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/tension_pvalue.py#L11-L14

Added lines #L11 - L14 were not covered by tests

logI = statsA.D_KL + statsB.D_KL - statsAB.D_KL
samples['logI'] = logI

Check warning on line 17 in anesthetic/tension_pvalue.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/tension_pvalue.py#L16-L17

Added lines #L16 - L17 were not covered by tests

logS = logR-logI
samples['logS'] = logS

Check warning on line 20 in anesthetic/tension_pvalue.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/tension_pvalue.py#L19-L20

Added lines #L19 - L20 were not covered by tests

d_G = statsA.d_G + statsB.d_G - statsAB.d_G
samples['d_G'] = d_G

Check warning on line 23 in anesthetic/tension_pvalue.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/tension_pvalue.py#L22-L23

Added lines #L22 - L23 were not covered by tests

p = chi2.sf(d_G-2*logS, d_G)
samples['p'] = p
return samples

Check warning on line 27 in anesthetic/tension_pvalue.py

View check run for this annotation

Codecov / codecov/patch

anesthetic/tension_pvalue.py#L25-L27

Added lines #L25 - L27 were not covered by tests
williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved
Binary file added tests/example_data/.DS_Store
williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
Empty file added tests/test.ipynb
williamjameshandley marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
122 changes: 122 additions & 0 deletions tests/test_tension_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#from anesthetic.samples import Samples
from anesthetic.examples.perfect_ns import correlated_gaussian
#from anesthetic import read_chains, make_2d_axes
import numpy as np
#import matplotlib.pyplot as plt
from scipy import linalg
from anesthetic.tension import tension_stats


def get_meanAB(meanA, meanB, covA, covB, covAB):
meanAB = covAB@(np.linalg.solve(covA, meanA)+np.linalg.solve(covB, meanB))
return meanAB


def get_covAB(covA, covB):
covAB_inv = linalg.inv(covA) + linalg.inv(covB)
covAB = linalg.inv(covAB_inv)
return covAB


def get_logLmaxAB(meanA, meanB, covA, covB, logLmaxA=0, logLmaxB=0):
meandiff = np.array(meanA)-np.array(meanB)
logLmaxAB = (-1/2 * [email protected](covA+covB, meandiff) +
logLmaxA + logLmaxB)
return logLmaxAB


def get_logR(meanA, meanB, covA, covB, V):
meandiff = np.array(meanA)-np.array(meanB)
logR = (-1/2*[email protected](covA+covB, meandiff) -
1/2*np.linalg.slogdet(2*np.pi*(covA+covB))[1] + np.log(V))
return logR

def get_logS(meanA, meanB, covA, covB, d):
meandiff = np.array(meanA)-np.array(meanB)
logS = d/2 - 1/2*[email protected](covA+covB, meandiff)
return logS

def get_logI(covA, covB, d, V):
logI = -d/2 - 1/2 * np.log(linalg.det(2*np.pi*(covA+covB))) + np.log(V)
return logI

def test_tension_stats_compatiable_gaussian():
nlive = 1000
bounds = [[0, 1], [0, 1], [0, 1]]

meanA = [0.1, 0.3, 0.5]
covA = np.array([[.01, 0.009, 0], [0.009, .01, 0], [0, 0, 0.1]])*0.01
logLmaxA = 0
samplesA = correlated_gaussian(nlive, meanA, covA, bounds, logLmaxA)

meanB = [0.1, 0.3, 0.5]
covB = np.array([[.01, -0.009, 0.01], [-0.009, .01, -0.001], [0.01, -0.001, 0.1]])*0.01
logLmaxB = 0
samplesB = correlated_gaussian(nlive, meanB, covB, bounds, logLmaxB)

covAB = get_covAB(covA, covB)
meanAB = get_meanAB(meanA, meanB, covA, covB, covAB)
logLmaxAB = get_logLmaxAB(meanA, meanB, covA, covB, logLmaxA=0, logLmaxB=0)
samplesAB = correlated_gaussian(nlive, meanAB, covAB, bounds, logLmaxAB)

nsamples = 1000
beta = 1
samples = tension_stats(samplesA, samplesB, samplesAB, nsamples, beta)

logR_std = samples.logR.std()
logR_mean = samples.logR.mean()

logS_std = samples.logS.std()
logS_mean = samples.logS.mean()

logI_std = samples.logI.std()
logI_mean = samples.logI.mean()

logR_exact = get_logR(meanA, meanB, covA, covB, V=1)
logS_exact = get_logS(meanA, meanB, covA, covB, d=len(bounds))
logI_exact = get_logI(covA, covB, d=len(bounds), V=1)

assert logR_mean - 3 * logR_std < logR_exact < logR_mean + 3 * logR_std
assert logS_mean - 3 * logS_std < logS_exact < logS_mean + 3 * logS_std
assert logI_mean - 3 * logI_std < logI_exact < logI_mean + 3 * logI_std


def test_tension_stats_incompatiable_gaussian():
nlive = 1000
bounds = [[0, 1], [0, 1], [0, 1]]

meanA = [0.1, 0.3, 0.5]
covA = np.array([[.01, 0.009, 0], [0.009, .01, 0], [0, 0, 0.1]])*0.01
logLmaxA = 0
samplesA = correlated_gaussian(nlive, meanA, covA, bounds, logLmaxA)

meanB = [0.15, 0.25, 0.45]
covB = np.array([[.01, -0.009, 0.01], [-0.009, .01, -0.001], [0.01, -0.001, 0.1]])*0.01
logLmaxB = 0
samplesB = correlated_gaussian(nlive, meanB, covB, bounds, logLmaxB)

covAB = get_covAB(covA, covB)
meanAB = get_meanAB(meanA, meanB, covA, covB, covAB)
logLmaxAB = get_logLmaxAB(meanA, meanB, covA, covB, logLmaxA=0, logLmaxB=0)
samplesAB = correlated_gaussian(nlive, meanAB, covAB, bounds, logLmaxAB)

nsamples = 1000
beta = 1
samples = tension_stats(samplesA, samplesB, samplesAB, nsamples, beta)

logR_std = samples.logR.std()
logR_mean = samples.logR.mean()

logS_std = samples.logS.std()
logS_mean = samples.logS.mean()

logI_std = samples.logI.std()
logI_mean = samples.logI.mean()

logR_exact = get_logR(meanA, meanB, covA, covB, V=1)
logS_exact = get_logS(meanA, meanB, covA, covB, d=len(bounds))
logI_exact = get_logI(covA, covB, d=len(bounds), V=1)

assert logR_mean - 3 * logR_std < logR_exact < logR_mean + 3 * logR_std
assert logS_mean - 3 * logS_std < logS_exact < logS_mean + 3 * logS_std
assert logI_mean - 3 * logI_std < logI_exact < logI_mean + 3 * logI_std
Loading