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

Uploaded Source

Built Distributions

lmfit-0.6.win32-py3.2.exe (221.0 kB view details)

Uploaded Source

lmfit-0.6.win32-py2.7.exe (221.0 kB view details)

Uploaded Source

lmfit-0.6.win32-py2.6.exe (221.0 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for lmfit-0.6.tar.gz
Algorithm Hash digest
SHA256 a3ba7c9c944d45651cc24d215b400f00662903916d3277d1f5e41e0a0da8bbbb
MD5 9ff5427b44f1a5cd387c1789e9a76d41
BLAKE2b-256 dc034b44291b0fc4df38d4223a0131912bac82ae80a2d691d3ac0bb6a43a113d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lmfit-0.6.win32-py3.2.exe
  • Upload date:
  • Size: 221.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for lmfit-0.6.win32-py3.2.exe
Algorithm Hash digest
SHA256 8cc2a5cbdf7fcb1bf4954d01b71b7592cb5628b17629a572dbf1650a935dc3cf
MD5 9b7caf3e8296ad5c5b02ffb2e11dce7c
BLAKE2b-256 80458d6bf346a5109e8bf4fc72f9f623b4f8734751da24c0e41c6ccd6012fed8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lmfit-0.6.win32-py2.7.exe
  • Upload date:
  • Size: 221.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for lmfit-0.6.win32-py2.7.exe
Algorithm Hash digest
SHA256 3458cbd74e2621c3e31db9ea984a587e46e7e85d2da46ee535dadab50749681c
MD5 4f37dff19713a2a0f5ee6954e58d3201
BLAKE2b-256 71dd60f88154ab344a7ce925b39384c5310cb666395c591cda82d27d50baba3b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lmfit-0.6.win32-py2.6.exe
  • Upload date:
  • Size: 221.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for lmfit-0.6.win32-py2.6.exe
Algorithm Hash digest
SHA256 b354c21ccfe42bbbcfb122c8ef640c5f6a57e02afcc2c5a5ac8104dbb44db956
MD5 9fd992f3da8ef86655586ee0ad5da6a8
BLAKE2b-256 d35ee0c35f2c95c5d8c36bdea957c60db7bb6b51a49e4b2882c668d8c84fb97b

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