diff --git a/bilby/core/result.py b/bilby/core/result.py index 823b277cdb22d0c1daeefa36140591dd63224b7c..2ec81338ee7ca5da541c94e271fe6024768fb87c 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 593fe7a6cfc68f701acb2a87a7506a2f14c1ccc1..2baf6e08d855b288d9d2d49bb4d1821083168493 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