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: create a warning when fitted parameter is at the bounds #2526

Draft
wants to merge 2 commits into
base: main
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
24 changes: 22 additions & 2 deletions src/pyhf/optimize/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,32 @@
raise exceptions.FailedMinimization(result)
return result

def _internal_postprocess(self, fitresult, stitch_pars, return_uncertainties=False):
def _internal_postprocess(
self, fitresult, stitch_pars, /, par_bounds, *, return_uncertainties=False
):
"""
Post-process the fit result.

Args:
fitresult (scipy.optimize.OptimizeResult): Fit result from :func:`_internal_minimize`
stitch_pars (:obj:`func`): callable that stitches fixed parameters into the unfixed parameters
par_bounds (:obj:`list` of :obj:`list`/:obj:`tuple`): The extrema of values the model parameters
are allowed to reach in the fit.
The shape should be ``(n, 2)`` for ``n`` model parameters.
return_uncertainties (:obj:`bool`): Return uncertainties on the fitted parameters. Default is off (``False``).

Returns:
fitresult (scipy.optimize.OptimizeResult): A modified version of the fit result.
"""
tensorlib, _ = get_backend()

# TODO: check how to handle this for batching

Check notice on line 89 in src/pyhf/optimize/mixins.py

View check run for this annotation

codefactor.io / CodeFactor

src/pyhf/optimize/mixins.py#L89

Unresolved comment '# TODO: check how to handle this for batching'. (C100)
# TODO: handle skipping fixed parameters

Check notice on line 90 in src/pyhf/optimize/mixins.py

View check run for this annotation

codefactor.io / CodeFactor

src/pyhf/optimize/mixins.py#L90

Unresolved comment '# TODO: handle skipping fixed parameters'. (C100)
# TODO: handle various backends

Check notice on line 91 in src/pyhf/optimize/mixins.py

View check run for this annotation

codefactor.io / CodeFactor

src/pyhf/optimize/mixins.py#L91

Unresolved comment '# TODO: handle various backends'. (C100)
for par_index, (fitted_par, bound) in enumerate(zip(fitresult.x, par_bounds)):
if fitted_par in bound:
log.warning(f'parameter at index {par_index} is at the bounds')

# stitch in missing parameters (e.g. fixed parameters)
fitted_pars = stitch_pars(tensorlib.astensor(fitresult.x))

Expand Down Expand Up @@ -195,7 +212,10 @@
**minimizer_kwargs, options=kwargs, par_names=par_names
)
result = self._internal_postprocess(
result, stitch_pars, return_uncertainties=return_uncertainties
result,
stitch_pars,
par_bounds=minimizer_kwargs['bounds'],
return_uncertainties=return_uncertainties,
)

_returns = [result.x]
Expand Down
Loading