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.2.0.tar.gz (321.4 kB view details)

Uploaded Source

Built Distribution

lmfit-1.2.0-py3-none-any.whl (93.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lmfit-1.2.0.tar.gz
  • Upload date:
  • Size: 321.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for lmfit-1.2.0.tar.gz
Algorithm Hash digest
SHA256 bc1bb6ea1dfea1db0489e400ca6b722f261f12a324a703b6e614877ca6ed0d38
MD5 68ef41759292db7bdd776dac602bd879
BLAKE2b-256 884e25dbc02731f1b046861529b87e6ae4657c3e8f8a8543bae9d12b56a02bb3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lmfit-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 93.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for lmfit-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2423791960bd2f32d1009649e33ce61ed00e5e537f1ace3b93be14c07f028b7
MD5 24d46f5602c83c7c73648e3afee487ba
BLAKE2b-256 8eeea536f1b7ace261e628d51ac4bbd0de91b5f4011d7ad084de9aa96f37c179

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