In [1]:
import numpy as np
import matplotlib.pyplot as plt
from iminuit import Minuit
from scipy.interpolate import CubicSpline
from iminuit.cost import LeastSquares
In [2]:
x = np.array([-0.37518513, -0.25075853, -0.15070008, -0.05177465,  0.04777233,
        0.14785866,  0.24318343,  0.3746897 ])

y = np.array([0.96236309, 0.90387022, 0.85716378, 0.81577485, 0.75851159,
       0.66316104, 0.42954885, 0.14576831])

ey = np.array([0.00588291, 0.01097116, 0.01447054, 0.01745376, 0.02217723,
       0.02828846, 0.04562499, 0.04592403])
In [3]:
def fitFun(x, p):
    xNodes = np.array([-0.4, -0.2, 0, 0.2, 0.4])
    spl = CubicSpline(xNodes, p, bc_type='natural')
    return 1/(1+np.exp(-spl(x)))
In [4]:
# Minuit fit
p0 = np.array([2., 2., 0., -1., -1.5])
mFit = LeastSquares(x, y, ey, fitFun)
m = Minuit(mFit, p0)  # pass starting values
m.migrad()
# m.hesse()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File /usr/local/lib/python3.11/site-packages/IPython/core/formatters.py:344, in BaseFormatter.__call__(self, obj)
    342     method = get_real_method(obj, self.print_method)
    343     if method is not None:
--> 344         return method()
    345     return None
    346 else:

File /usr/local/lib/python3.11/site-packages/iminuit/minuit.py:2627, in Minuit._repr_html_(self)
   2624 import io
   2626 with _TemporaryFigure(5, 4):
-> 2627     self.visualize()
   2628     with io.StringIO() as io:
   2629         plt.savefig(io, format="svg", dpi=10)

File /usr/local/lib/python3.11/site-packages/iminuit/minuit.py:1372, in Minuit.visualize(self, plot, **kwargs)
   1348 def visualize(self, plot: Callable = None, **kwargs):
   1349     """
   1350     Visualize agreement of current model with data (requires matplotlib).
   1351 
   (...)
   1370     Minuit.interactive
   1371     """
-> 1372     return self._visualize(plot)(self.values, **kwargs)

File /usr/local/lib/python3.11/site-packages/iminuit/cost.py:2234, in LeastSquares.visualize(self, args, model_points)
   2232     ym = self.model(xm, *args)
   2233 else:
-> 2234     xm, ym = _smart_sampling(lambda x: self.model(x, *args), x[0], x[-1])
   2235 plt.plot(xm, ym)

File /usr/local/lib/python3.11/site-packages/iminuit/util.py:1557, in _smart_sampling(f, xmin, xmax, start, tol, maxiter, maxtime)
   1555 t0 = monotonic()
   1556 x = np.linspace(xmin, xmax, start)
-> 1557 ynew = f(x)
   1558 ymin = np.min(ynew)
   1559 ymax = np.max(ynew)

File /usr/local/lib/python3.11/site-packages/iminuit/cost.py:2234, in LeastSquares.visualize.<locals>.<lambda>(x)
   2232     ym = self.model(xm, *args)
   2233 else:
-> 2234     xm, ym = _smart_sampling(lambda x: self.model(x, *args), x[0], x[-1])
   2235 plt.plot(xm, ym)

TypeError: fitFun() takes 2 positional arguments but 6 were given
Out[4]:
┌─────────────────────────────────────────────────────────────────────────┐
│                                Migrad                                   │
├──────────────────────────────────┬──────────────────────────────────────┤
│ FCN = 0.4533 (χ²/ndof = 0.2)     │              Nfcn = 176              │
│ EDM = 2.98e-07 (Goal: 0.0002)    │            time = 0.5 sec            │
├──────────────────────────────────┼──────────────────────────────────────┤
│          Valid Minimum           │   Below EDM threshold (goal x 10)    │
├──────────────────────────────────┼──────────────────────────────────────┤
│      No parameters at limit      │           Below call limit           │
├──────────────────────────────────┼──────────────────────────────────────┤
│             Hesse ok             │         Covariance accurate          │
└──────────────────────────────────┴──────────────────────────────────────┘
┌───┬──────┬───────────┬───────────┬────────────┬────────────┬─────────┬─────────┬───────┐
│   │ Name │   Value   │ Hesse Err │ Minos Err- │ Minos Err+ │ Limit-  │ Limit+  │ Fixed │
├───┼──────┼───────────┼───────────┼────────────┼────────────┼─────────┼─────────┼───────┤
│ 0 │ x0   │   3.44    │   0.19    │            │            │         │         │       │
│ 1 │ x1   │   1.97    │   0.10    │            │            │         │         │       │
│ 2 │ x2   │   1.34    │   0.10    │            │            │         │         │       │
│ 3 │ x3   │   0.21    │   0.12    │            │            │         │         │       │
│ 4 │ x4   │   -2.2    │    0.4    │            │            │         │         │       │
└───┴──────┴───────────┴───────────┴────────────┴────────────┴─────────┴─────────┴───────┘
┌────┬─────────────────────────────────────────┐
│    │      x0      x1      x2      x3      x4 │
├────┼─────────────────────────────────────────┤
│ x0 │  0.0354  -0.004   0.003  -0.002   0.005 │
│ x1 │  -0.004  0.0095  -0.002   0.002  -0.004 │
│ x2 │   0.003  -0.002 0.00906  -0.002   0.005 │
│ x3 │  -0.002   0.002  -0.002  0.0134  -0.002 │
│ x4 │   0.005  -0.004   0.005  -0.002   0.186 │
└────┴─────────────────────────────────────────┘
In [ ]: