Least-Squares Minimization with Bounds and Constraints
Project description
Overview
The lmfit Python library supports provides tools for non-linear least-squares minimization and curve fitting. The goal is to make these optimization algorithms more flexible, more comprehensible, and easier to use well, with the key feature of casting variables in minimization and fitting routines as named parameters that can have many attributes beside just a current value.
LMfit is a pure Python package, built on top of Scipy and Numpy, and so easy to install with pip install lmfit.
For questions, comments, and suggestions, please use the LMfit google mailing list or Github discussions. For software issues and bugs, use Github Issues, but please read Contributing.md before creating an Issue.
Parameters and Minimization
LMfit provides optimization routines similar to (and based on) those from scipy.optimize, but with a simple, flexible approach to parameterizing a model for fitting to data using named parameters. These named Parameters can be held fixed or freely adjusted in the fit, or held between lower and upper bounds. Parameters can also be constrained as a simple mathematical expression of other Parameters.
A Parameters object (which acts like a Python dictionary) contains named parameters, and can be built as with:
import lmfit fit_params = lmfit.Parameters() fit_params['amp'] = lmfit.Parameter(value=1.2) fit_params['cen'] = lmfit.Parameter(value=40.0, vary=False) fit_params['wid'] = lmfit.Parameter(value=4, min=0) fit_params['fwhm'] = lmfit.Parameter(expr='wid*2.355')
or using the equivalent:
fit_params = lmfit.create_params(amp=1.2, cen={'value':40, 'vary':False}, wid={'value': 4, 'min':0}, fwhm={'expr': 'wid*2.355'})
In the general minimization case (see below for Curve-fitting), the user will also write an objective 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 = lmfit.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, basin-hopping, and more) are also supported.
Curve-Fitting with lmfit.Model
One of the most common use of least-squares minimization is for curve fitting, where minimization of data-model, or (data-model)*weights. Using lmfit.minimize as above, the objective function would take data and weights and effectively calculated the model and then return the value of (data-model)*weights.
To simplify this, and make curve-fitting more flexible, lmfit provides a Model class that wraps a model function that represents the model (without the data or weights). Parameters are then automatically found from the named arguments of the model function. In addition, simple model functions can be readily combined and reused, and several common model functions are included in lmfit.
Exploration of Confidence Intervals
Lmfit tries to always estimate uncertainties in fitting parameters and correlations between them. It does this even for those methods where the corresponding scipy.optimize routines do not estimate uncertainties. Lmfit also provides methods to explicitly explore and evaluate the confidence intervals in fit results.
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
File details
Details for the file lmfit-1.3.2.tar.gz
.
File metadata
- Download URL: lmfit-1.3.2.tar.gz
- Upload date:
- Size: 623.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31beeae1f027c1b8c14dcd7f2e8488a80b75fb389e77fca677549bdc2fe597bb |
|
MD5 | 214b4eada76c06b6209df54462e3894e |
|
BLAKE2b-256 | ade0e6ced7c4f7c3e7aa64057d6482bcfc798fbe731fb3226918769b7b76aba1 |
File details
Details for the file lmfit-1.3.2-py3-none-any.whl
.
File metadata
- Download URL: lmfit-1.3.2-py3-none-any.whl
- Upload date:
- Size: 98.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b834f054cd7a5172f3b431345b292e5d95ea387d6f96d60ad35a11b8efee6ac |
|
MD5 | 3f8b4824c518a88450f63b0d1bf81b9d |
|
BLAKE2b-256 | 1c3b594a8d453a1970ca20d0a39b310882279a9396285f45f89b08e8f6b60e1f |