\[\DeclareMathOperator{\erf}{erf} \DeclareMathOperator{\argmin}{argmin} \newcommand{\R}{\mathbb{R}} \newcommand{\n}{\boldsymbol{n}}\]

Module pyqt_fit.curve_fitting

Author:Pierre Barbier de Reuille <pierre.barbierdereuille@gmail.com>

This module specifically implement the curve fitting, wrapping the default scipy.optimize.leastsq function. It allows for parameter value fixing, different kind of residual and added constraints function.

The main class of the module is the CurveFitting class.

class pyqt_fit.curve_fitting.CurveFitting(xdata, ydata, p0, fct, args=(), residuals=None, fix_params=(), Dfun=None, Dres=None, col_deriv=1, constraints=None, *lsq_args, **lsq_kword)[source]

Fit a curve using the scipy.optimize.leastsq() function

Parameters:
  • xdata (ndarray) – Explaining values
  • ydata (ndarray) – Target values
  • p0 (tuple) – Initial estimates for the parameters of fct
  • fct (callable) – Function to optimize. The call will be equivalent to fct(p0, xdata, *args)
  • args (tuple) – Additional arguments for the function
  • residuals (callable or None) – Function computing the residuals. The call is equivalent to residuals(y, fct(x)) and it should return a ndarray. If None, residuals are simply the difference between the computed and expected values.
  • fix_params (tuple of int) – List of indices for the parameters in p0 that shouldn’t change
  • Dfun (callable) – Function computing the jacobian of fct w.r.t. the parameters. The call will be equivalent to Dfun(p0, xdata, *args)
  • Dres (callable) – Function computing the jacobian of the residuals w.r.t. the parameters. The call will be equivalent to Dres(y, fct(x), DFun(x)). If None, residuals must also be None.
  • col_deriv (int) – Define if Dfun returns the derivatives by row or column. With n = len(xdata) and m = len(p0), the shape of output of Dfun must be (n,m) if 0, and (m,n) if non-0.
  • constraints (callable) – If not None, this is a function that should always return a list ofvalues (the same), to add penalties for bad parameters. The function call is equivalent to: constraints(p0)
  • lsq_args (tuple) – List of unnamed arguments passed to optimize.leastsq, starting with ftol
  • lsq_kword (dict) – Dictionnary of named arguments passed to py:func:scipy.optimize.leastsq, starting with ftol

Once constructed, the following variables contain the result of the fitting:

Variables:
  • popt (ndarray) – The solution (or the result of the last iteration for an unsuccessful call)
  • pcov (ndarray) – The estimated covariance of popt. The diagonals provide the variance of the parameter estimate.
  • res (ndarray) – Final residuals
  • infodict (dict) –

    a dictionary of outputs with the keys:

    nfev
    the number of function calls
    fvec
    the function evaluated at the output
    fjac
    A permutation of the R matrix of a QR factorization of the final approximate Jacobian matrix, stored column wise. Together with ipvt, the covariance of the estimate can be approximated.
    ipvt
    an integer array of length N which defines a permutation matrix, p, such that fjac*p = q*r, where r is upper triangular with diagonal elements of nonincreasing magnitude. Column j of p is column ipvt(j) of the identity matrix.
    qtf
    the vector (transpose(q) * fvec)
    CI
    list of tuple of parameters, each being the lower and upper bounds for the confidence interval in the CI argument at the same position.
    est_jacobian
    True if the jacobian is estimated, false if the user-provided functions have been used

Note

In this implementation, residuals are supposed to be a generalisation of the notion of difference. In the end, the mathematical expression of this minimisation is:

\[\hat{\theta} = \argmin_{\theta\in \mathbb{R}^p} \sum_i r(y_i, f(\theta, x_i))^2\]

Where \(\theta\) is the vector of \(p\) parameters to optimise, \(r\) is the residual function and \(f\) is the function being fitted.

__call__(xdata)[source]

Return the value of the fitted function for each of the points in xdata

Previous topic

Module pyqt_fit.plot_fit

Next topic

Module pyqt_fit.bootstrap

This Page