2

I have some measured data which can be either a well established gaussian or something that seems to be a gamma distribution, I currently have the following code (snippet) which performs quite well for data that is nicely gaussian:

def gaussFunction(x, A, mu, sigma):
    return A*numpy.exp(-(x-mu)**2/(2.*sigma**2))

# Snippet of the code that does the fitting
p0 = [numpy.max(y_points), x_points[numpy.argmax(y_points)],0.1]
# Attempt to fit a gaussian function to the calibrant space
try:
  coeff, var_matrix = curve_fit(self.gaussFunction, x_points, y_points, p0)
  newX = numpy.linspace(x_points[0],x_points[-1],1000)
  newY = self.gaussFunction(newX, *coeff)
  fig =  plt.figure()
  ax = fig.add_subplot(111)
  plt.plot(x_points, y_points, 'b*')
  plt.plot(newX,newY, '--')
  plt.show()

Demonstration that it works well for datapoints which are nicely gaussian:

enter image description here

The problem however arises that some of my datapoints are not matching with a good gaussian and I get this:

enter image description here

I would be tempted to try a cubic spline but conceptually I would like to stick to a Gaussian curve fit since that is the data structure that should be within the data (which can occur with a knee or a tail in some data as shown in the second figure). I would highly appreciate if someone has any tip or suggestion on how to deal with this 'issue'.

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Bas Jansen
  • 3,273
  • 5
  • 30
  • 66
  • maybe [this thread](http://stackoverflow.com/a/16651955/832621) will help you to use `scipy` built-in statistical functions to fit your data... – Saullo G. P. Castro May 23 '14 at 19:46
  • Are your data bound to be >0? Might try truncated Gaussian. http://en.wikipedia.org/wiki/Truncated_normal_distribution – CT Zhu May 27 '14 at 17:32
  • The data is always above 0, I have to admit that I just went for a cubic spline for now but I might revisit this problem at a later point. – Bas Jansen May 28 '14 at 09:33
  • Could you provide x_points and y_points, please?! – Cleb Jul 15 '15 at 14:15

0 Answers0