\[\DeclareMathOperator{\erf}{erf}
\DeclareMathOperator{\argmin}{argmin}
\newcommand{\R}{\mathbb{R}}
\newcommand{\n}{\boldsymbol{n}}\]
Module pyqt_fit.curve_fitting
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:
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