Non-Linear Least-Squares Minimization and Curve-Fitting for Python Release 0.9.4-Dirty

Total Page:16

File Type:pdf, Size:1020Kb

Non-Linear Least-Squares Minimization and Curve-Fitting for Python Release 0.9.4-Dirty Non-Linear Least-Squares Minimization and Curve-Fitting for Python Release 0.9.4-dirty Matthew Newville, Till Stensitzki, and others July 26, 2016 Contents 1 Getting started with Non-Linear Least-Squares Fitting3 2 Downloading and Installation7 2.1 Prerequisites...............................................7 2.2 Downloads................................................7 2.3 Installation................................................7 2.4 Development Version..........................................7 2.5 Testing..................................................8 2.6 Acknowledgements...........................................8 2.7 License..................................................8 3 Getting Help 11 4 Frequently Asked Questions 13 4.1 What’s the best way to ask for help or submit a bug report?....................... 13 4.2 Why did my script break when upgrading from lmfit 0.8.3 to 0.9.0?.................. 13 4.3 I get import errors from IPython..................................... 13 4.4 How can I fit multi-dimensional data?.................................. 13 4.5 How can I fit multiple data sets?..................................... 14 4.6 How can I fit complex data?....................................... 14 4.7 Can I constrain values to have integer values?.............................. 14 4.8 How should I cite LMFIT?........................................ 14 5 Parameter and Parameters 15 5.1 The Parameter class.......................................... 15 5.2 The Parameters class......................................... 17 5.3 Simple Example............................................. 18 6 Performing Fits, Analyzing Outputs 21 6.1 The minimize() function....................................... 21 6.2 Writing a Fitting Function........................................ 22 6.3 Choosing Different Fitting Methods................................... 23 6.4 MinimizerResult – the optimization result............................. 24 6.5 Using a Iteration Callback Function................................... 26 6.6 Using the Minimizer class...................................... 26 6.7 emcee() - calculating the posterior probability distribution of parameters.............. 30 6.8 Getting and Printing Fit Reports..................................... 34 7 Modeling Data and Curve Fitting 37 i 7.1 Example: Fit data to Gaussian profile.................................. 37 7.2 The Model class............................................. 40 7.3 The ModelResult class........................................ 46 7.4 Composite Models : adding (or multiplying) Models.......................... 51 8 Built-in Fitting Models in the models module 57 8.1 Peak-like models............................................. 57 8.2 Linear and Polynomial Models..................................... 61 8.3 Step-like models............................................. 62 8.4 Exponential and Power law models................................... 62 8.5 User-defined Models........................................... 63 8.6 Example 1: Fit Peaked data to Gaussian, Lorentzian, and Voigt profiles................ 64 8.7 Example 2: Fit data to a Composite Model with pre-defined models.................. 67 8.8 Example 3: Fitting Multiple Peaks – and using Prefixes........................ 69 9 Calculation of confidence intervals 73 9.1 Method used for calculating confidence intervals............................ 73 9.2 A basic example............................................. 73 9.3 An advanced example.......................................... 74 9.4 Confidence Interval Functions...................................... 77 10 Bounds Implementation 81 11 Using Mathematical Constraints 83 11.1 Overview................................................. 83 11.2 Supported Operators, Functions, and Constants............................. 84 11.3 Using Inequality Constraints....................................... 84 11.4 Advanced usage of Expressions in lmfit................................. 85 Python Module Index 87 ii Non-Linear Least-Squares Minimization and Curve-Fitting for Python, Release 0.9.4-dirty Warning: Upgrading scripts from version 0.8.3 to 0.9.0? See whatsnew_090_label Warning: Support for Python 2.6 and scipy 0.13 will be dropped with version 0.9.5. Lmfit provides a high-level interface to non-linear optimization and curve fitting problems for Python. Lmfit builds on and extends many of the optimization algorithm of scipy.optimize, especially the Levenberg-Marquardt method from optimize.leastsq. Lmfit 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 in the fit, have a fixed value, or have upper and/or lower bounds. A Parameter can even have a value that is constrained by an algebraic expression of other Parameter values. • 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 optimize.leastsq will automatically calculate uncertainties and correlations from the covariance matrix, the accuracy of these estimates are often questionable. To help address this, lmfit has functions to explicitly explore parameter space to determine confidence levels even for the most difficult cases. • Improved curve-fitting with the Model class. This extends the capabilities of optimize.curve_fit, allowing you to turn a function that models for your data into a python class that helps you parametrize and fit data with that model. • Many pre-built models for common lineshapes are included and ready to use. The lmfit package is Free software, using an MIT 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 Non-Linear Least-Squares Minimization and Curve-Fitting for Python, Release 0.9.4-dirty 2 Contents CHAPTER 1 Getting started with Non-Linear Least-Squares Fitting The lmfit package is designed to provide simple tools to help you build complex fitting models for non-linear least- squares problems and apply these models to real data. This section gives an overview of the concepts and describes how to set up and perform simple fits. Some basic knowledge of Python, numpy, and modeling data are assumed. To do a non-linear least-squares fit of a model to data or for a variety of other optimization problems, the main task is to write an objective function that takes the values of the fitting variables and calculates either a scalar value to be minimized or an array of values that is to be minimized in the least-squares sense. For many data fitting processes, the least-squares approach is used, and the objective function should return an array of (data-model), perhaps scaled by some weighting factor such as the inverse of the uncertainty in the data. For such a problem, the chi-square (휒2) statistic is often defined as: N X [ymeas − ymodel(v)]2 휒2 = i i 휖2 i i meas model where yi is the set of measured data, yi (v) is the model calculation, v is the set of variables in the model to be optimized in the fit, and 휖i is the estimated uncertainty in the data. In a traditional non-linear fit, one writes an objective function that takes the variable values and calculates the residual meas model meas model yi − yi (v), or the residual scaled by the data uncertainties, [yi − yi (v)]/휖i, or some other weighting factor. As a simple example, one might write an objective function like this: def residual(vars, x, data, eps_data): amp= vars[0] phaseshift= vars[1] freq= vars[2] decay= vars[3] model= amp * sin(x * freq+ phaseshift) * exp(-x*x*decay) return (data-model)/eps_data To perform the minimization with scipy.optimize, one would do: from scipy.optimize import leastsq vars=[10.0, 0.2, 3.0, 0.007] out= leastsq(residual, vars, args=(x, data, eps_data)) Though it is wonderful to be able to use python for such optimization problems, and the scipy library is robust and easy to use, the approach here is not terribly different from how one would do the same fit in C or Fortran. There are several practical challenges to using this approach, including: 1. The user has to keep track of the order of the variables, and their meaning – vars[0] is the amplitude, vars[2] is the frequency, and so on, although there is no intrinsic meaning to this order. 3 Non-Linear Least-Squares Minimization and Curve-Fitting for Python, Release 0.9.4-dirty 2. If the user wants to fix a particular variable (not vary it in the fit), the residual function has to be altered to have fewer variables, and have the corresponding constant value passed in some other way. While reasonable for simple cases, this quickly becomes a significant work for more complex models, and greatly complicates modeling for people not intimately familiar with the details of the fitting code. 3. There is no simple, robust way to put bounds on values for the variables, or enforce mathematical relationships between the variables. In fact, those optimization methods that do provide bounds, require bounds to be set for all variables with separate arrays that are in the same arbitrary order as variable values. Again, this is acceptable for small or one-off cases, but becomes painful if the fitting model needs to change. These shortcomings are really do solely to the use of traditional arrays of variables, as matches closely the imple- mentation of the Fortran code. The lmfit
Recommended publications
  • Non-Linear Least-Squares Minimization and Curve-Fitting for Python Release 0.9.3-Dirty
    Non-Linear Least-Squares Minimization and Curve-Fitting for Python Release 0.9.3-dirty Matthew Newville, Till Stensitzki, and others March 27, 2016 Contents 1 Getting started with Non-Linear Least-Squares Fitting3 2 Downloading and Installation7 2.1 Prerequisites...............................................7 2.2 Downloads................................................7 2.3 Installation................................................7 2.4 Development Version..........................................7 2.5 Testing..................................................8 2.6 Acknowledgements...........................................8 2.7 License..................................................8 3 Getting Help 11 4 Frequently Asked Questions 13 4.1 What’s the best way to ask for help or submit a bug report?....................... 13 4.2 Why did my script break when upgrading from lmfit 0.8.3 to 0.9.0?.................. 13 4.3 I get import errors from IPython..................................... 13 4.4 How can I fit multi-dimensional data?.................................. 13 4.5 How can I fit multiple data sets?..................................... 14 4.6 How can I fit complex data?....................................... 14 4.7 Can I constrain values to have integer values?.............................. 14 4.8 How should I cite LMFIT?........................................ 14 5 Parameter and Parameters 15 5.1 The Parameter class.......................................... 15 5.2 The Parameters class......................................... 17 5.3
    [Show full text]
  • Non-Linear Least-Squares Minimization and Curve-Fitting for Python Release 1.0.2
    Non-Linear Least-Squares Minimization and Curve-Fitting for Python Release 1.0.2 Matthew Newville, Till Stensitzki, Renee Otten, and others Feb 11, 2021 CONTENTS 1 Getting started with Non-Linear Least-Squares Fitting3 2 Downloading and Installation7 2.1 Prerequisites...............................................7 2.2 Downloads................................................7 2.3 Installation................................................8 2.4 Development Version..........................................8 2.5 Testing..................................................8 2.6 Acknowledgements...........................................8 2.7 Copyright, Licensing, and Re-distribution................................ 10 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 How should I cite LMFIT?........................................ 16 4.8 I get errors from NaN in my fit. What can I do?............................. 16 4.9 Why are Parameter values sometimes stuck at initial values?...................... 17 4.10 Why are uncertainties in Parameters sometimes not determined?...................
    [Show full text]
  • On Characteristic Functions of Products of Two Random Variables
    The University of Manchester Research On Characteristic Functions of Products of Two Random Variables DOI: 10.1007/s11277-019-06462-3 Document Version Accepted author manuscript Link to publication record in Manchester Research Explorer Citation for published version (APA): Jiang, X., & Nadarajah, S. (2019). On Characteristic Functions of Products of Two Random Variables. Wireless Personal Communications. https://doi.org/10.1007/s11277-019-06462-3 Published in: Wireless Personal Communications Citing this paper Please note that where the full-text provided on Manchester Research Explorer is the Author Accepted Manuscript or Proof version this may differ from the final Published version. If citing, it is advised that you check and use the publisher's definitive version. General rights Copyright and moral rights for the publications made accessible in the Research Explorer are retained by the authors and/or other copyright owners and it is a condition of accessing publications that users recognise and abide by the legal requirements associated with these rights. Takedown policy If you believe that this document breaches copyright please refer to the University of Manchester’s Takedown Procedures [http://man.ac.uk/04Y6Bo] or contact [email protected] providing relevant details, so we can investigate your claim. Download date:26. Sep. 2021 On characteristic functions of products of two random variables by X. Jiang and S. Nadarajah School of Mathematics, University of Manchester, Manchester M13 9PL, UK email: [email protected] Abstract: Motivated by a recent paper published in IEEE Signal Processing Letters, we study the distribution of the product of two independent random variables, one of them being the standard normal random variable and the other allowed to follow one of nearly fifty distributions.
    [Show full text]
  • Non-Linear Least-Squares Minimization and Curve-Fitting for Python Release 0.9.12
    Non-Linear Least-Squares Minimization and Curve-Fitting for Python Release 0.9.12 Matthew Newville, Till Stensitzki, and others Nov 29, 2018 CONTENTS 1 Getting started with Non-Linear Least-Squares Fitting3 2 Downloading and Installation7 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 11 4 Frequently Asked Questions 13 4.1 What’s the best way to ask for help or submit a bug report?....................... 13 4.2 Why did my script break when upgrading from lmfit 0.8.3 to 0.9.0?.................. 13 4.3 I get import errors from IPython..................................... 13 4.4 How can I fit multi-dimensional data?.................................. 13 4.5 How can I fit multiple data sets?..................................... 14 4.6 How can I fit complex data?....................................... 14 4.7 Can I constrain values to have integer values?.............................. 14 4.8 How should I cite LMFIT?........................................ 14 4.9 I get errors from NaN in my fit. What can I do?............................. 14 5 Parameter and Parameters 17 5.1 The Parameter class.........................................
    [Show full text]