Skip to content

Commit

Permalink
test that the peak hint is working
Browse files Browse the repository at this point in the history
  • Loading branch information
joezuntz committed Sep 8, 2024
1 parent 15e5de2 commit 6310fdc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 1 addition & 2 deletions cosmosis/samplers/dynesty/dynesty_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ def execute(self):
for sample, logwt, logl, derived in zip(results['samples'],results['logwt'], results['logl'], results['blob']):
prior = derived[0]
post = prior + logl
self.output.parameters(sample, logwt, prior, post, derived[1:])
self.output.parameters(sample, derived[1:], logwt, prior, post)

self.output.final("efficiency", results['eff'])
self.output.final("nsample", len(results['samples']))
self.output.final("log_z", results['logz'][-1])
self.output.final("log_z_error", results['logzerr'][-1])

self.converged = True


Expand Down
16 changes: 13 additions & 3 deletions cosmosis/test/test_samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# parameters, so our expected prior is 1/6 for each of them.
EXPECTED_LOG_PRIOR = 2*np.log(1./6)

def run(name, check_prior, check_extra=True, can_postprocess=True, do_truth=False, no_extra=False, pp_extra=True, pp_2d=True, **options):
def run(name, check_prior, check_extra=True, can_postprocess=True, do_truth=False, no_extra=False, pp_extra=True, pp_2d=True, hints_peak=True, **options):

sampler_class = Sampler.registry[name]

Expand Down Expand Up @@ -61,6 +61,16 @@ def run(name, check_prior, check_extra=True, can_postprocess=True, do_truth=Fals
# but not all of them
assert not np.all(pr==-np.inf)

if hints_peak:
assert sampler.distribution_hints.has_peak()
peak = sampler.distribution_hints.get_peak()
idx = output["post"].argmax()
assert np.isclose(output["parameters--p1"][idx], peak[0])
assert np.isclose(output["parameters--p2"][idx], peak[1])
sampler.distribution_hints.del_peak()
assert not sampler.distribution_hints.has_peak()


if check_extra and not no_extra:
p1 = output['parameters--p1']
p2 = output['parameters--p2']
Expand Down Expand Up @@ -115,7 +125,7 @@ def test_truth():
run('emcee', True, walkers=8, samples=100, do_truth=True)

def test_fisher():
run('fisher', False, check_extra=False)
run('fisher', False, check_extra=False, hints_peak=False)

def test_grid():
run('grid', True, pp_extra=False, nsample_dimension=10)
Expand Down Expand Up @@ -179,7 +189,7 @@ def test_star():
run('star', False, pp_extra=False, pp_2d=False)

def test_test():
run('test', False, can_postprocess=False)
run('test', False, can_postprocess=False, hints_peak=False)

def test_list_sampler():
# test that the burn and thin parameters work
Expand Down

0 comments on commit 6310fdc

Please sign in to comment.