Non-Linear Least-Squares Minimization and Curve-Fitting for Python

Non-Linear Least-Squares Minimization and Curve-Fitting for Python

Release 0.9.12

Matthew Newville, Till Stensitzki, and others

Dec 13, 2018

Contents

1 Getting started with Non-Linear Least-Squares Fitting

3

2 Downloading and Installation

7

2.1 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2.2 Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2.3 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2.4 Development Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.5 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.6 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.7 Copyright, Licensing, and Re-distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3 Getting Help

13

4 Frequently Asked Questions

15

4.1 What's the best way to ask for help or submit a bug report? . . . . . . . . . . . . . . . . . . . . . . . 15

4.2 Why did my script break when upgrading from lmfit 0.8.3 to 0.9.0? . . . . . . . . . . . . . . . . . . 15

4.3 I get import errors from IPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4.4 How can I fit multi-dimensional data? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4.5 How can I fit multiple data sets? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

4.6 How can I fit complex data? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

4.7 Can I constrain values to have integer values? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

4.8 How should I cite LMFIT? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

4.9 I get errors from NaN in my fit. What can I do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

5 Parameter and Parameters

19

5.1 The Parameter class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

5.2 The Parameters class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

5.3 Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

6 Performing Fits and Analyzing Outputs

27

6.1 The minimize() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

6.2 Writing a Fitting Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

6.3 Choosing Different Fitting Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

6.4 MinimizerResult ? the optimization result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

6.5 Using a Iteration Callback Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

6.6 Using the Minimizer class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

6.7 Minimizer.emcee() - calculating the posterior probability distribution of parameters . . . . . . 44

i

6.8 Getting and Printing Fit Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

7 Modeling Data and Curve Fitting

51

7.1 Motivation and simple example: Fit data to Gaussian profile . . . . . . . . . . . . . . . . . . . . . . 51

7.2 The Model class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

7.3 The ModelResult class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

7.4 Composite Models : adding (or multiplying) Models . . . . . . . . . . . . . . . . . . . . . . . . . . 74

8 Built-in Fitting Models in the models module

81

8.1 Peak-like models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

8.2 Linear and Polynomial Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

8.3 Step-like models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

8.4 Exponential and Power law models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

8.5 User-defined Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

8.6 Example 1: Fit Peak data to Gaussian, Lorentzian, and Voigt profiles . . . . . . . . . . . . . . . . . 100

8.7 Example 2: Fit data to a Composite Model with pre-defined models . . . . . . . . . . . . . . . . . . 103

8.8 Example 3: Fitting Multiple Peaks ? and using Prefixes . . . . . . . . . . . . . . . . . . . . . . . . 105

9 Calculation of confidence intervals

109

9.1 Method used for calculating confidence intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

9.2 A basic example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

9.3 An advanced example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

9.4 Confidence Interval Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

10 Bounds Implementation

117

11 Using Mathematical Constraints

119

11.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

11.2 Supported Operators, Functions, and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

11.3 Using Inequality Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

11.4 Advanced usage of Expressions in lmfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

12 Release Notes

123

12.1 Version 0.9.12 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

12.2 Version 0.9.10 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

12.3 Version 0.9.9 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

12.4 Version 0.9.6 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

12.5 Version 0.9.5 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

12.6 Version 0.9.4 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

12.7 Version 0.9.3 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

12.8 Version 0.9.0 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

Python Module Index

127

ii

Non-Linear Least-Squares Minimization and Curve-Fitting for Python, Release 0.9.12

Lmfit provides a high-level interface to non-linear optimization and curve fitting problems for Python. It builds on and extends many of the optimization methods of scipy.optimize. Initially inspired by (and named for) extending the Levenberg-Marquardt method from scipy.optimize.leastsq, lmfit now provides a number of useful enhancements to optimization and data fitting problems, including:

? Using Parameter objects instead of plain floats as variables. A Parameter has a value that can be varied during the fit or kept at a fixed value. It can have upper and/or lower bounds. A Parameter can even have a value that is constrained by an algebraic expression of other Parameter values. As a Python object, a Parameter can also have attributes such as a standard error, after a fit that can estimate uncertainties.

? Ease of changing fitting algorithms. Once a fitting model is set up, one can change the fitting algorithm used to find the optimal solution without changing the objective function.

? Improved estimation of confidence intervals. While scipy.optimize.leastsq will automatically calculate uncertainties and correlations from the covariance matrix, the accuracy of these estimates is sometimes questionable. To help address this, lmfit has functions to explicitly explore parameter space and determine confidence levels even for the most difficult cases.

? Improved curve-fitting with the Model class. This extends the capabilities of scipy.optimize.curve_fit, allowing you to turn a function that models your data into a Python class that helps you parametrize and fit data with that model.

? Many built-in models for common lineshapes are included and ready to use.

The lmfit package is Free software, using an Open Source license. The software and this document are works in progress. If you are interested in participating in this effort please use the lmfit GitHub repository.

Contents

1

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download