Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bobyqa minimizer #147

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions cosmosis/samplers/maxlike/maxlike_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,33 @@ def likefn(p_in):
if not np.isfinite(start_like):
raise RuntimeError('invalid starting point for maxlike')

result = scipy.optimize.minimize(likefn, start_vector, method=self.method,
jac=False, tol=self.tolerance, #bounds=bounds,
options={'maxiter':self.maxiter, 'disp':True})
if self.method.lower() == "bobyqa":
# use the specific pybobyqa minimizer
import pybobyqa

# this works with bounds in the form of a tuple of two arrays
lower = np.array([b[0] for b in bounds])
upper = np.array([b[1] for b in bounds])
kw = {
"seek_global_minimum": True,
"bounds": (lower,upper),
"print_progress": logs.is_enabled_for(logs.NOISY),
"rhobeg": 0.1,
"rhoend": self.tolerance,
}
result = pybobyqa.solve(likefn, start_vector, **kw)
opt_norm = result.x
else:
# Use scipy mainimizer instead
result = scipy.optimize.minimize(likefn, start_vector, method=self.method,
jac=False, tol=self.tolerance,
options={'maxiter':self.maxiter, 'disp':True})

opt_norm = result.x

opt_norm = result.x
opt = self.pipeline.denormalize_vector(opt_norm)


#Some output - first log the parameters to the screen.
#It's not really a warning - that's just a level name
results = self.pipeline.run_results(opt)
if self.max_posterior:
logs.overview("Best fit (by posterior):\n%s"%' '.join(str(x) for x in opt))
Expand Down
3 changes: 3 additions & 0 deletions cosmosis/test/test_samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ def test_gridmax():
def test_maxlike():
run('maxlike', True, can_postprocess=False)

def test_bobyqa():
run('maxlike', True, can_postprocess=False, method='bobyqa')

def test_metropolis():
run('metropolis', True, samples=20)
run('metropolis', True, samples=20, covmat_sample_start=True)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ dulwich
urllib3
scikit-learn
nautilus-sampler >= 1.0.1
Py-BOBYQA
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def run(self):
"dulwich",
"scikit-learn",
"future",
"Py-BOBYQA",

]

Expand Down
Loading