scikit-hep / iminuit

Jupyter-friendly Python interface for C++ MINUIT2
https://scikit-hep.org/iminuit
Other
283 stars 76 forks source link

Crash in visualization #966

Closed claudiocc1 closed 7 months ago

claudiocc1 commented 7 months ago

hi,

I went back to an old project. It used to work with the then-current version of iminuit. Sorry, cannnot remember the exact version. Now it crashes with iminuit 2.25 (python 3.11.5, Intel Mac.).

The fit actually works, and gives the exact same result now as it used to. I think the crash is in the visualization part, which I guess has been added since?

Here you can find my jupyter notebook, stripped down to the bare minimum (html and ipynb versions) http://hep.ucsb.edu/people/claudio/minuit/

Thanks.

Claudio

PS is there a way to turn off the visualization?

HDembinski commented 7 months ago

Thanks for reporting, that is indeed a bug. I will see how this can be fixed. A temporary workaround for you is

# Minuit fit
p0 = np.array([2., 2., 0., -1., -1.5])
mFit = LeastSquares(x, y, ey, fitFun)
mFit.visualize = lambda x, *args: None
m = Minuit(mFit, p0)  # pass starting values
m.migrad()
# m.hesse()
HDembinski commented 7 months ago

If you want to fit a smooth function $f(x)$ through your data points under the condition $f(x) \ge 0$, you can also use Bernstein polynomials. They are guaranteed to be positive inside the domain, if all the parameters are positive. You cannot safely extrapolate Bernstein polynomials outside their domain, though. If you need to query the function outside the fitted range, make the domain of the polynomial as large as you need to query it.

from numba_stats import bernstein

def model(x, p):
    domain_of_polynomial = (-0.4, 0.4)
    return bernstein.density(x, p, *domain_of_polynomial)

start = np.ones(4)  # increase this to increase flexibility of Bernstein polynomial
m = Minuit(LeastSquares(x, y, ey, model), start) 
m.limits = (0, None)  # ensure positiveness
m.migrad()
claudiocc1 commented 7 months ago

Yes I know :) We are working with people that like splines (I dont), so here we are.... C.

HDembinski commented 7 months ago

I like splines, too :). Bernstein polynomials are a special case of splines.