Skip to content

Commit

Permalink
Merge pull request #156 from joezuntz/repeat-maxlike
Browse files Browse the repository at this point in the history
Expose bobyqa hessian
  • Loading branch information
joezuntz authored Dec 9, 2024
2 parents d813703 + 17d71a4 commit 55c0c6a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
8 changes: 8 additions & 0 deletions cosmosis/samplers/maxlike/maxlike_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def save_final_outputs(self, best_fit_results, final=False):
if self.output_cov:
np.savetxt(self.output_cov, results.covmat)

if self.output_ini:
self.pipeline.create_ini(results.vector, self.output_ini)


# We only want to update the distribution hints at the very end
if final:
# These values are used by subsequent samplers, if you chain
Expand Down Expand Up @@ -144,6 +148,10 @@ def run_optimizer(self, inputs):
}
optimizer_result = pybobyqa.solve(likefn, start_vector, **kw)
opt_norm = optimizer_result.x
# bobyqa calls it .hessian but scipy calls it .hess, so copy it here
# if available
if optimizer_result.hessian is not None:
optimizer_result.hess = optimizer_result.hessian
else:
# Use scipy mainimizer instead
optimizer_result = scipy.optimize.minimize(likefn, start_vector, method=self.method,
Expand Down
11 changes: 9 additions & 2 deletions cosmosis/test/test_samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ def test_maxlike():
with tempfile.TemporaryDirectory() as dirname:
output_ini = os.path.join(dirname, "output.ini")
output_cov = os.path.join(dirname, "output_cov.txt")
run('maxlike', True, can_postprocess=False, method="L-BFGS-B", max_posterior=True, output_ini=output_ini, output_cov=output_cov)
run('maxlike', True, can_postprocess=False, method="L-BFGS-B", max_posterior=True, output_ini=output_ini, output_covmat=output_cov)
assert os.path.exists(output_cov)
assert os.path.exists(output_ini)



output = run('maxlike', True, can_postprocess=False, repeats=5, start_method="prior")
Expand Down Expand Up @@ -188,7 +191,11 @@ def test_maxlike():


def test_bobyqa():
run('maxlike', True, can_postprocess=False, method='bobyqa')
with tempfile.TemporaryDirectory() as dirname:
output_cov = os.path.join(dirname, "output_cov.txt")
run('maxlike', True, can_postprocess=False, method='bobyqa', output_covmat=output_cov)
assert os.path.exists(output_cov)


def test_metropolis():
run('metropolis', True, samples=20)
Expand Down
2 changes: 1 addition & 1 deletion cosmosis/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.14'
__version__ = '3.14.1'

0 comments on commit 55c0c6a

Please sign in to comment.