erlab.analysis.fit.minuit

Classes

Iterable()

LeastSq(x, y, yerror, model, *[, loss, ...])

A thin wrapper around iminuit.cost.LeastSquares that produces better plots.

Minuit(fcn, *args[, grad, g2, hessian, name])

iminuit.Minuit with additional functionality.

Sequence()

All the operations on a read-only sequence.

class erlab.analysis.fit.minuit.LeastSq(x, y, yerror, model, *, loss='linear', verbose=0, grad=None, name=None)[source]

Bases: LeastSquares

A thin wrapper around iminuit.cost.LeastSquares that produces better plots.

visualize(args, model_points=0)[source]

Visualize data and model agreement (requires matplotlib).

The visualization is drawn with matplotlib.pyplot into the current axes.

Parameters:
  • args (array-like) – Parameter values.

  • model_points (int or array-like, optional) – How many points to use to draw the model. Default is 0, in this case an smart sampling algorithm selects the number of points. If array-like, it is interpreted as the point locations.

class erlab.analysis.fit.minuit.Minuit(fcn, *args, grad=None, g2=None, hessian=None, name=None, **kwds)[source]

Bases: Minuit

iminuit.Minuit with additional functionality.

This class extends the functionality of the iminuit.Minuit class by providing a convenient method from_lmfit to initialize the Minuit object from an lmfit.Model object.

For more information on the iminuit library, see its documentation.

Examples

>>> import lmfit.models
>>> import numpy as np
>>> from erlab.analysis.fit.minuit import Minuit
>>> # Create an lmfit.Model object
>>> model = lmfit.models.LinearModel()
>>> # Generate some data
>>> x = np.linspace(0, 10, 100)
>>> y = model.eval(x=x, a=2, b=1)
>>> rng = np.random.default_rng(1)
>>> y = rng.normal(y, 0.5)
>>> # Initialize a Minuit object from the lmfit.Model object
>>> m = Minuit.from_lmfit(model, y, x)
>>> # Perform the fit
>>> m.migrad()
classmethod from_lmfit(model, data, ivars, yerr=None, return_cost=False, params=None, **kwargs)[source]

Create an iminuit fit from an lmfit model.

Parameters:
  • model (Model) – Lmfit model to evaluate. Parameter values, fixed states, and finite bounds are transferred to the returned minimizer.

  • data (ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]] | DataArray) – Dependent data values.

  • ivars (ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]] | DataArray | Sequence[ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]] | DataArray]) – Independent-variable values in the order given by model.independent_vars. A single array is accepted when the model has one independent variable.

  • yerr (float | ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]] | None, default: None) – Standard uncertainty of data, as a scalar or an array broadcastable to it. If omitted, every uncertainty is one in the units of data.

  • return_cost (bool, default: False) – If True, return the constructed LeastSq cost function together with the minimizer.

  • params (default: None) – Lmfit parameters to transfer. If omitted, call model.guess or, for a model without a guess method, model.make_params.

  • **kwargs – Initial parameter values or dictionaries of arguments for lmfit.Parameter.set(). These overrides are applied only when params is omitted.

Returns:

  • minimizer (Minuit) – Configured iminuit minimizer.

  • cost, minimizer (tuple of LeastSq and Minuit) – Cost function and minimizer when return_cost is True.

Raises:

ValueError – If the number of independent-variable arrays does not match the model, or if a parameter that varies is constrained by an lmfit expression. Fixed expression-constrained parameters are omitted from the iminuit parameter list.

Return type:

Minuit | tuple[LeastSq, Minuit]