diff --git a/cosmosis/samplers/maxlike/maxlike_sampler.py b/cosmosis/samplers/maxlike/maxlike_sampler.py index 33805e83..8c2bd8af 100755 --- a/cosmosis/samplers/maxlike/maxlike_sampler.py +++ b/cosmosis/samplers/maxlike/maxlike_sampler.py @@ -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)) diff --git a/cosmosis/test/test_samplers.py b/cosmosis/test/test_samplers.py index cb20b192..41f4d105 100644 --- a/cosmosis/test/test_samplers.py +++ b/cosmosis/test/test_samplers.py @@ -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) diff --git a/requirements.txt b/requirements.txt index a0bfbdc3..c2020f38 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,3 +14,4 @@ dulwich urllib3 scikit-learn nautilus-sampler >= 1.0.1 +Py-BOBYQA diff --git a/setup.py b/setup.py index 73a14034..1cecd29e 100644 --- a/setup.py +++ b/setup.py @@ -196,6 +196,7 @@ def run(self): "dulwich", "scikit-learn", "future", + "Py-BOBYQA", ]