Hi,
I am trying to use ROOT::Fit::Fitter
to perform a simple linear regression.
Here is how I set up the Fitter:
fitter = ROOT.Fit.Fitter()
func = ROOT.TF1("f1", "pol1", 0, 130)
fitter.SetFunction(ROOT.Math.WrappedMultiTF1(func, func.GetNdim()), False)
fitter.Config().SetMinimizer("Minuit2");
After this, I add the data in the BinData type:
x = np.array([117.5, 97.5, 87.5, 77.5])
x_err = np.array([2.5, 2.5, 2.5, 2.5])
y = np.array([-12.5, -75, -102.5, -127.5])
y_err = np.array([2.5, 2.5, 2.5, 2.5])
bin_data = ROOT.Fit.BinData(len(x), x, y, x_err, y_err)
Then, use Fit
function to perform the fit:
fitter.Config().ParSettings(0).SetValue(0.)
fitter.Config().ParSettings(1).SetValue(1.)
is_ok = fitter.Fit(bin_data)
print(f"is ok: {is_ok}")
res = fitter.Result()
print(f"slope: {res.Parameter(1)}")
print(f"offset: {res.Parameter(0)}")
print(f"pvalue: {res.Prob()}")
Here is the result:
is ok: True
slope: -215605.28611662454
offset: 20481809.40923477
pvalue: 3.967978809222436e-31
In the meanwhile, I also use a python library to do the linear fitting:
res = stats.linregress(x, y)
print(f"slope: {res.slope}")
print(f"offset: {res.intercept}")
print(f"pvalue: {res.pvalue}")
With python linear regression, I got a satisfying result:
slope: 2.892857142857143
offset: -354.19642857142856
pvalue: 0.0013385665091588672
Why did ROOT Fitter algorithm failed to fit the parameters? Are there additional settings I should add?
Thanks for your attention
Please read tips for efficient and successful posting and posting code
ROOT Version: 6.30.08
Platform: Fedora 40
Compiler: gcc