scikit-hep / pyhf

pure-Python HistFactory implementation with tensors and autodiff
https://pyhf.readthedocs.io/
Apache License 2.0
283 stars 83 forks source link

Implementation of fixed params in scipy minimizer #2439

Open andrewfowlie opened 8 months ago

andrewfowlie commented 8 months ago

Summary

I'm using some fixed parameters in a pyhf model and scipy optimization. I noticed that fixed parameters are implemented using equality constraints. Only two minimization methods support equality constraints: 'SLSQP' (the default) and ''trust-constr'.

If you use a method that does not support them, you only get a warning, e.g., `RuntimeWarning: Method TNC cannot handle constraints.' And the minimization doesn't respect the constraints, i.e., the fixed parameters are not fixed. This wasn't obvious to me as a user.

Can I suggest that if there are fixed params, and method that does not support constraints is attempted, it throws an exception?

I wonder, though, if we could do a more general way of implementing fixed parameters. Something like:

def minimize_with_fixed_param_at_0(func, fixed_param, *args, **kwargs):
    def wrapped(x):
        y = [fixed_param] + x
        return func(y)
    return minimize(wrapped, *args, **kwargs)

It needs more sophisticated logic to combine the fixed params with the unfixed ones (here I just assume the fixed parameter is at index 0). This would not rely on methods that support equality constraints.

Additional Information

Code of Conduct