Fit with the python interface to Minuit 2 called iminuit https://iminuit.readthedocs.io/en/stable/
from matplotlib import pyplot as plt
plt.rcParams["font.size"] = 20
import numpy as np
Data
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype='d')
dx = np.array([0.1,0.1,0.5,0.1,0.5,0.1,0.5,0.1,0.5,0.1], dtype='d')
y = np.array([1.1 ,2.3 ,2.7 ,3.2 ,3.1 ,2.4 ,1.7 ,1.5 ,1.5 ,1.7 ], dtype='d')
dy = np.array([0.15,0.22,0.29,0.39,0.31,0.21,0.13,0.15,0.19,0.13], dtype='d')
Define fit function
def pol3(a0, a1, a2, a3):
return a0 + x*a1 + a2*x**2 + a3*x**3
least-squares function: sum of data residuals squared
def LSQ(a0, a1, a2, a3):
return np.sum((y - pol3(a0, a1, a2, a3)) ** 2 / dy ** 2)
import Minuit object
from iminuit import Minuit
Minuit instance using LSQ function to minimize
LSQ.errordef = Minuit.LEAST_SQUARES
#LSQ.errordef = Minuit.LIKELIHOOD
m = Minuit(LSQ,a0=-1.3, a1=2.6 ,a2=-0.24 ,a3=0.005)
m.fixed["a3"] = True
m.params
run migrad
m.fixed["a3"] = False
m.params
m.migrad()
Get contour
m.draw_mncontour("a2", "a3", cl=[1, 2, 3])
Improve the fit
m.hesse()
m.minos()
access fit results
print(m.values,m.errors)
a0_fit = m.values["a0"]
a1_fit = m.values["a1"]
a2_fit = m.values["a2"]
a3_fit = m.values["a3"]
print (m.covariance)
prepare data to display fitted function
x_plot = np.linspace( 0.5, 10.5 , 500 )
y_fit = a0_fit + a1_fit * x_plot + a2_fit * x_plot**2 + a3_fit * x_plot**3
The Minos algorithm uses the profile likelihood method to compute (generally asymmetric) confidence intervals. This can be plotted
m.draw_profile("a2")
Get a 2D contour of the function around the minimum for 2 parameters
m.draw_mncontour("a2", "a3" , cl=[1, 2, 3])
lotlib
plt.figure()
plt.errorbar(x, y, dy , dx, fmt="o")
plt.plot(x_plot, y_fit)
plt.title("iminuit Fit Test")
plt.xlim(-0.1, 10.1)
plt.show()