Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fitting a histogram with skewed gaussian

I want to fit histograms with a skewed gaussian. I take my data from a text file:

rate, err = loadtxt('hist.dat', unpack = True)

and then plot them as a histogram:

plt.hist(rate, bins= 128)

This histogram has a skewed gaussian shape, that I would like to fit. I can do it with a simple gaussian, because scipy has the function included, but not with a skewed. How can I proceed?

Possibly, a goodness of fit test returned would be the best.

like image 252
Py-ser Avatar asked Sep 15 '25 18:09

Py-ser


1 Answers

You might find lmfit (http://lmfit.github.io/lmfit-py/) useful. This has a Skewed Gaussian model built in. Your problem might be as simple as

from lmfit.models import SkewedGaussianModel

xvals, yvals = read_your_histogram()

model = SkewedGaussianModel()

# set initial parameter values
params = model.make_params(amplitude=10, center=0, sigma=1, gamma=0)

# adjust parameters  to best fit data.
result = model.fit(yvals, params, x=xvals)

print(result.fit_report())
pylab.plot(xvals, yvals)
pylab.plot(xvals, result.best_fit) 

This will report the values and uncertainties for the parameters amplitude, center, sigma (for the normal Gaussian), and gamma, the skewness factor.