Curve fitting with global optimization routines
Project description
curve_fit.annealing
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))
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.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c629fba3efdc77880c92f8cd3169e8906d8f41d02dd592ac33064a052d30c1b6 |
|
MD5 | ff61079cd689c5d6b79e55eb0059ea86 |
|
BLAKE2b-256 | 0cd96b202bc0fba4751d251f458e3ec0697abe5b7d6e13458f18d0df0178e495 |
Hashes for curve_fit.annealing-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a3fd765f301b756522c5e29e5071f6ab5b8251329a31c711dbdf3f7d4f2e416 |
|
MD5 | 53902e35ed3b2498450cb437a99a196d |
|
BLAKE2b-256 | 48ef1b7d6ae6f4173c251401fc7d4d662880f8844ca59e7c6228ef434c47e7a8 |