Using minuit to minimize chi2 and estimate errors

I am using Minuit with pyroot. I have learnt how to define a function in python and pass it to the fitting method, using ROOT::Fit::Fitter for example. Now I would like to use all the functionality provided by Minuit to minimize chi2 and estimate the best fit and error bars. I have found all the information about the TMinuit class in

https://root.cern.ch/doc/master/classTMinuit.html

Now I wondering if anyone has a tutorial or a working example I can use as a template for doing such analysis, plotting contours etc. I have googled it and surprisingly I haven’t found much material about minuit in C++.

There is the $(ROOTSYS)/tutorials/fit/lfit.C. Otherwise maybe @moneta could have more information…

Hi,

This example is for the TMinuit class. This is a low level class and we recommend to use either the ROOT::Math::Fitter class or, if you want something at lower level, the ROOT::Math::Minimiser.
Examples are also in tutorials in $(ROOTSYS)/tutorials/fit/ but in C++, it should be easy to convert them Python.
Direct links are:

Lorenzo

@moneta

I have already performed the fit using ROOT::MATH::Fitter class in python, and it works.

I think the minimizer class is something I may use. I would need specifically to draw contours of the chi2 for arbitrary values of the chi2 (Contours from minos for arbitrary sigma level), and you mentioned that I could use ROOT::Math::MinimizerOptions::SetDefaultErrorDef( value). Now it is not clear to me if I should use then the minimizer class to draw the contours or the TMinuit class. (Do I understand correctly that the classes T<Something> are considered low level?)

@bellenot I cannot find this example in my version 6.16.00

https://root.cern.ch/doc/master/Ifit_8C.html

For the contour you can use

https://root.cern.ch/doc/master/classROOT_1_1Math_1_1Minimizer.html#ab05feb0a8f256d6c217e63b7ee6a05cc

as following

TGraph g(n); 
minimizer->Contour(ipar, jpar, n, g.GetX(), g.GetY() ); 
g.Draw("AC");

@moneta I have found some sample code Numerical Minimization with ROOT::Math::Functor and I am able to run it. If I change min = ROOT.Math.Factory.CreateMinimizer("GSLMultiMin", "BFGS") to min = ROOT.Math.Factory.CreateMinimizer("Minuit2", "") I get an error:

    min.SetMaxFunctionCalls(1000000)
ReferenceError: attempt to access a null-pointer

but min = ROOT.Math.Factory.CreateMinimizer("Minuit", "") works. Is it obvious? any explanation?


EDIT: I did not install minuit2 , that’s it

@moneta I have written the following code:

chi2_contour2=13.8305
ROOT.Math.MinimizerOptions.SetDefaultErrorDef(chi2_contour2)
npoints=array.array('I',[100])
x0=np.zeros(100)
x1=np.zeros(100)    
minimum.Contour(0,1,npoints,x0, x1)
plt.figure()
plt.plot(x0,x1)
chi2_contour3=9.33246
ROOT.Math.MinimizerOptions.SetDefaultErrorDef(chi2_contour3)
x2=np.zeros(100)
x3=np.zeros(100)    
minimum.Contour(0,1,npoints,x2, x3)    
plt.plot(x2,x3)

but the resulting contours lay exactly on top of each other, so it looks like SetDefaultErrorDef is not governing this. any ideas?

I have changed ROOT.Math.MinimizerOptions.SetDefaultErrorDef(chi2_contour2) with minimum.SetErrorDef(chi2_contour2) and it produces two distinct contours (using either chi2_contour2 or chi2_contour3 ). It is not clear to me if SetErrorDef takes as input the same quantity of SetDefaultErrorDef, as I do not find any documentation, I suppose I should look into the source code then. In particular it is not clear if the input should be the value of the chi2 or the probability or some multiplicative factor of sigma


EDIT: from https://root.cern.ch/download/minuit.pdf section 3.3.8 and table 7.1 I would understand that ERRDEF is actually the UP parameter that is the difference between the chi2 I am interested in and the minimum chi2

is this correct?

I found this:
https://root.cern/doc/master/classROOT_1_1Math_1_1Minimizer.html#aa4e46f31bb8dfb770fcb704e585a3593

According to this, the ErrorDef I guess you need

ErrorDef = 0.5 * N^2

for likelihood fits, and the same without 0.5 for chi^2 fits, and N is the is the number of standard deviations (sigmas) you want to plot the contour at.