Skip to content

Commit

Permalink
Updated docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
williamjameshandley committed Sep 19, 2024
1 parent 4947c69 commit 7a10785
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
26 changes: 15 additions & 11 deletions anesthetic/tension.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,42 @@
from scipy.stats import chi2


def tension_stats(A, B, AB, nsamples=None, beta=None): # noqa: D301
def stats(A, B, AB, nsamples=None, beta=None): # noqa: D301
"""Compute tension statistics between two samples.
Using nested sampling we can compute:
- ``logR``: Logarithmic of R statistic
.. math::
\\log R = \\log Z_\\mathrm{AB} - \\log Z_\\mathrm{A}
- \\log Z_\\mathrm{B}
\\log \mathcal{R} = \\log \mathcal{Z}_\\mathrm{AB}
- \\log \mathcal{Z}_\\mathrm{A}
- \\log \mathcal{Z}_\\mathrm{B}
- ``logI``: Logarithmic of information ratio
.. math::
\\log I = D_\\mathrm{KL}^A + D_\\mathrm{KL}^B - D_\\mathrm{KL}^{AB}
\\log \mathcal{I} = \mathcal{D}_\\mathrm{KL}^A
+ \mathcal{D}_\\mathrm{KL}^B
- \mathcal{D}_\\mathrm{KL}^{AB}
- ``logS``: Logarithmic of suspiciousness
.. math::
\\log S = \\log L_\\mathrm{AB} - \\log
L_\\mathrm{A} - \\log L_\\mathrm{B}
\\log \mathcal{S} = \\log \mathcal{L}_\\mathrm{AB}
- \\log \mathcal{L}_\\mathrm{A}
- \\log \mathcal{L}_\\mathrm{B}
- ``d_G``: Gaussian model dimensionality
(or posterior variance of the log-likelihood)
.. math::
d_\\mathrm{G}/2 = \\mathrm{var}(\\log L)_P
d_\\mathrm{G}/2 = \\mathrm{var}(\\log \mathcal{L})_\mathcal{P}
- ``p``: p-value for the tension between two samples
.. math::
p = \\int_{d_\\mathrm{G} - 2 \\log S}^{\\infty} \\chi^2 (x)dx
p = \\int_{d_\\mathrm{G} - 2 \\log \mathcal{S}}^{\\infty} \\chi^2 (x)dx
Parameters
----------
Expand Down Expand Up @@ -72,15 +76,15 @@ def tension_stats(A, B, AB, nsamples=None, beta=None): # noqa: D301

logR = statsAB.logZ-statsA.logZ-statsB.logZ
samples_stats['logR'] = logR
samples_stats.set_label('logR', r'$\log{R}$')
samples_stats.set_label('logR', r'$\log\mathcal{R}$')

logI = statsA.D_KL + statsB.D_KL - statsAB.D_KL
samples_stats['logI'] = logI
samples_stats.set_label('logI', r'$\log{I}$')
samples_stats.set_label('logI', r'$\log\mathcal{I}$')

logS = statsAB.logL_P - statsA.logL_P - statsB.logL_P
samples_stats['logS'] = logS
samples_stats.set_label('logS', r'$\log{S}$')
samples_stats.set_label('logS', r'$\log\mathcal{S}$')

d_G = statsA.d_G + statsB.d_G - statsAB.d_G
samples_stats['d_G'] = d_G
Expand Down
Binary file removed tests/example_data/.DS_Store
Binary file not shown.
18 changes: 9 additions & 9 deletions tests/test_tension.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from anesthetic.examples.perfect_ns import correlated_gaussian
import numpy as np
from numpy.linalg import inv, solve, slogdet
from anesthetic.tension import tension_stats
from anesthetic.tension import stats


def test_tension_stats_compatiable_gaussian():
Expand Down Expand Up @@ -32,8 +32,7 @@ def test_tension_stats_compatiable_gaussian():

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

logR_std = samples_stats.logR.std()
logR_mean = samples_stats.logR.mean()
Expand All @@ -51,8 +50,9 @@ def test_tension_stats_compatiable_gaussian():
logI_exact = -d/2 - 1/2 * slogdet(2*np.pi*(covA+covB))[1] + np.log(V)
assert logI_mean - 3 * logI_std < logI_exact < logI_mean + 3 * logI_std

assert samples_stats.get_labels().tolist() == ([r'$\log{R}$',
r'$\log{I}$', r'$\log{S}$',
assert samples_stats.get_labels().tolist() == ([r'$\log\mathcal{R}$',
r'$\log\mathcal{I}$',
r'$\log\mathcal{S}$',
r'$d_\mathrm{G}$', r'$p$'])


Expand Down Expand Up @@ -85,8 +85,7 @@ def test_tension_stats_incompatiable_gaussian():

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

logR_std = samples_stats.logR.std()
logR_mean = samples_stats.logR.mean()
Expand All @@ -104,6 +103,7 @@ def test_tension_stats_incompatiable_gaussian():
logI_exact = -d/2 - 1/2 * slogdet(2*np.pi*(covA+covB))[1] + np.log(V)
assert logI_mean - 3 * logI_std < logI_exact < logI_mean + 3 * logI_std

assert samples_stats.get_labels().tolist() == ([r'$\log{R}$',
r'$\log{I}$', r'$\log{S}$',
assert samples_stats.get_labels().tolist() == ([r'$\log\mathcal{R}$',
r'$\log\mathcal{I}$',
r'$\log\mathcal{S}$',
r'$d_\mathrm{G}$', r'$p$'])

0 comments on commit 7a10785

Please sign in to comment.