Skip to main content

Least-Squares Minimization with Bounds and Constraints

Project description

https://dev.azure.com/lmfit/lmfit-py/_apis/build/status/lmfit.lmfit-py?branchName=master https://codecov.io/gh/lmfit/lmfit-py/branch/master/graph/badge.svg https://img.shields.io/pypi/v/lmfit.svg https://img.shields.io/pypi/dm/lmfit.svg https://img.shields.io/badge/docs-read-brightgreen https://zenodo.org/badge/4185/lmfit/lmfit-py.svg

Overview

LMfit-py provides a Least-Squares Minimization routine and class with a simple, flexible approach to parameterizing a model for fitting to data.

LMfit is a pure Python package, and so easy to install from source or with pip install lmfit.

For questions, comments, and suggestions, please use the LMfit mailing list. Using the bug tracking software in GitHub Issues is encouraged for known problems and bug reports. Please read Contributing.md before creating an Issue.

Parameters and Fitting

LMfit-py provides a Least-Squares Minimization routine and class with a simple, flexible approach to parameterizing a model for fitting to data. Named Parameters can be held fixed or freely adjusted in the fit, or held between lower and upper bounds. In addition, parameters can be constrained as a simple mathematical expression of other Parameters.

To do this, the programmer defines a Parameters object, an enhanced dictionary, containing named parameters:

fit_params = Parameters()
fit_params['amp'] = Parameter(value=1.2, min=0.1, max=1000)
fit_params['cen'] = Parameter(value=40.0, vary=False)
fit_params['wid'] = Parameter(value=4, min=0)

or using the equivalent:

fit_params = Parameters()
fit_params.add('amp', value=1.2, min=0.1, max=1000)
fit_params.add('cen', value=40.0, vary=False)
fit_params.add('wid', value=4, min=0)

The programmer will also write a function to be minimized (in the least-squares sense) with its first argument being this Parameters object, and additional positional and keyword arguments as desired:

def myfunc(params, x, data, someflag=True):
    amp = params['amp'].value
    cen = params['cen'].value
    wid = params['wid'].value
    ...
    return residual_array

For each call of this function, the values for the params may have changed, subject to the bounds and constraint settings for each Parameter. The function should return the residual (i.e., data-model) array to be minimized.

The advantage here is that the function to be minimized does not have to be changed if different bounds or constraints are placed on the fitting Parameters. The fitting model (as described in myfunc) is instead written in terms of physical parameters of the system, and remains remains independent of what is actually varied in the fit. In addition, which parameters are adjusted and which are fixed happens at run-time, so that changing what is varied and what constraints are placed on the parameters can easily be modified by the user in real-time data analysis.

To perform the fit, the user calls:

result = minimize(myfunc, fit_params, args=(x, data), kws={'someflag':True}, ....)

After the fit, a MinimizerResult class is returned that holds the results the fit (e.g., fitting statistics and optimized parameters). The dictionary result.params contains the best-fit values, estimated standard deviations, and correlations with other variables in the fit.

By default, the underlying fit algorithm is the Levenberg-Marquardt algorithm with numerically-calculated derivatives from MINPACK’s lmdif function, as used by scipy.optimize.leastsq. Most other solvers that are present in scipy (e.g., Nelder-Mead, differential_evolution, basinhopping, etctera) are also supported.

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

lmfit-1.1.0.tar.gz (308.6 kB view details)

Uploaded Source

Built Distribution

lmfit-1.1.0-py3-none-any.whl (90.4 kB view details)

Uploaded Python 3

File details

Details for the file lmfit-1.1.0.tar.gz.

File metadata

  • Download URL: lmfit-1.1.0.tar.gz
  • Upload date:
  • Size: 308.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for lmfit-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a2755b708ad7bad010178da28f082f55cbee7a084a625b452632e2d77b5391fb
MD5 66d6e65f51b4b250f1ee23cb4c647d5f
BLAKE2b-256 ce778cc51e8f8397e90653688306c79cb2a91662821df613721a276313ec226c

See more details on using hashes here.

File details

Details for the file lmfit-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: lmfit-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 90.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for lmfit-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 29f0540f94b3969a23db2b51abf309f327af8ea3667443ac4cd93d07fdfdb14f
MD5 56e515db3f97b7208c7e6a05a61e71d0
BLAKE2b-256 eaab87059c989f262c88821cdc960944f0830e241c240652eef55e3eb8508e46

See more details on using hashes here.

Supported by

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