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

feat: limit parameter variations in rankings to stay within parameter bounds #490

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions src/cabinetry/fit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ def ranking(
init_pars = init_pars or model.config.suggested_init()
fix_pars = fix_pars or model.config.suggested_fixed()

par_bounds = par_bounds or [
tuple(bound) for bound in model.config.suggested_bounds()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this behave correctly if some parameters are bound but not others? I am guessing then the bound will be None for unboun ones and we have the same issue in variations?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All parameters are meant to have bounds as far as I am aware so a None should not happen here. I am however unsure if this is technically a guarantee from pyhf, if so at least I do not see it explicitly mentioned. I do not remember ever running into an example where the bounds are not set (other than scikit-hep/pyhf#1516 which I would call a bug). I thought iminuit also requires bounds but that turns out to be wrong, it only requires starting values.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I meant here was that If par_bounds is not None or a "falsy"-valued object, it will be chosen instead of the suggested bounds list. So if par_bounds is for example (None, (0,10), None), we still have two unbound parameters. Am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I understand your point I think, if a user provides an invalid value then yes this will cause problems. Such an example would not follow the expected format Optional[List[Tuple[float, float]]].

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah so something like:

model, data = model_utils.model_and_data(spec)
fit_results = fit.fit(model,data)
ranking_results = fit.ranking(model, data, fit_results=fit_results, par_bounds=[None, None, (-2,2)])

fails with the current implementation. I guess ideally this user input should be fine, and we should be checking be updating individual bounds which are not set. I don't see why we should completely stop user from specifying only one parameter bound. A simple insertion of elements from suggested_bounds where there is a None entry can fix this issue.

Copy link
Member Author

@alexander-held alexander-held Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[None, None, (-2,2)] is an invalid input at the moment, it has to be either None or a list of tuples of floats according to the desired type. This is mostly done to align with the pyhf API. I'm not opposed to supporting what you suggest, though it does feel a bit complicated to me to have a user create a list of None and then update it at the right index. In that case they might as well get the suggested_bounds themselves as a starting point and update that index?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But either way the addition doesn't hurt so I'm fine with it.

]

all_impacts = []
for i_par, label in enumerate(labels):
if i_par == poi_index:
Expand All @@ -613,6 +617,10 @@ def ranking(
log.debug(f"impact of {label} is zero, skipping fit")
parameter_impacts.append(0.0)
else:
if not par_bounds[i_par][0] <= np_val <= par_bounds[i_par][1]:
np_val = min(
max(np_val, par_bounds[i_par][0]), par_bounds[i_par][1]
)
init_pars_ranking = init_pars.copy()
init_pars_ranking[i_par] = np_val # value of current nuisance parameter
fit_results_ranking = _fit_model(
Expand Down
Loading