(Important) - ) Modeling Data and Curve Fitting - Non-Linear Least-Squares Minimization and Curve-Fitting For Python
(Important) - ) Modeling Data and Curve Fitting - Non-Linear Least-Squares Minimization and Curve-Fitting For Python
We want to use this function to fit to data y(x) represented by the arrays y and x. With scipy.optimize.curve_fit,
this would be:
https://lmfit.github.io/lmfit-py/model.html 1/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
That is, we create data, make an initial guess of the model values, and run scipy.optimize.curve_fit with the model
function, data arrays, and initial guesses. The results returned are the optimal values for the parameters and the
covariance matrix. It’s simple and useful, but it misses the benefits of lmfit.
With lmfit, we create a Model that wraps the gaussian model function, which automatically generates the appro-
priate residual function, and determines the corresponding parameter names from the function signature itself:
gmodel = Model(gaussian)
print(f'parameter names: {gmodel.param_names}')
print(f'independent variables: {gmodel.independent_vars}')
As you can see, the Model gmodel determined the names of the parameters and the independent variables. By
default, the first argument of the function is taken as the independent variable, held in independent_vars, and
the rest of the functions positional arguments (and, in certain cases, keyword arguments – see below) are used
for Parameter names. Thus, for the gaussian function above, the independent variable is x, and the parameters
are named amp, cen, and wid, and – all taken directly from the signature of the model function. As we will see be-
low, you can modify the default assignment of independent variable / arguments and specify yourself what the
independent variable is and which function arguments should be identified as parameter names.
Parameters are not created when the model is created. The model knows what the parameters should be named,
but nothing about the scale and range of your data. To help you create Parameters for a Model, each model has a
make_params() method that will generate parameters with the expected names. You will have to do this, or make
Parameters some other way (say, with create_params()), and assign initial values for all Parameters. You can also
assign other attributes when doing this:
params = gmodel.make_params()
This creates the Parameters but does not automatically give them initial values since it has no idea what the scale
should be. If left unspecified, the initial values will be -Inf, which will generally fail to give useful results. You can
set initial values for parameters with keyword arguments to make_params():
or assign them (and other parameter properties) after the Parameters class has been created.
A Model has several methods associated with it. For example, one can use the eval() method to evaluate the
model or the fit() method to fit data to this model with a Parameter object. Both of these methods can take ex-
plicit keyword arguments for the parameter values. For example, one could use eval() to calculate the predicted
function:
or with:
Admittedly, this a slightly long-winded way to calculate a Gaussian function, given that you could have called your
gaussian function directly. But now that the model is set up, we can use its fit() method to fit this model to
data, as with:
https://lmfit.github.io/lmfit-py/model.html 2/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
or with:
Putting everything together, included in the examples folder with the source code, is:
# <examples/doc_model_gaussian.py>
import matplotlib.pyplot as plt
from numpy import exp, loadtxt, pi, sqrt
data = loadtxt('model1d_gauss.dat')
x = data[:, 0]
y = data[:, 1]
gmodel = Model(gaussian)
result = gmodel.fit(y, x=x, amp=5, cen=5, wid=1)
print(result.fit_report())
plt.plot(x, y, 'o')
plt.plot(x, result.init_fit, '--', label='initial fit')
plt.plot(x, result.best_fit, '-', label='best fit')
plt.legend()
plt.show()
# <end examples/doc_model_gaussian.py>
which is pretty compact and to the point. The returned result will be a ModelResult object. As we will see below,
this has many components, including a fit_report() method, which will show:
[[Model]]
Model(gaussian)
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 33
# data points = 101
# variables = 3
chi-square = 3.40883599
reduced chi-square = 0.03478404
Akaike info crit = -336.263713
Bayesian info crit = -328.418352
R-squared = 0.98533348
[[Variables]]
amp: 8.88021893 +/- 0.11359522 (1.28%) (init = 5)
cen: 5.65866102 +/- 0.01030495 (0.18%) (init = 5)
wid: 0.69765478 +/- 0.01030505 (1.48%) (init = 1)
[[Correlations]] (unreported correlations are < 0.100)
C(amp, wid) = +0.5774
As the script shows, the result will also have init_fit for the fit with the initial parameter values and a best_fit
for the fit with the best fit parameter values. These can be used to generate the following plot:
https://lmfit.github.io/lmfit-py/model.html 3/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
which shows the data in blue dots, the best fit as a solid green line, and the initial fit as a dashed orange line.
Note that the model fitting was really performed with:
gmodel = Model(gaussian)
result = gmodel.fit(y, params, x=x, amp=5, cen=5, wid=1)
These lines clearly express that we want to turn the gaussian function into a fitting model, and then fit the y(x)
data to this model, starting with values of 5 for amp, 5 for cen and 1 for wid. In addition, all the other features of
lmfit are included: Parameters can have bounds and constraints and the result is a rich object that can be reused
to explore the model fit in detail.
https://lmfit.github.io/lmfit-py/model.html 4/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
name (str, optional) – Name for the model. When None (default) the name is the same as
the model function (func).
**kws (dict, optional) – Additional keyword arguments to pass to model function.
Notes
1. Parameter names are inferred from the function arguments, and a residual function is automatically
constructed.
2. The model function must return an array that will be the same size as the data being modeled.
3. nan_policy sets what to do when a NaN or missing value is seen in the data. Should be one of:
‘raise’ : raise a ValueError (default)
‘propagate’ : do nothing
‘omit’ : drop missing data
Examples
The model function will normally take an independent variable (generally, the first argument) and a series of
arguments that are meant to be parameters for the model. Thus, a simple peak using a Gaussian defined as:
this will automatically discover the names of the independent variables and parameters:
Notes
1. if params is None, the values for all parameters are expected to be provided as keyword arguments.
2. If params is given, and a keyword argument for a parameter value is also given, the keyword argument will
be used in place of the value in the value in params.
3. all non-parameter arguments for the model function, including all the independent variables will need
to be passed in using keyword arguments.
https://lmfit.github.io/lmfit-py/model.html 5/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
4. The return types are generally numpy.ndarray, but may depends on the model function and input indepen-
dent variables. That is, return values may be Python float, int, or complex values.
Notes
1. if params is None, the values for all parameters are expected to be provided as keyword arguments. Mixing
params and keyword arguments is deprecated (see Model.eval).
2. all non-parameter arguments for the model function, including all the independent variables will need
to be passed in using keyword arguments.
3. Parameters are copied on input, so that the original Parameter objects are unchanged, and the updated
values are in the returned ModelResult.
Examples
Take t to be the independent variable and data to be the curve we will fit. Use keyword arguments to set ini-
tial guesses:
https://lmfit.github.io/lmfit-py/model.html 6/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
Model.guess(data, x, **kws)
Guess starting values for the parameters of a Model.
This is not implemented for all models, but is available for many of the built-in models.
Parameters: data (array_like) – Array of data (i.e., y-values) to use to guess parameter values.
x (array_like) – Array of values for the independent variable (i.e., x-values).
**kws (optional) – Additional keyword arguments, passed to model function.
Returns: Initial, guessed values for the parameters of a Model.
Return type: Parameters
Raises: NotImplementedError – If the guess method is not implemented for a Model.
Notes
Should be implemented for each model subclass to run self.make_params(), update starting values and re-
turn a Parameters object.
Changed in version 1.0.3: Argument x is now explicitly required to estimate starting values.
Model.make_params(verbose=False, **kwargs)
Create a Parameters object for a Model.
Parameters: verbose (bool, optional) – Whether to print out messages (default is False).
**kwargs (optional) –
Parameter names and initial values or dictionaries of
values and attributes.
Notes
1. Parameter values can be numbers (floats or ints) to set the parameter value, or dictionaries with any of
the following keywords: value, vary, min, max, expr, brute_step, is_init_value to set those parameter
attributes.
2. This method will also apply any default values or parameter hints that may have been defined for the
model.
Example
Model.set_param_hint(name, **kwargs)
Set hints to use when creating parameters with make_params().
The given hint can include optional bounds and constraints (value, vary, min, max, expr), which will be
used by Model.make_params() when building default parameters.
While this can be used to set initial values, Model.make_params or the function create_params should be pre-
ferred for creating parameters with initial values.
https://lmfit.github.io/lmfit-py/model.html 7/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
The intended use here is to control how a Model should create parameters, such as setting bounds that are
required by the mathematics of the model (for example, that a peak width cannot be negative), or to define
common constrained parameters.
Parameters: name (str) – Parameter name, can include the models prefix or not.
**kwargs (optional) –
Arbitrary keyword arguments, needs to be a Parameter attribute. Can be any of the
following:
value : float, optional
Numerical Parameter value.
Example
Model.print_param_hints(colwidth=8)
Print a nicely aligned text-table of parameter hints.
Parameters: colwidth (int, optional) – Width of each column, except for first and last columns.
Model.post_fit(fitresult)
function that is called just after fit, can be overloaded by subclasses to add non-fitting ‘calculated
parameters’
See Using uncertainties in the fitted parameters for post-fit calculations.
https://lmfit.github.io/lmfit-py/model.html 8/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
name
Name of the model, used only in the string representation of the model. By default this will be taken from
the model function.
opts
Extra keyword arguments to pass to model function. Normally this will be determined internally and should
not be changed.
param_hints
Dictionary of parameter hints. See Using parameter hints.
param_names
List of strings of parameter names.
prefix
Prefix used for name-mangling of parameter names. The default is ''. If a particular Model has arguments
amplitude, center, and sigma, these would become the parameter names. Using a prefix of 'g1_' would con-
vert these parameter names to g1_amplitude, g1_center, and g1_sigma. This can be essential to avoid name
collision in composite models.
import numpy as np
from lmfit import Model
decay_model = Model(decay)
print(f'independent variables: {decay_model.independent_vars}')
params = decay_model.make_params()
print('\nParameters:')
for pname, par in params.items():
print(pname, par)
https://lmfit.github.io/lmfit-py/model.html 9/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
Parameters:
tau <Parameter 'tau', value=-inf, bounds=[-inf:inf]>
N <Parameter 'N', value=-inf, bounds=[-inf:inf]>
Here, t is assumed to be the independent variable because it is the first argument to the function. The other
function arguments are used to create parameters for the model.
If you want tau to be the independent variable in the above example, you can say so:
params = decay_model.make_params()
print('\nParameters:')
for pname, par in params.items():
print(pname, par)
Parameters:
t <Parameter 't', value=-inf, bounds=[-inf:inf]>
N <Parameter 'N', value=-inf, bounds=[-inf:inf]>
You can also supply multiple values for multi-dimensional functions with multiple independent variables. In fact,
the meaning of independent variable here is simple, and based on how it treats arguments of the function you
are modeling:
independent variable
A function argument that is not a parameter or otherwise part of the model, and that will be required to be
explicitly provided as a keyword argument for each fit with Model.fit() or evaluation with Model.eval().
Note that independent variables are not required to be arrays, or even floating point numbers.
mod = Model(decay2)
params = mod.make_params()
print('Parameters:')
for pname, par in params.items():
print(pname, par)
Parameters:
tau <Parameter 'tau', value=-inf, bounds=[-inf:inf]>
N <Parameter 'N', value=10, bounds=[-inf:inf]>
https://lmfit.github.io/lmfit-py/model.html 10/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
Here, even though N is a keyword argument to the function, it is turned into a parameter, with the default numer-
ical value as its initial value. By default, it is permitted to be varied in the fit – the 10 is taken as an initial value, not
a fixed value. On the other hand, the check_positive keyword argument, was not converted to a parameter be-
cause it has a boolean default value. In some sense, check_positive becomes like an independent variable to the
model. However, because it has a default value it is not required to be given for each model evaluation or fit, as
independent variables are.
Parameters:
f1_amplitude <Parameter 'f1_amplitude', value=1, bounds=[-inf:inf]>
f1_center <Parameter 'f1_center', value=0, bounds=[-inf:inf]>
f1_sigma <Parameter 'f1_sigma', value=1, bounds=[-inf:inf]>
You would refer to these parameters as f1_amplitude and so forth, and the model will know to map these to the
amplitude argument of myfunc.
To supply initial values for parameters in the definition of the model function, you can simply supply a default
value:
https://lmfit.github.io/lmfit-py/model.html 11/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
instead of using:
This has the advantage of working at the function level – all parameters with keywords can be treated as options.
It also means that some default initial value will always be available for the parameter.
When creating parameters with Model.make_params() you can specify initial values. To do this, use keyword argu-
ments for the parameter names. You can either set initial values as numbers (floats or ints) or as dictionaries with
keywords of (value, vary, min, max, expr, brute_step, and is_init_value) to specify these parameter attributes.
mod = Model(myfunc)
You can also create your own Parameters directly using create_params(). This is independent of using the Model
class, but is essentially equivalent to Model.make_params() except with less checking of errors for model prefixes
and so on.
mod = Model(myfunc)
Because less error checking is done, Model.make_params() should probably be preferred when using Models.
Finally, you can explicitly supply initial values when using a model. That is, as with Model.make_params(), you can
include values as keyword arguments to either the Model.eval() or Model.fit() methods:
These approaches to initialization provide many opportunities for setting initial values for parameters. The meth-
ods can be combined, so that you can set parameter hints but then change the initial value explicitly with
https://lmfit.github.io/lmfit-py/model.html 12/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
Model.fit().
mod = Model(myfunc)
mod.set_param_hint('bounded_parameter', min=0, max=1.0)
pars = mod.make_params()
Parameter hints are discussed in more detail in section Using parameter hints.
Parameter hints are stored in a model’s param_hints attribute, which is simply a nested dictionary:
print('Parameter hints:')
for pname, par in mod.param_hints.items():
print(pname, par)
Parameter hints:
bounded_parameter {'min': 0, 'max': 1.0}
You can change this dictionary directly or use the Model.set_param_hint() method. Either way, these parameter
hints are used by Model.make_params() when making parameters.
Parameter hints also allow you to create new parameters. This can be useful to make derived parameters with
constraint expressions. For example to get the full-width at half maximum of a Gaussian model, one could use a
parameter hint of:
mod = Model(gaussian)
mod.set_param_hint('wid', min=0)
mod.set_param_hint('fwhm', expr='2.3548*wid')
params = mod.make_params(amp={'value': 10, 'min':0.1, 'max':2000},
cen=5.5, wid=1.25)
params.pretty_print()
With that definition, the value (and uncertainty) of the fwhm parameter will be reported in the output of any fit
done with that model.
https://lmfit.github.io/lmfit-py/model.html 13/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
Model.fit(), you really need to pass in values for the data to be modeled and the independent data used to cal-
culate that data.
As discussed in Types of Data to Use for Fitting, the mathematical solvers used by lmfit all work exclusively with
1-dimensional numpy arrays of datatype (dtype) “float64”. The value of the calculation (model-data)*weights us-
ing the calculation of your model function, and the data and weights you pass in will always be coerced to an 1-
dimensional ndarray with dtype “float64” when it is passed to the solver. If it cannot be coerced, an error will oc-
cur and the fit will be aborted.
That coercion will usually work for “array like” data that is not already a float64 ndarray. But, depending on the
model function, the calculations within the model function may not always work well for some “array like” data
types - especially independent data that are in list of numbers and ndarrays of type “float32” or “int16” or less
precision.
To be clear, independent data for models using Model are meant to be truly independent, and not not required to
be strictly numerical or objects that are easily converted to arrays of numbers. The could, for example, be a dic-
tionary, an instance of a user-defined class, or other type of structured data. You can use independent data any
way you want in your model function. But, as with almost all the examples given here, independent data is often
also a 1-dimensional array of values, say x, and a simple view of the fit would be to plot the data as y as a function
of x. Again, this is not required, but it is very common, especially for novice users.
By default, all data and independent data passed to Model.fit() that is “array like” - a list or tuple of numbers, a
pandas.Series, and h5py.Dataset, or any object that has an __array__() method – will be converted to a
“float64” ndarray before the fit begins. If the array-like data is complex, it will be converted to a “complex128”
ndarray, which will always work too. This conversion before the fit begins ensures that the model function sees
only “float64 ndarrays”, and nearly guarantees that data type conversion will not cause problems for the fit. But it
also means that if you have passed a pandas.Series as data or independent data, not all of the methods or at-
tributes of that Series will be available by default within the model function.
New in version 1.2.2.
This coercion can be turned of with the coerce_farray option to Model.fit(). When set to False, neither the
data nor the independent data will be coerced from their original data type, and the user will be responsible to
arrange for the calculation and return value from the model function to be allow a proper and accurate conver-
sion to a “float64” ndarray.
See also Types of Data to Use for Fitting for general advise and recommendations on types of data to use when
fitting data.
save the model, and if dill is available at run-time, the dill-encoded function will try to be used. Note that this
approach will generally allow you to save a model that can be used by another installation of the same version of
Python, but may not work across Python versions. For preserving fits for extended periods of time (say, archiving
for documentation of scientific results), we strongly encourage you to save the full Python code used for the
model function and fit process.
save_model(model, fname)
Save a Model to a file.
Parameters: model (Model) – Model to be saved.
fname (str) – Name of file for saved Model.
load_model(fname, funcdefs=None)
Load a saved Model from a file.
Parameters: fname (str) – Name of file containing saved Model.
funcdefs (dict, optional) – Dictionary of custom function names and definitions.
Returns: Model object loaded from file.
Return type: Model
# <examples/doc_model_savemodel.py>
import numpy as np
sinemodel = Model(mysine)
pars = sinemodel.make_params(amp=1, freq=0.25, shift=0)
save_model(sinemodel, 'sinemodel.sav')
# <end examples/doc_model_savemodel.py>
# <examples/doc_model_loadmodel.py>
import os
import sys
if not os.path.exists('sinemodel.sav'):
os.system(f"{sys.executable} doc_model_savemodel.py")
data = np.loadtxt('sinedata.dat')
x = data[:, 0]
y = data[:, 1]
https://lmfit.github.io/lmfit-py/model.html 15/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
model = load_model('sinemodel.sav', funcdefs={'mysine': mysine})
params = model.make_params(amp=dict(value=3, min=0),
freq=0.52,
shift=dict(value=0, min=-1, max=1))
plt.plot(x, y, 'o')
plt.plot(x, result.best_fit, '-')
plt.show()
# <end examples/doc_model_loadmodel.py>
https://lmfit.github.io/lmfit-py/model.html 16/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
ModelResult methods
ModelResult.eval(params=None, **kwargs)
Evaluate model function.
Parameters: params (Parameters, optional) – Parameters to use.
**kwargs (optional) – Options to send to Model.eval().
Returns: Array or value for the evaluated model.
Return type: numpy.ndarray, float, int, or complex
ModelResult.eval_components(params=None, **kwargs)
Evaluate each component of a composite model function.
Parameters: params (Parameters, optional) – Parameters, defaults to ModelResult.params.
**kwargs (optional) – Keyword arguments to pass to model function.
Returns: Keys are prefixes of component models, and values are the estimated model value for each
component of the model.
Return type: dict
ModelResult.summary()
Return a dictionary with statistics and attributes of a ModelResult.
Returns: Dictionary of statistics and many attributes from a ModelResult.
Return type: dict
https://lmfit.github.io/lmfit-py/model.html 17/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
Notes
1. values for data arrays are not included.
2. The result summary dictionary will include the following entries:
model, method, ndata, nvarys, nfree, chisqr, redchi, aic, bic, rsquared, nfev, max_nfev, aborted,
errorbars, success, message, lmdif_message, ier, nan_policy, scale_covar, calc_covar, ci_out,
col_deriv, flatchain, call_kws, var_names, user_options, kws, init_values, best_values, and
params.
where ‘params’ is a list of parameter “states”: tuples with entries of (name, value, vary, expr, min, max,
brute_step, stderr, correl, init_value, user_data).
3. The result will include only plain Python objects, and so should be easily serializable with JSON or similar
tools.
ModelResult.conf_interval(**kwargs)
Calculate the confidence intervals for the variable parameters.
Confidence intervals are calculated using the confidence.conf_interval() function and keyword arguments
(**kwargs) are passed to that function. The result is stored in the ci_out attribute so that it can be accessed
without recalculating them.
Notes
1. This is based on the excellent and clear example from
https://www.astro.rug.nl/software/kapteyn/kmpfittutorial.html#confidence-and-prediction-intervals,
which references the original work of: J. Wolberg, Data Analysis Using the Method of Least Squares,
2006, Springer
2. The value of sigma is number of sigma values, and is converted to a probability. Values of 1, 2, or 3 give
probabilities of 0.6827, 0.9545, and 0.9973, respectively. If the sigma value is < 1, it is interpreted as the
probability itself. That is, sigma=1 and sigma=0.6827 will give the same results, within precision errors.
3. Also sets attributes of dely for the uncertainty of the model (which will be the same as the array re-
turned by this method) and dely_comps, a dictionary of dely for each component.
https://lmfit.github.io/lmfit-py/model.html 18/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
Examples
See also
ModelResult.plot_fit
Plot the fit results using matplotlib.
ModelResult.plot_residuals
Plot the fit residuals using matplotlib.
Notes
The method combines ModelResult.plot_fit and ModelResult.plot_residuals.
https://lmfit.github.io/lmfit-py/model.html 19/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
If yerr is specified or if the fit model included weights, then matplotlib.axes.Axes.errorbar is used to plot
the data. If yerr is not specified and the fit includes weights, yerr set to 1/self.weights.
If model returns complex data, yerr is treated the same way that weights are in this case.
If fig is None then matplotlib.pyplot.figure(**fig_kws) is called, otherwise fig_kws is ignored.
See also
ModelResult.plot_residuals
Plot the fit residuals using matplotlib.
ModelResult.plot
Plot the fit results and residuals using matplotlib.
Notes
For details about plot format strings and keyword arguments see documentation of
matplotlib.axes.Axes.plot.
If yerr is specified or if the fit model included weights, then matplotlib.axes.Axes.errorbar is used to plot
the data. If yerr is not specified and the fit includes weights, yerr set to 1/self.weights.
If model returns complex data, yerr is treated the same way that weights are in this case.
If ax is None then matplotlib.pyplot.gca(**ax_kws) is called.
https://lmfit.github.io/lmfit-py/model.html 20/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
See also
ModelResult.plot_fit
Plot the fit results using matplotlib.
ModelResult.plot
Plot the fit results and residuals using matplotlib.
Notes
For details about plot format strings and keyword arguments see documentation of
matplotlib.axes.Axes.plot.
If yerr is specified or if the fit model included weights, then matplotlib.axes.Axes.errorbar is used to plot
the data. If yerr is not specified and the fit includes weights, yerr set to 1/self.weights.
If model returns complex data, yerr is treated the same way that weights are in this case.
If ax is None then matplotlib.pyplot.gca(**ax_kws) is called.
ModelResult.iter_cb()
Optional callable function, to be called at each fit iteration. This must take take arguments of (params,
iter, resid, *args, **kws), where params will have the current parameter values, iter the iteration, resid
the current residual array, and *args and **kws as passed to the objective function. See Using a Iteration
Callback Function.
ModelResult.jacfcn()
Optional callable function, to be called to calculate Jacobian array.
ModelResult attributes
A ModelResult will take all of the attributes of MinimizerResult, and several more. Here, we arrange them into
categories.
best_values
Dictionary with parameter names as keys, and best-fit values as values.
https://lmfit.github.io/lmfit-py/model.html 21/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
init_params
Initial parameters, as passed to Model.fit().
init_values
Dictionary with parameter names as keys, and initial values as values.
init_vals
list of values for the variable parameters.
params
Parameters used in fit; will contain the best-fit values.
uvars
Dictionary of uncertainties ufloats from Parameters.
var_names
List of variable Parameter names used in optimization in the same order as the values in init_vals and
covar.
best_fit
numpy.ndarray result of model function, evaluated at provided independent variables and with best-fit
parameters.
covar
numpy.ndarray (square) covariance matrix returned from fit.
data
numpy.ndarray of data to compare to model.
dely
numpy.ndarray of estimated uncertainties in the y values of the model from
ModelResult.eval_uncertainty() (see Calculating uncertainties in the model function).
dely_comps
a dictionary of estimated uncertainties in the y values of the model components, from
ModelResult.eval_uncertainty() (see Calculating uncertainties in the model function).
init_fit
numpy.ndarray result of model function, evaluated at provided independent variables and with initial
parameters.
residual
numpy.ndarray for residual.
weights
numpy.ndarray (or None) of weighting values to be used in fit. If not None, it will be used as a multiplicative
factor of the residual array, so that weights*(data - fit) is minimized in the least-squares sense.
components
List of components of the Model.
Fit Status
https://lmfit.github.io/lmfit-py/model.html 22/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
aborted
Whether the fit was aborted.
errorbars
Boolean for whether error bars were estimated by fit.
flatchain
A pandas.DataFrame view of the sampling chain if the emcee method is uses.
ier
Integer returned code from scipy.optimize.leastsq.
lmdif_message
String message returned from scipy.optimize.leastsq.
message
String message returned from minimize().
method
String naming fitting method for minimize().
call_kws
Dict of keyword arguments actually send to underlying solver with minimize().
model
Instance of Model used for model.
scale_covar
Boolean flag for whether to automatically scale covariance matrix.
userargs
positional arguments passed to Model.fit(), a tuple of (y, weights)
userkws
keyword arguments passed to Model.fit(), a dict, which will have independent data arrays such as x.
Fit Statistics
aic
Floating point best-fit Akaike Information Criterion statistic (see MinimizerResult – the optimization result).
bic
Floating point best-fit Bayesian Information Criterion statistic (see MinimizerResult – the optimization result).
chisqr
Floating point best-fit chi-square statistic (see MinimizerResult – the optimization result).
ci_out
Confidence interval data (see Calculation of confidence intervals) or None if the confidence intervals have not
been calculated.
ndata
Integer number of data points.
nfev
https://lmfit.github.io/lmfit-py/model.html 23/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
2
∑ (yi − fi)
2 i
R = 1 −
2
∑ (yi − ȳ)
i
success
Boolean value of whether fit succeeded. This is an optimistic view of success, meaning that the method fin-
ished without error.
dely = result.eval_uncertainty(sigma=3)
plt.fill_between(x, result.best_fit-dely, result.best_fit+dely, color="#ABABAB",
label='3-$\sigma$ uncertainty band')
to the example fit to the Gaussian at the beginning of this chapter will give 3-σ bands for the best-fit Gaussian,
and produce the figure below.
https://lmfit.github.io/lmfit-py/model.html 24/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
# <examples/doc_model_uncertainty2.py>
import matplotlib.pyplot as plt
import numpy as np
dat = np.loadtxt('NIST_Gauss2.dat')
x = dat[:, 1]
y = dat[:, 0]
model = (GaussianModel(prefix='g1_') +
GaussianModel(prefix='g2_') +
ExponentialModel(prefix='bkg_'))
comps = result.eval_components(x=x)
dely = result.eval_uncertainty(sigma=3)
https://lmfit.github.io/lmfit-py/model.html 25/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
axes[0][0].plot(x, y, 'o', color='#99002299', markersize=3, label='data')
axes[0][0].plot(x, result.best_fit, '-', label='best fit')
axes[0][0].plot(x, result.init_fit, '--', label='initial fit')
axes[0][0].set_title('data, initial fit, and best-fit')
axes[0][0].legend()
plt.show()
# <end examples/doc_model_uncertainty2.py>
[[Model]]
((Model(gaussian, prefix='g1_') + Model(gaussian, prefix='g2_')) + Model(exponential, prefix='b
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 55
# data points = 250
# variables = 8
chi-square = 1247.52821
reduced chi-square = 5.15507524
Akaike info crit = 417.864631
Bayesian info crit = 446.036318
R-squared = 0.99648654
[[Variables]]
g1_amplitude: 4257.77399 +/- 42.3838008 (1.00%) (init = 3000)
g1_center: 107.030957 +/- 0.15006868 (0.14%) (init = 100)
https://lmfit.github.io/lmfit-py/model.html 26/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
g1_sigma: 16.6725789 +/- 0.16048222 (0.96%) (init = 10)
g2_amplitude: 2493.41715 +/- 36.1696228 (1.45%) (init = 3000)
g2_center: 153.270104 +/- 0.19466723 (0.13%) (init = 150)
g2_sigma: 13.8069453 +/- 0.18680099 (1.35%) (init = 10)
bkg_amplitude: 99.0183280 +/- 0.53748639 (0.54%) (init = 100)
bkg_decay: 90.9508824 +/- 1.10310769 (1.21%) (init = 80)
g1_fwhm: 39.2609222 +/- 0.37790675 (0.96%) == '2.3548200*g1_sigma'
g1_height: 101.880228 +/- 0.59217122 (0.58%) == '0.3989423*g1_amplitude/max(1e-15, g1_sigma
g2_fwhm: 32.5128710 +/- 0.43988270 (1.35%) == '2.3548200*g2_sigma'
g2_height: 72.0455936 +/- 0.61721901 (0.86%) == '0.3989423*g2_amplitude/max(1e-15, g2_sigma
[[Correlations]] (unreported correlations are < 0.500)
C(g1_amplitude, g1_sigma) = +0.8243
C(g2_amplitude, g2_sigma) = +0.8154
C(bkg_amplitude, bkg_decay) = -0.6946
C(g1_sigma, g2_center) = +0.6842
C(g1_center, g2_amplitude) = -0.6689
C(g1_center, g2_sigma) = -0.6520
C(g1_amplitude, g2_center) = +0.6477
C(g1_center, g2_center) = +0.6205
C(g1_center, g1_sigma) = +0.5075
C(g1_amplitude, bkg_decay) = -0.5074
https://lmfit.github.io/lmfit-py/model.html 27/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
As with the previous section, after a fit is complete, you may want to do some further calculations with the result-
ing Parameter values. Since these Parameters will have not only best-fit values but also usually have uncertain-
ties, it is desirable for subsequent calculations to be able to propagate those uncertainties to any resulting calcu-
lated value. In addition, it is common for Parameters to have finite - and sometimes large - correlations which
should be taken into account in such calculations.
The ModelResult.uvars will be a dictionary with keys for all variable Parameters and values that are uvalues from
the uncertainties package. When used in mathematical calculations with basic Python operators or numpy func-
tions, these uvalues will automatically propagate their uncertainties to the resulting calculation, and taking into
account the full covariance matrix describing the correlation between values.
This readily allows “derived Parameters” to be evaluated just after the fit. In fact, it might be useful to have a
Model always do such a calculation just after the fit. The Model.post_fit() method allows exactly that: you can
overwrite this otherwise empty method for any Model. It takes one argument: the ModelResult instance just after
the actual fit has run (and before Model.fit() returns) and can be used to add Parameters or do other post-fit
processing.
The following example script shows two different methods for calculating a centroid value for two peaks, either
by doing the calculation directly after the fit with the result.uvars or by capturing this in a Model.post_fit()
method that would be run for all instances of that model. It also demonstrates that taking correlations between
Parameters into account when performing calculations can have a noticeable influence on the resulting
uncertainties.
# <examples/doc_uvars_params.py>
import numpy as np
dat = np.loadtxt('NIST_Gauss2.dat')
x = dat[:, 1]
y = dat[:, 0]
bkg = ExponentialModel(prefix='exp_')
gauss1 = GaussianModel(prefix='g1_')
gauss2 = GaussianModel(prefix='g2_')
print(out.fit_report(min_correl=0.5))
uvar_g1amp = out.uvars['g1_amplitude']
uvar_g2amp = out.uvars['g2_amplitude']
uvar_g1cen = out.uvars['g1_center']
uvar_g2cen = out.uvars['g2_center']
https://lmfit.github.io/lmfit-py/model.html 28/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
print(f"Correlation of peak1 and peak2 areas: {out.params['g1_amplitude'].correl['g2_am
print(f"Stderr Quadrature sum for peak1 and peak2 area2: {area_quadrature:.5f}")
print(f"Stderr of peak1 + peak2 area, with correlation: {(uvar_g1amp+uvar_g2amp).std_dev:.5f}")
def post_fit(result):
"example post fit function"
result.params.add('post_area', expr='g1_amplitude + g2_amplitude')
result.params.add('post_centroid', expr='(g1_amplitude*g1_center+ g2_amplitude*g2_center)/(g1_
mod.post_fit = post_fit
[[Model]]
((Model(gaussian, prefix='g1_') + Model(gaussian, prefix='g2_')) + Model(exponential, prefix='ex
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 46
# data points = 250
# variables = 8
chi-square = 1247.52821
reduced chi-square = 5.15507524
Akaike info crit = 417.864631
Bayesian info crit = 446.036318
R-squared = 0.99648654
[[Variables]]
g1_amplitude: 4257.77390 +/- 42.3837959 (1.00%) (init = 4000)
g1_center: 107.030957 +/- 0.15006877 (0.14%) (init = 105)
g1_sigma: 16.6725785 +/- 0.16048211 (0.96%) (init = 15)
g2_amplitude: 2493.41716 +/- 36.1697062 (1.45%) (init = 2000)
g2_center: 153.270104 +/- 0.19466774 (0.13%) (init = 155)
g2_sigma: 13.8069453 +/- 0.18680153 (1.35%) (init = 15)
exp_amplitude: 99.0183277 +/- 0.53748650 (0.54%) (init = 100)
exp_decay: 90.9508838 +/- 1.10310767 (1.21%) (init = 100)
g1_fwhm: 39.2609214 +/- 0.37790648 (0.96%) == '2.3548200*g1_sigma'
g1_height: 101.880229 +/- 0.59217051 (0.58%) == '0.3989423*g1_amplitude/max(1e-15, g1_sigma
g2_fwhm: 32.5128709 +/- 0.43988398 (1.35%) == '2.3548200*g2_sigma'
g2_height: 72.0455939 +/- 0.61721985 (0.86%) == '0.3989423*g2_amplitude/max(1e-15, g2_sigma
[[Correlations]] (unreported correlations are < 0.500)
C(g1_amplitude, g1_sigma) = +0.8243
C(g2_amplitude, g2_sigma) = +0.8154
C(exp_amplitude, exp_decay) = -0.6946
C(g1_sigma, g2_center) = +0.6842
C(g1_center, g2_amplitude) = -0.6689
C(g1_center, g2_sigma) = -0.6520
https://lmfit.github.io/lmfit-py/model.html 29/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
C(g1_amplitude, g2_center) = +0.6477
C(g1_center, g2_center) = +0.6205
C(g1_center, g1_sigma) = +0.5075
C(g1_amplitude, exp_decay) = -0.5074
### Peak Area:
Peak1: 4257.77390+/-42.38380
Peak2: 2493.41716+/-36.16971
Sum : 6751.19106+/-46.50802
Correlation of peak1 and peak2 areas: -0.30712
Stderr Quadrature sum for peak1 and peak2 area2: 55.71924
Stderr of peak1 + peak2 area, with correlation: 46.50802
### Peak Centroid:
Peak Centroid: 124.10846+/-0.14074
[[Model]]
((Model(gaussian, prefix='g1_') + Model(gaussian, prefix='g2_')) + Model(exponential, prefix='ex
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 46
# data points = 250
# variables = 8
chi-square = 1247.52821
reduced chi-square = 5.15507524
Akaike info crit = 417.864631
Bayesian info crit = 446.036318
R-squared = 0.99648654
[[Variables]]
g1_amplitude: 4257.77390 +/- 42.3837959 (1.00%) (init = 4000)
g1_center: 107.030957 +/- 0.15006877 (0.14%) (init = 105)
g1_sigma: 16.6725785 +/- 0.16048211 (0.96%) (init = 15)
g2_amplitude: 2493.41716 +/- 36.1697062 (1.45%) (init = 2000)
g2_center: 153.270104 +/- 0.19466774 (0.13%) (init = 155)
g2_sigma: 13.8069453 +/- 0.18680153 (1.35%) (init = 15)
exp_amplitude: 99.0183277 +/- 0.53748650 (0.54%) (init = 100)
exp_decay: 90.9508838 +/- 1.10310767 (1.21%) (init = 100)
g1_fwhm: 39.2609214 +/- 0.37790648 (0.96%) == '2.3548200*g1_sigma'
g1_height: 101.880229 +/- 0.59217051 (0.58%) == '0.3989423*g1_amplitude/max(1e-15, g1_sigma
g2_fwhm: 32.5128709 +/- 0.43988398 (1.35%) == '2.3548200*g2_sigma'
g2_height: 72.0455939 +/- 0.61721985 (0.86%) == '0.3989423*g2_amplitude/max(1e-15, g2_sigma
post_area: 6751.19106 +/- 46.5080249 (0.69%) == 'g1_amplitude + g2_amplitude'
post_centroid: 124.108459 +/- 0.14074482 (0.11%) == '(g1_amplitude*g1_center+ g2_amplitude*g2_c
[[Correlations]] (unreported correlations are < 0.500)
C(g1_amplitude, g1_sigma) = +0.8243
C(g2_amplitude, g2_sigma) = +0.8154
C(exp_amplitude, exp_decay) = -0.6946
C(g1_sigma, g2_center) = +0.6842
C(g1_center, g2_amplitude) = -0.6689
C(g1_center, g2_sigma) = -0.6520
C(g1_amplitude, g2_center) = +0.6477
C(g1_center, g2_center) = +0.6205
C(g1_center, g1_sigma) = +0.5075
C(g1_amplitude, exp_decay) = -0.5074
[[Model]]
((Model(gaussian, prefix='g1_') + Model(gaussian, prefix='g2_')) + Model(exponential, prefix='ex
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 46
# data points = 250
# variables = 8
chi-square = 1247.52821
reduced chi-square = 5.15507524
Akaike info crit = 417.864631
Bayesian info crit = 446.036318
R-squared = 0.99648654
[[Variables]]
g1_amplitude: 4257.77390 +/- 42.3837959 (1.00%) (init = 4000)
g1_center: 107.030957 +/- 0.15006877 (0.14%) (init = 105)
https://lmfit.github.io/lmfit-py/model.html 30/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
g1_sigma: 16.6725785 +/- 0.16048211 (0.96%) (init = 15)
g2_amplitude: 2493.41716 +/- 36.1697062 (1.45%) (init = 2000)
g2_center: 153.270104 +/- 0.19466774 (0.13%) (init = 155)
g2_sigma: 13.8069453 +/- 0.18680153 (1.35%) (init = 15)
exp_amplitude: 99.0183277 +/- 0.53748650 (0.54%) (init = 100)
exp_decay: 90.9508838 +/- 1.10310767 (1.21%) (init = 100)
g1_fwhm: 39.2609214 +/- 0.37790648 (0.96%) == '2.3548200*g1_sigma'
g1_height: 101.880229 +/- 0.59217051 (0.58%) == '0.3989423*g1_amplitude/max(1e-15, g1_sigma
g2_fwhm: 32.5128709 +/- 0.43988398 (1.35%) == '2.3548200*g2_sigma'
g2_height: 72.0455939 +/- 0.61721985 (0.86%) == '0.3989423*g2_amplitude/max(1e-15, g2_sigma
area: 6751.19106 +/- 46.5080249 (0.69%) == 'g1_amplitude + g2_amplitude'
centroid: 124.108459 +/- 0.14074482 (0.11%) == '(g1_amplitude*g1_center+ g2_amplitude*g2_c
post_area: 6751.19106 +/- 46.5080249 (0.69%) == 'g1_amplitude + g2_amplitude'
post_centroid: 124.108459 +/- 0.14074482 (0.11%) == '(g1_amplitude*g1_center+ g2_amplitude*g2_c
[[Correlations]] (unreported correlations are < 0.500)
C(g1_amplitude, g1_sigma) = +0.8243
C(g2_amplitude, g2_sigma) = +0.8154
C(exp_amplitude, exp_decay) = -0.6946
C(g1_sigma, g2_center) = +0.6842
C(g1_center, g2_amplitude) = -0.6689
C(g1_center, g2_sigma) = -0.6520
C(g1_amplitude, g2_center) = +0.6477
C(g1_center, g2_center) = +0.6205
C(g1_center, g1_sigma) = +0.5075
C(g1_amplitude, exp_decay) = -0.5074
Note that the Model.post_fit() does not need to be limited to this use case of adding derived Parameters.
load_modelresult(fname, funcdefs=None)
Load a saved ModelResult from a file.
Parameters: fname (str) – Name of file containing saved ModelResult.
funcdefs (dict, optional) – Dictionary of custom function names and definitions.
Returns: ModelResult object loaded from file.
Return type: ModelResult
# <examples/doc_model_savemodelresult.py>
import numpy as np
https://lmfit.github.io/lmfit-py/model.html 31/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
from lmfit.models import GaussianModel
data = np.loadtxt('model1d_gauss.dat')
x = data[:, 0]
y = data[:, 1]
gmodel = GaussianModel()
result = gmodel.fit(y, x=x, amplitude=5, center=5, sigma=1)
save_modelresult(result, 'gauss_modelresult.sav')
print(result.fit_report())
# <end examples/doc_model_savemodelresult.py>
# <examples/doc_model_loadmodelresult.py>
import os
import sys
if not os.path.exists('gauss_modelresult.sav'):
os.system(f"{sys.executable} doc_model_savemodelresult.py")
data = np.loadtxt('model1d_gauss.dat')
x = data[:, 0]
y = data[:, 1]
result = load_modelresult('gauss_modelresult.sav')
print(result.fit_report())
plt.plot(x, y, 'o')
plt.plot(x, result.best_fit, '-')
plt.show()
# <end examples/doc_model_loadmodelresult.py>
mod = Model(gaussian_plus_line)
https://lmfit.github.io/lmfit-py/model.html 32/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
But we already had a function for a gaussian function, and maybe we’ll discover that a linear background isn’t suf-
ficient which would mean the model function would have to be changed.
Instead, lmfit allows models to be combined into a CompositeModel. As an alternative to including a linear back-
ground in our model function, we could define a linear function:
This model has parameters for both component models, and can be used as:
# <examples/doc_model_two_components.py>
import matplotlib.pyplot as plt
from numpy import exp, loadtxt, pi, sqrt
data = loadtxt('model1d_gauss.dat')
x = data[:, 0]
y = data[:, 1] + 0.25*x - 1.0
plt.plot(x, y, 'o')
plt.plot(x, result.init_fit, '--', label='initial fit')
plt.plot(x, result.best_fit, '-', label='best fit')
plt.legend()
plt.show()
# <end examples/doc_model_two_components.py>
[[Model]]
(Model(gaussian) + Model(line))
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 55
# data points = 101
# variables = 5
chi-square = 2.57855517
reduced chi-square = 0.02685995
https://lmfit.github.io/lmfit-py/model.html 33/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
Akaike info crit = -360.457020
Bayesian info crit = -347.381417
R-squared = 0.99194643
[[Variables]]
amp: 8.45930976 +/- 0.12414531 (1.47%) (init = 5)
cen: 5.65547889 +/- 0.00917673 (0.16%) (init = 5)
wid: 0.67545513 +/- 0.00991697 (1.47%) (init = 1)
slope: 0.26484403 +/- 0.00574892 (2.17%) (init = 0)
intercept: -0.96860189 +/- 0.03352202 (3.46%) (init = 1)
[[Correlations]] (unreported correlations are < 0.100)
C(slope, intercept) = -0.7954
C(amp, wid) = +0.6664
C(amp, intercept) = -0.2216
C(amp, slope) = -0.1692
C(cen, slope) = -0.1618
C(wid, intercept) = -0.1477
C(cen, intercept) = +0.1287
C(wid, slope) = -0.1127
On the left, data is shown in blue dots, the total fit is shown in solid green line, and the initial fit is shown as a or-
ange dashed line. The figure on the right shows again the data in blue dots, the Gaussian component as a orange
dashed line and the linear component as a green dashed line. It is created using the following code:
comps = result.eval_components()
plt.plot(x, y, 'o')
plt.plot(x, comps['gaussian'], '--', label='Gaussian component')
plt.plot(x, comps['line'], '--', label='Line component')
The components were generated after the fit using the ModelResult.eval_components() method of the result,
which returns a dictionary of the components, using keys of the model name (or prefix if that is set). This will use
the parameter values in result.params and the independent variables (x) used during the fit. Note that while the
ModelResult held in result does store the best parameters and the best estimate of the model in
result.best_fit, the original model and parameters in pars are left unaltered.
You can apply this composite model to other data sets, or evaluate the model at other values of x. You may want
to do this to give a finer or coarser spacing of data point, or to extrapolate the model outside the fitting range.
This can be done with:
https://lmfit.github.io/lmfit-py/model.html 34/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
In this example, the argument names for the model functions do not overlap. If they had, the prefix argument to
Model would have allowed us to identify which parameter went with which component model. As we will see in
the next chapter, using composite models with the built-in models provides a simple way to build up complex
models.
Notes
The two models can use different independent variables.
Note that when using built-in Python binary operators, a CompositeModel will automatically be constructed for
you. That is, doing:
will create a CompositeModel. Here, left will be Model(fcn1), op will be operator.add(), and right will be another
CompositeModel that has a left attribute of Model(fcn2), an op of operator.mul(), and a right of Model(fcn3).
To use a binary operator other than +, -, *, or / you can explicitly create a CompositeModel with the appropriate
binary operator. For example, to convolve two models, you could define a simple convolution function, perhaps
as:
import numpy as np
which extends the data in both directions so that the convolving kernel function gives a valid result over the data
range. Because this function takes two array arguments and returns an array, it can be used as the binary opera-
tor. A full script using this technique is here:
# <examples/doc_model_composite.py>
import matplotlib.pyplot as plt
import numpy as np
https://lmfit.github.io/lmfit-py/model.html 35/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
y = step(x, amplitude=12.5, center=4.5, sigma=0.88, form='erf')
np.random.seed(0)
y = y + np.random.normal(scale=0.35, size=x.size)
# create parameters for model. Note that 'mid' and 'center' will be highly
# correlated. Since 'mid' is used as an integer index, it will be very
# hard to fit, so we fix its value
pars = mod.make_params(amplitude=dict(value=1, min=0),
center=3.5,
sigma=dict(value=1.5, min=0),
mid=dict(value=4, vary=False))
print(result.fit_report())
# generate components
comps = result.eval_components(x=x)
# plot results
fig, axes = plt.subplots(1, 2, figsize=(12.8, 4.8))
axes[0].plot(x, y, 'bo')
axes[0].plot(x, result.init_fit, 'k--', label='initial fit')
axes[0].plot(x, result.best_fit, 'r-', label='best fit')
axes[0].legend()
axes[1].plot(x, y, 'bo')
axes[1].plot(x, 10*comps['jump'], 'k--', label='Jump component')
axes[1].plot(x, 10*comps['gaussian'], 'r-', label='Gaussian component')
axes[1].legend()
plt.show()
# <end examples/doc_model_composite.py>
[[Model]]
(Model(jump) <function convolve at 0x15d0c93f0> Model(gaussian))
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 33
https://lmfit.github.io/lmfit-py/model.html 36/37
8/18/23, 3:35 AM Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python
# data points = 201
# variables = 3
chi-square = 24.7562335
reduced chi-square = 0.12503148
Akaike info crit = -414.939746
Bayesian info crit = -405.029832
R-squared = 0.99632577
[[Variables]]
mid: 4 (fixed)
amplitude: 0.62508458 +/- 0.00189732 (0.30%) (init = 1)
center: 5.50853669 +/- 0.00973231 (0.18%) (init = 3.5)
sigma: 0.59576097 +/- 0.01348579 (2.26%) (init = 1.5)
[[Correlations]] (unreported correlations are < 0.100)
C(amplitude, center) = +0.3292
C(amplitude, sigma) = +0.2680
Using composite models with built-in or custom operators allows you to build complex models from testable sub-
components.
https://lmfit.github.io/lmfit-py/model.html 37/37