Tool for general non-linear fitting based on the Marquardt algorithm. To setup a fit you need to:

1. define parameters and assign them reasonable initial values
2. define a fitting function in terms of the parameters
3. define the fit and carry it out

Depending on the outcome, one might need to change the initial values of the parameters or decide to change which parameters are fixed and which are to be fitted.

  1. Setting up the parameters:

    >>> import LT_Fit.parameters as P  # get the parameter module
    >>> import LT_fit.gen_fit as G     # load the genfit module
    >>> a = P.Parameter(1., 'amp')     # parameter a, called 'amp', initialized to 1. 
    >>> b = P.Parameter(3., 'omega')   # parameter b, called 'omega', intialized to 3.
    >>> c = P.Parameter(1.5, 'phase')  # parameter c, called 'phase', initialize to 1.5
  2. Define the fitting function:

    >>> def f(x):
    ....    return a()*sin(b() * x + c() )
  3. Now do the fit:

    >>> F = G.genfit( f, [a, b, c], x = xval, y = y_exp) #  the list [a,b,c] defines which parameters are fitted
    >>> F = G.genfit( f, [a], x = xval, y = y_exp, yerr = sigy) #  only a is fitted, but take the exp. error into account
    >>> F = G.genfit( f, [a, c], x = xval, y = y_exp, yerr = sigy) #  a and c are fitted b is kept constant

To change the initial values and repeat the fit:

>>> a.set(1.5)
>>> b.set(3.2)
>>> c.set(2.0)
>>> F = G.genfit( f, [a, b, c], x = xval, y = y_exp)

Finally plot the data and the fit:

>>> import as B
>>> B.plot_exp(xval, y_exp, sigy)   # plot the data with errorbars
>>> F.plot()                        # plot the fitted function as a line    
>>> B.plot_line(F.xpl, F.ypl)       # old version of plotting the fitted function as a line
>>> F(x)                            # evaluate the fitted function at x
>>> show()                          # show the new plot

class LT_Fit.gen_fit.genfit(function, parameters, x=None, y=None, y_err=None, nplot=100, full_output=1, ftol=0.001, print_results=True, plot_fit=True, **kwargs)

general non-linear fit based on the Marquardt algorithm

Important keywords:




(numpy.array()) array of experimental values (mandatory)


(numpy.array()) array of independent variables


(numpy.array()) array of errors


number of points to be used for plotting the fit


minimal change in chi square to determine if the fit has converged


additional keywords are passes to scipy.optimize.least_squares, examples below


an array for upper and lower bounds for the parameter


alternative loss function to handle outliers e.g. ‘huber’


outliers with a residual more the f_scale should be not affect the result


(default True) plot the fitted function automatically

Additional keyword arguments are passed on to scipy.optimize.leastsq()


use the saved parameters

plot(xv=None, **kwargs)

Plot the fitting function

xvTYPE, optional

x-values for which the fitting function should be plotted. The default is None i.e. using the standard xpl and ypl values.


keyword aguments passed to matplotlib plot function


make a deep copy of the current parameters to be saved


set the array of values x

set_yval(y, y_err=None)

set the array of values to be fitted


show the fitted parameters