Skip to main content

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

.. highlight:: none

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.

.. highlight:: python

::

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,2np.pi],[0,2],[0,2],[0,2np.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. xdata : array_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. ydata : array_like The dependent data, a length M array - nominally f(xdata, ...). method : str scipy.optimize method to use for non-linear least squares minimization. Default is 'dual_annealing'. args, kwargs : tuple and dict, optional Additional arguments passed to the optimization method.

Returns: Return OptimizeResult object. The x attribute holds the fitting parameters.

.. _scipy.optimize: https://docs.scipy.org/doc/scipy/reference/optimize.html .. _scipy.optimize.dual_annealing: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.dual_annealing.html#scipy.optimize.dual_annealing .. _scipy.optimize.basinhopping: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.basinhopping.html#scipy.optimize.basinhopping

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

curve_fit.annealing-0.0.2.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

curve_fit.annealing-0.0.2-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file curve_fit.annealing-0.0.2.tar.gz.

File metadata

  • Download URL: curve_fit.annealing-0.0.2.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.5.3

File hashes

Hashes for curve_fit.annealing-0.0.2.tar.gz
Algorithm Hash digest
SHA256 907f42ed43698b08ec4c834f75618fb628582d9a6bad5f6f2098e9f00d8aacdf
MD5 5b94c7bae312afdb4c53c877a44f4977
BLAKE2b-256 fc8c96ee8d0c06e14f1e5bf83058bc0a736ecc6fa072cde38522bfbbf4491b5e

See more details on using hashes here.

File details

Details for the file curve_fit.annealing-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: curve_fit.annealing-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.5.3

File hashes

Hashes for curve_fit.annealing-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 73e8cd870ffcd05d0201c3d6987aeb25d7777efb6204b845f24455ca0c35aa20
MD5 35c67be60d08a90ef771756aabddcfff
BLAKE2b-256 d0d2363c2f82ca926e8eb3a85a64558f95dd4b77dff1b77de826cdc90cd1b51d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page