From 8ed6875b3211bb77a8ab52dd1ef66efaf6e68d4c Mon Sep 17 00:00:00 2001
From: Gregory Ashton <gregory.ashton@ligo.org>
Date: Fri, 1 May 2020 09:44:47 +1000
Subject: [PATCH] Move Kish effective sample size calculation to separate
 function

---
 bilby/core/result.py |  2 +-
 bilby/core/utils.py  | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/bilby/core/result.py b/bilby/core/result.py
index 823b277cd..2ec81338e 100644
--- a/bilby/core/result.py
+++ b/bilby/core/result.py
@@ -198,7 +198,7 @@ def reweight(result, label=None, new_likelihood=None, new_prior=None,
     ln_weights, new_log_likelihood_array, new_log_prior_array = get_weights_for_reweighting(
         result, new_likelihood=None, new_prior=None, old_likelihood=None, old_prior=None)
 
-    log_n_eff = 2 * logsumexp(ln_weights) - logsumexp(2 * ln_weights)
+    log_n_eff = utils.kish_log_effective_sample_size(ln_weights)
     n_eff = int(fraction * np.exp(log_n_eff))
     logger.info("Reweighted posterior has {} effective samples".format(n_eff))
     weights = np.exp(ln_weights)
diff --git a/bilby/core/utils.py b/bilby/core/utils.py
index 593fe7a6c..2baf6e08d 100644
--- a/bilby/core/utils.py
+++ b/bilby/core/utils.py
@@ -1205,6 +1205,26 @@ def safe_save_figure(fig, filename, **kwargs):
         fig.savefig(fname=filename, **kwargs)
 
 
+def kish_log_effective_sample_size(ln_weights):
+    """ Calculate the Kish effective sample size from the natural-log weights
+
+    See https://en.wikipedia.org/wiki/Effective_sample_size for details
+
+    Parameters
+    ----------
+    ln_weights: array
+        An array of the ln-weights
+
+    Returns
+    -------
+    ln_n_eff:
+        The natural-log of the effective sample size
+
+    """
+    log_n_eff = 2 * logsumexp(ln_weights) - logsumexp(2 * ln_weights)
+    return log_n_eff
+
+
 class IllegalDurationAndSamplingFrequencyException(Exception):
     pass
 
-- 
GitLab