
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.......................................... 17 5.2 The Parameters class......................................... 19 5.3 Simple Example............................................. 22 6 Performing Fits and Analyzing Outputs 25 6.1 The minimize() function....................................... 25 6.2 Writing a Fitting Function........................................ 27 6.3 Choosing Different Fitting Methods................................... 28 6.4 MinimizerResult – the optimization result............................. 29 6.5 Using a Iteration Callback Function................................... 33 6.6 Using the Minimizer class...................................... 33 6.7 Minimizer.emcee() - calculating the posterior probability distribution of parameters...... 42 6.8 Getting and Printing Fit Reports..................................... 45 7 Modeling Data and Curve Fitting 49 7.1 Motivation and simple example: Fit data to Gaussian profile...................... 49 i 7.2 The Model class............................................. 52 7.3 The ModelResult class........................................ 62 7.4 Composite Models : adding (or multiplying) Models.......................... 69 8 Built-in Fitting Models in the models module 75 8.1 Peak-like models............................................. 75 8.2 Linear and Polynomial Models..................................... 86 8.3 Step-like models............................................. 88 8.4 Exponential and Power law models................................... 90 8.5 User-defined Models........................................... 91 8.6 Example 1: Fit Peak data to Gaussian, Lorentzian, and Voigt profiles................. 93 8.7 Example 2: Fit data to a Composite Model with pre-defined models.................. 96 8.8 Example 3: Fitting Multiple Peaks – and using Prefixes........................ 98 9 Calculation of confidence intervals 103 9.1 Method used for calculating confidence intervals............................ 103 9.2 A basic example............................................. 103 9.3 An advanced example.......................................... 104 9.4 Confidence Interval Functions...................................... 108 10 Bounds Implementation 111 11 Using Mathematical Constraints 113 11.1 Overview................................................. 113 11.2 Supported Operators, Functions, and Constants............................. 113 11.3 Using Inequality Constraints....................................... 114 11.4 Advanced usage of Expressions in lmfit................................. 115 12 Release Notes 117 12.1 Version 0.9.12 Release Notes...................................... 117 12.2 Version 0.9.10 Release Notes...................................... 118 12.3 Version 0.9.9 Release Notes....................................... 118 12.4 Version 0.9.6 Release Notes....................................... 118 12.5 Version 0.9.5 Release Notes....................................... 118 12.6 Version 0.9.4 Release Notes....................................... 119 12.7 Version 0.9.3 Release Notes....................................... 119 12.8 Version 0.9.0 Release Notes....................................... 119 Python Module Index 121 Index 123 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 uncer- tainties 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 Non-Linear Least-Squares Minimization and Curve-Fitting for Python, Release 0.9.12 2 CONTENTS CHAPTER ONE GETTING STARTED WITH NON-LINEAR LEAST-SQUARES FITTING The lmfit package provides 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 – this is not a tutorial on why or how to perform a minimization or fit data, but is rather aimed at explaining how to use lmfit to do these things. In order to do a non-linear least-squares fit of a model to data or for any other optimization problem, 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 are to be minimized, typically in the least-squares sense. For many data fitting processes, the latter 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 array yi − yi (v), or the residual array scaled by the data uncertainties, [yi − yi (v)]/휖i, or some other weighting factor. As a simple concrete example, one might want to model data with a decaying sine wave, and so write an objective function like this: from numpy import exp, sin 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 this: from scipy.optimize import leastsq (continues on next page) 3 Non-Linear Least-Squares Minimization and Curve-Fitting for Python, Release 0.9.12 (continued from previous page) 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: a) The user has to keep track of the order of the variables, and their meaning – vars[0] is the amplitude, vars[2]
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages128 Page
-
File Size-