Curve fitting with global optimization routines
Project description
Most curve fitting algorithms rely on local optimization routines. These demand good estimates of the fit parameters.
Instead, this module allows to use global optimization routines of scipy.optimize to minimize the squared deviation function.
Installation
This module can be installed from PyPI
pip3 install curve_fit.annealing
Example
Let us fit a beat signal with two sinus functions, with a total of 6 free parameters.
By default, the curve_fit function of this module will use the scipy.optimize.dual_annealing method to find the global optimum of the curve fitting problem. The dual annealing algorithm requires bounds for the fitting parameters. Other global optimization methods like scipy.optimize.basinhopping require an initial guess of the parameters instead.
import numpy as np from matplotlib import pyplot as plt from curve_fit import annealing def f(x,p): # Sum of two sinus functions return p[0]*np.sin(p[1]*x + p[2]) + p[3]*np.sin(p[4]*x+p[5]) xdata = np.linspace(-100,100,1000) ydata = f(xdata, [1, 1, 0, 1, 0.9, 0]) plt.plot(xdata, ydata, label='data') bounds=[[0,2],[0,2],[0,2*np.pi],[0,2],[0,2],[0,2*np.pi]] result = annealing.curve_fit(f, xdata, ydata, bounds=bounds) p_opt = result.x # optimal fit parameters ydata_res = f(xdata, p_opt) plt.plot(xdata, ydata_res, label='fit') plt.legend() plt.grid() plt.show()
Or use scipy.optimize.basinhopping
result = annealing.curve_fit(f, xdata, ydata, method='basinhopping', x0=np.zeros(6))
API
curve_fit(f, xdata, ydata, [method='dual_annealing', args, kwargs])
Fit function f to data with selectable optimization method from scipy.optimize.
- Parameters:
- f: callable
The model function, f(xdata, p). The second argument holds the fitting parameters.
- xdataarray_like or object
The independent variable where the data is measured. Should usually be an M-length sequence or an (k,M)-shaped array for functions with k predictors, but can actually be any object.
- ydataarray_like
The dependent data, a length M array - nominally f(xdata, ...).
- methodstr
scipy.optimize method to use for non-linear least squares minimization. Default is ‘dual_annealing’.
- args, kwargstuple and dict, optional
Additional arguments passed to the optimization method.
- Returns:
Return OptimizeResult object. The x attribute holds the fitting parameters.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for curve_fit.annealing-0.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9bbb9db7f89c98717db0434547f7d98d343cece3e2697f91eee4fcbe0923a78 |
|
MD5 | 5182efc08edf1ab52802e1a866fe264b |
|
BLAKE2b-256 | 7320c52a733ced5a83fd8f57ba36ed64d0518e7e119f845e15b87aa34c02dc47 |
Hashes for curve_fit.annealing-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 891d71d22b5e420a19e0e13442243627c5ec9599f03727fc852c14226a16505d |
|
MD5 | 2408c8ade0dc6db10b976cdf5275d29e |
|
BLAKE2b-256 | 06554d170342aa219342ad97dc63e804abb85eb718191a4070f166bb3be26cec |