gaussfit

tayph.gaussfit(x, y, nparams=3, startparams=None, yerr=None, fixparams=None, minvalues=None, maxvalues=None, verbose=False)[source] [edit on github]

This is a wrapper around lmfit to fit a Gaussian plus a polynomial continuum using the LM least-squares minimization algorithm. It is designed to be interacted with in a similar way as IDL’s Gaussfit routine, though not exactly the same.

The user provides data as np.arrays that form x and y pairs. The Gaussian model has a minimum number of 3 parameters: The amplitude, mean and standard deviation. In addition, the user can request additional parameters that describe the continuum as a polynomial with increasing degree. The model is therefore defined as follows:

y = A*exp(-(x - mu)**2/(sqrt(2)sigma)**2) + c1 + c2*x + c3*x**2 + …

If initial guesses for the parameters are unknown or omitted, this function will guess start parameters albeit coarsely. The code will determine whether the peak is positive or negative by comparing the distance between the maximum and the median with the distance between the minimum and the median. If the former is larger, the Gaussian is considered to be positive. The amplitude is then guessed as the maximum - the minimum of the data y and the mean is guessed as the location of that maximum. Vice versa if the peak was established to be a minimum. The standard deviation is estimated as a fifth of the length of the data array.

The nparams keyword will determine the number of free parameters to fit (3 for the Gaussian plus however many for the continuum).

If startparams is set, the nparams keyword is overruled; and initial parameters need to be applied in the order [A,mu,sigma,c1,c2,…].

By default, the least-squares optimizer assumes unweighted data points. If error bars on y are known, provide them with the yerr keyword (in the same way as you would supply them to plt.errorbar(x,y,yerr=…)).

Parameter values can also be fixed (if startparams is set) or bounded. To fix parameters, set fixparams to a list (with same length as startparams) filled with 0’s and 1’s. Each parameter that is 1 will be fixed.

For putting minimum and/or maximum bounds on the parameters, similarly provide lists with the same length as startparams, with the minimum/maximum of each parameter. To set a bound on only one parameter, the others that you wish to remain unbounded need to be set to np.inf or -np.inf.

NaNs in y are omitted by the lmfit.minimizer routine. NaNs in x are not accepted.

Lmfit is a powerful package for all of your curvefitting needs, and includes emcee functionality as well. Do check it out if you wish to fit models that are more complex than simple Gaussians, or wish to have access to more advanced statistics: https://lmfit.github.io. If you want to add more advanced functionality or build variations on this wrapper, a very useful page is their examples page: https://lmfit.github.io/lmfit-py/examples/index.html

Parameters
xnp.ndarray

The grid onto which the data is defined. NaNs are not allowed.

ynp.ndarray

The data. NaNs are allowed and will be omitted by the minimizer.

nparamsint, optional.

The number of parameters of the model. The model is defined as above, with a minimum of 3 parameters.

startparmaslist, optional.

The starting parameters at which the fit is initialized, in the order [A,mu,sigma,c1,c2,…]. This overrules the nparams keyword.

yerrnp.ndarray, optional.

The standard deviation on each of the values of y, by which the least-squares minimization will be weighted.

fixparamslist, optional

The parameters to fix, in the form of [0,1,0,1,1]; in the same order as startparams.

minvalueslist, optional

The minimum bounds of the fitting parameters, in the form of [-np.inf,10,5,-np.inf,-np.inf].

maxvalueslist, optional

The maximum bounds of the fitting parameters, in the form of [np.inf,10,5,np.inf,np.inf].

verbosebool, optional

If set to True, the lmfit package will talk to you.

Returns
rlist

The best-fit parameters, in the same order as startparams.

re: list

The 1-sigma confidence intervals on the best-fit parameters.