triqs.fit.fit.Fit

class triqs.fit.fit.Fit(x_array, y_array, fitter, p0=None)[source]

Bases: object

Least-squares fit of a parametric model to 1D data.

Given a model f(x, p_0, ..., p_{n-1}) and an initial guess, Fit adjusts the parameters by minimising \(\sum_i |f(x_i, \mathbf{p}) - y_i|^2\) using scipy.optimize.leastsq(). The fit is performed in __init__; the constructed object is callable and stringifies to a LaTeX expression, so it can be passed directly to triqs.plot.mpl_interface.oplot().

Parameters:
x_arrayarray-like

1D array of abscissae.

y_arrayarray-like

1D array of ordinates, same length as x_array.

fittertuple

Triple (F, name, init_value_default) where F is a callable F(x, *params) -> y acting elementwise on a numpy array, name is a printf-style template such that name % params yields a LaTeX expression, and init_value_default is a tuple of default initial values used when p0 is None. Ready-made fitters: linear, quadratic.

p0tuple, optional

Initial guess. Defaults to the third element of fitter.

Attributes

function

(callable) The model F extracted from fitter.

fname

(str) The printf-style LaTeX template extracted from fitter.

param

(numpy.ndarray) Fitted parameter values returned by scipy.optimize.leastsq.

Methods

__call__(x)

Evaluate the fitted model at x.

Examples

Linear fit of noisy data, then plotted on top of the data via oplot():

from triqs.fit import Fit, linear
from triqs.plot.mpl_interface import oplot, plt
f = Fit(x, y, linear)
oplot(f)
plt.show()