Skip to content

Commit

Permalink
fix: check POIs floating (#1409)
Browse files Browse the repository at this point in the history
* Add all_pois_floating to infer.utils to check if POIs are fixed
* Add check in hypotest that POIs are not fixed
* Add test that hypotest raises an exception if a fixed POI is used in the given model
  • Loading branch information
lukasheinrich authored Apr 15, 2021
1 parent 0b934a1 commit 0588b21
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Fits and Tests
mle.fixed_poi_fit
hypotest
intervals.upperlimit
utils.all_pois_floating

Exceptions
----------
Expand Down
15 changes: 15 additions & 0 deletions src/pyhf/infer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

from . import utils
from .. import get_backend
from .. import exceptions


def _check_hypotest_prerequisites(pdf, data, init_pars, par_bounds, fixed_params):
if pdf.config.poi_index is None:
raise exceptions.UnspecifiedPOI(
'No POI is defined. A POI is required to run a hypothesis test.'
)

if not utils.all_pois_floating(pdf, fixed_params):
raise exceptions.InvalidModel(
f'POI at index [{pdf.config.poi_index}] is set as fixed, which makes inference impossible. Please unfix the POI to continue.'
)


def hypotest(
Expand Down Expand Up @@ -131,6 +144,8 @@ def hypotest(
par_bounds = par_bounds or pdf.config.suggested_bounds()
fixed_params = fixed_params or pdf.config.suggested_fixed()

_check_hypotest_prerequisites(pdf, data, init_pars, par_bounds, fixed_params)

calc = utils.create_calculator(
calctype,
data,
Expand Down
18 changes: 18 additions & 0 deletions src/pyhf/infer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
log = logging.getLogger(__name__)


def all_pois_floating(pdf, fixed_params):
r"""
Check whether all POI(s) are floating (i.e. not within the fixed set).
Args:
pdf (~pyhf.pdf.Model): The statistical model adhering to the schema
``model.json``.
fixed_params (:obj:`list` or `tensor` of :obj:`bool`): Array of
:obj:`bool` indicating if model parameters are fixed.
Returns:
:obj:`bool`: The result whether all POIs are floating.
"""

poi_fixed = fixed_params[pdf.config.poi_index]
return not poi_fixed


def create_calculator(calctype, *args, **kwargs):
"""
Creates a calculator object of the specified `calctype`.
Expand Down
12 changes: 12 additions & 0 deletions tests/test_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,15 @@ def test_toy_calculator(tmpdir, hypotest_args):
assert toy_calculator_qtilde_mu.teststatistic(mu_test) == pytest.approx(
3.938244920380498, 1e-07
)


def test_fixed_poi(tmpdir, hypotest_args):
"""
Check that the return structure of pyhf.infer.hypotest with the
additon of the return_expected keyword arg is as expected
"""

_, _, pdf = hypotest_args
pdf.config.param_set('mu').suggested_fixed = [True]
with pytest.raises(pyhf.exceptions.InvalidModel):
pyhf.infer.hypotest(*hypotest_args)

0 comments on commit 0588b21

Please sign in to comment.