Skip to content

Commit

Permalink
fixes for minos everywhere when fcn is n-dim (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDembinski authored Dec 18, 2020
1 parent 1a30804 commit f05cd8d
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 82 deletions.
15 changes: 9 additions & 6 deletions src/iminuit/_minuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,8 @@ def minos(self, *parameters, cl=None, ncall=None):
self
"""
ncall = 0 if ncall is None else int(ncall)

if cl is None:
factor = 1.0
else:
Expand All @@ -803,19 +805,20 @@ def minos(self, *parameters, cl=None, ncall=None):
factor = chi2(1).ppf(cl)

if not self._fmin:
# create a FunctionMinimum for MnMinos
# create a seed minimum for MnMinos
fm = FunctionMinimum(
self._fcn, self._last_state, self._strategy, self._tolerance
)
# running MnHesse on seed is necessary for MnMinos to work
hesse = MnHesse(self.strategy)
hesse(self._fcn, fm, ncall)
self._last_state = fm.state
self._make_covariance()
else:
fm = self._fmin._src

ncall = 0 if ncall is None else int(ncall)

if not fm.is_valid:
raise RuntimeError(
"Function minimum is not valid. Make sure MIGRAD converged."
)
raise RuntimeError("Function minimum is not valid.")

if len(parameters) == 0:
pars = [par for par in self.parameters if not self.fixed[par]]
Expand Down
14 changes: 9 additions & 5 deletions tests/test_minuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1461,10 +1461,14 @@ def test_minos_new_min():
assert m.merrors["x"].upper == approx(1.1, abs=1e-2)


@pytest.mark.parametrize("x", (0.9, 1.0, 1.1))
def test_minos_without_migrad(x):
m = Minuit(lsq(lambda x: (x - 1) ** 2), x)
def test_minos_without_migrad():
m = Minuit(lsq(lambda x, y: (x - 1) ** 2 + (y / 2) ** 2), 1.001, 0.001)
m.minos()
me = m.merrors["x"]
assert x + me.lower == approx(0, abs=5e-3)
assert x + me.upper == approx(2, abs=5e-3)
assert me.is_valid
assert me.lower == approx(-1, abs=5e-3)
assert me.upper == approx(1, abs=5e-3)
me = m.merrors["y"]
assert me.is_valid
assert me.lower == approx(-2, abs=5e-3)
assert me.upper == approx(2, abs=5e-3)
Loading

0 comments on commit f05cd8d

Please sign in to comment.