Skip to main content

Least-Squares Minimization with Constraints for Python

Project description

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 (ie, 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 adjuested 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 consumer 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, each real variable in the fit_params dictionary is updated to have best-fit values, estimated standard deviations, and correlations with other variables in the fit, while the results dictionary holds fit statistics and information.

By default, the underlying fit algorithm is the Levenberg-Marquart algorithm with numerically-calculated derivatives from MINPACK’s lmdif function, as used by scipy.optimize.leastsq. Other solvers (currently Simulated Annealing and L-BFGS-B) are also available, though slightly less well-tested and 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-0.5.tar.gz (282.9 kB view details)

Uploaded Source

Built Distributions

lmfit-0.5.win32-py3.2.exe (219.7 kB view details)

Uploaded Source

lmfit-0.5.win32-py2.7.exe (219.7 kB view details)

Uploaded Source

lmfit-0.5.win32-py2.6.exe (219.7 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: lmfit-0.5.tar.gz
  • Upload date:
  • Size: 282.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for lmfit-0.5.tar.gz
Algorithm Hash digest
SHA256 c546c4365fe04f87b8d9fb7ccea5890ee37ebc289ac428c0e4d04a1a142eac5d
MD5 4642c5e05ba4c422377737e873314697
BLAKE2b-256 cceb08efa9ffa9eee78e68f30af607a5eb3ff005643867345169e197ce88dcd7

See more details on using hashes here.

File details

Details for the file lmfit-0.5.win32-py3.2.exe.

File metadata

File hashes

Hashes for lmfit-0.5.win32-py3.2.exe
Algorithm Hash digest
SHA256 6087973047a1c67c17968924d9ccd3e0b5d8d0f4064ca0b5e50477bcb5077b47
MD5 562d4f01122de3887b45b37c6f3fc413
BLAKE2b-256 676854943c43e6aa76d71f2ce6ae1ea107b4ef9ec572713621bf7f6ae76dcfe8

See more details on using hashes here.

File details

Details for the file lmfit-0.5.win32-py2.7.exe.

File metadata

File hashes

Hashes for lmfit-0.5.win32-py2.7.exe
Algorithm Hash digest
SHA256 dd8dd0eeaca2279256546842fa0138a35cf51543b9abd672713bbe7e46758707
MD5 400b7df98f0612da47eeafdefdc0dcb6
BLAKE2b-256 69a10fc5d44cca8164d51982c9ceb9ad11d16066a4e8124c26490fe595e33ecf

See more details on using hashes here.

File details

Details for the file lmfit-0.5.win32-py2.6.exe.

File metadata

File hashes

Hashes for lmfit-0.5.win32-py2.6.exe
Algorithm Hash digest
SHA256 199f5c2ae95a8ffef702e0928165e0a135d5e9719e8809dc9ff47f8635c80f08
MD5 a1597b00cbf33abcc4cfab4ac2e9d99d
BLAKE2b-256 757e2f773cde66d0a0e6f87b831e753f57a1ee3aca1e167eb50d4d3f8e5e32fe

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