From 5b14c183bd702f8d7ad613af731cf63492705f15 Mon Sep 17 00:00:00 2001 From: Colm Talbot Date: Tue, 29 Sep 2026 08:23:43 -0400 Subject: [PATCH 1/6] FEAT: discard dynesty mcmc chains that are too short --- bilby/core/sampler/dynesty_utils.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/bilby/core/sampler/dynesty_utils.py b/bilby/core/sampler/dynesty_utils.py index 6d9ee5f7c..3516d16f3 100644 --- a/bilby/core/sampler/dynesty_utils.py +++ b/bilby/core/sampler/dynesty_utils.py @@ -109,6 +109,8 @@ def __init__(self, **kwargs): self.sampler_kwargs["walks"] = self.walks self.naccept = kwargs.get("naccept", 10) self.maxmcmc = kwargs.get("maxmcmc", 5000) + self.sampler_kwargs["target"] = self.naccept + self.sampler_kwargs["maxmcmc"] = self.maxmcmc def tune(self, tuning_info, update=True): """ @@ -228,6 +230,31 @@ def sample(args): "reject": walks - naccept, } + # check that the number of accepted steps is consistent with the target + # number of accepted steps, if it is much lower than the target, our + # mcmc chain is too short and we may be open to bias. If we're hitting + # maxmcmc, this is expected, but it also happens at the beginning of the + # run or around phase transitions, so we discard this point and so an + # equivalent longer mcmc will happen at the next iteration + dist = binom(n=walks, p=args.kwargs["target"] / walks) + minimum = dist.ppf(1e-4) + undersampled = naccept < minimum + if undersampled and walks >= args.kwargs["maxmcmc"]: + logger.warning( + f"Requested {args.kwargs['target']} accepted steps per MCMC chain, " + f"but only {naccept} were accepted out of the maximum allowed number " + f"of steps ({walks}). Increase 'maxmcmc' to improve convergence." + ) + elif undersampled: + logger.info( + f"Only {naccept} accepted steps out of target {args.kwargs['target']}. " + "Discarding this point. " + "This is expected at the beginning of the run, increase 'walks'. " + "This can also happen when the geometry of the likelihood surface " + "changes drastically." + ) + logl = args.loglstar - 1 + return SamplerReturn( u=current_u, v=current_v, From 21b47bb550f2fb7fcbc7bfd5e40c99075c6152cc Mon Sep 17 00:00:00 2001 From: Colm Talbot Date: Tue, 29 Sep 2026 08:31:29 -0400 Subject: [PATCH 2/6] BUG: fix missing import --- bilby/core/sampler/dynesty_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bilby/core/sampler/dynesty_utils.py b/bilby/core/sampler/dynesty_utils.py index 3516d16f3..9f41cc561 100644 --- a/bilby/core/sampler/dynesty_utils.py +++ b/bilby/core/sampler/dynesty_utils.py @@ -4,6 +4,7 @@ import numpy as np from dynesty.internal_samplers import InternalSampler, SamplerReturn from dynesty.utils import SamplerHistoryItem, apply_reflect, get_random_generator +from scipy.stats import binom from ...bilby_mcmc.chain import calculate_tau from ..utils.log import logger From 2540850c53238909e6b4ae8cd4a6faf95d306e24 Mon Sep 17 00:00:00 2001 From: Colm Talbot Date: Tue, 29 Sep 2026 08:40:03 -0400 Subject: [PATCH 3/6] TEST: update dynesty tests with new kwarg --- test/core/sampler/dynesty_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/core/sampler/dynesty_test.py b/test/core/sampler/dynesty_test.py index ca15eb021..edf25e864 100644 --- a/test/core/sampler/dynesty_test.py +++ b/test/core/sampler/dynesty_test.py @@ -410,6 +410,7 @@ def test_proposal_functions_run_new(self): args = Dummy(u=np.ones(4) / 2, axes=np.ones((2, 2))) args.live_points[0] += 1 args.kwargs["ncdim"] = 2 + args.kwargs["target"] = 1 # args.kwargs["live"][0] += 1 for proposals in [ ["diff"], From e0b9885f57f72333ff176c0fba4ab75c544cf0c6 Mon Sep 17 00:00:00 2001 From: Colm Talbot Date: Mon, 19 Oct 2026 10:55:49 -0400 Subject: [PATCH 4/6] LOG: Change warning to debug for maxmcmc Also, fix string formatting for log messages --- bilby/core/sampler/dynesty_utils.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/bilby/core/sampler/dynesty_utils.py b/bilby/core/sampler/dynesty_utils.py index 9f41cc561..6b69f6890 100644 --- a/bilby/core/sampler/dynesty_utils.py +++ b/bilby/core/sampler/dynesty_utils.py @@ -241,18 +241,23 @@ def sample(args): minimum = dist.ppf(1e-4) undersampled = naccept < minimum if undersampled and walks >= args.kwargs["maxmcmc"]: - logger.warning( - f"Requested {args.kwargs['target']} accepted steps per MCMC chain, " - f"but only {naccept} were accepted out of the maximum allowed number " - f"of steps ({walks}). Increase 'maxmcmc' to improve convergence." + logger.debug( + "Requested %s accepted steps per MCMC chain, " + "but only %s were accepted out of the maximum allowed number " + "of steps (%s). Increase 'maxmcmc' to improve convergence." + args.kwargs['target'], + naccept, + walks, ) elif undersampled: logger.info( - f"Only {naccept} accepted steps out of target {args.kwargs['target']}. " + "Only %s accepted steps out of target %s. " "Discarding this point. " "This is expected at the beginning of the run, increase 'walks'. " "This can also happen when the geometry of the likelihood surface " - "changes drastically." + "changes drastically.", + naccept, + args.kwargs['target'], ) logl = args.loglstar - 1 From 2a65016511055302639c519de84ca8e528e2f77f Mon Sep 17 00:00:00 2001 From: Colm Talbot Date: Mon, 19 Oct 2026 11:05:41 -0400 Subject: [PATCH 5/6] TYPO: add missing comma --- bilby/core/sampler/dynesty_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bilby/core/sampler/dynesty_utils.py b/bilby/core/sampler/dynesty_utils.py index 6b69f6890..975a83c9f 100644 --- a/bilby/core/sampler/dynesty_utils.py +++ b/bilby/core/sampler/dynesty_utils.py @@ -244,7 +244,7 @@ def sample(args): logger.debug( "Requested %s accepted steps per MCMC chain, " "but only %s were accepted out of the maximum allowed number " - "of steps (%s). Increase 'maxmcmc' to improve convergence." + "of steps (%s). Increase 'maxmcmc' to improve convergence.", args.kwargs['target'], naccept, walks, From b5c511f0227f084d891878310de7da79f490cf86 Mon Sep 17 00:00:00 2001 From: Colm Talbot Date: Wed, 21 Oct 2026 12:35:02 -0400 Subject: [PATCH 6/6] FMT: fix string formatting to appease the precommit gods --- bilby/core/sampler/dynesty_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bilby/core/sampler/dynesty_utils.py b/bilby/core/sampler/dynesty_utils.py index 975a83c9f..3096bb97d 100644 --- a/bilby/core/sampler/dynesty_utils.py +++ b/bilby/core/sampler/dynesty_utils.py @@ -245,7 +245,7 @@ def sample(args): "Requested %s accepted steps per MCMC chain, " "but only %s were accepted out of the maximum allowed number " "of steps (%s). Increase 'maxmcmc' to improve convergence.", - args.kwargs['target'], + args.kwargs["target"], naccept, walks, ) @@ -257,7 +257,7 @@ def sample(args): "This can also happen when the geometry of the likelihood surface " "changes drastically.", naccept, - args.kwargs['target'], + args.kwargs["target"], ) logl = args.loglstar - 1