Skip to content

Commit

Permalink
Merge pull request #147 from joezuntz/bobyqa
Browse files Browse the repository at this point in the history
Add bobyqa minimizer
  • Loading branch information
joezuntz authored Oct 28, 2024
2 parents 4e8900d + 7684226 commit ab828be
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
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

0 comments on commit ab828be

Please sign in to comment.