0

I have data points distributed in this manner:enter image description here

Now, I want to fit a smooth Gaussian over it. Can anyone tell me what would be the best way to fit a smooth Gaussian for these data points

1 Answers1

0

There can't be. It is obviously not a gaussian at all.

You have to find another model. For example, a constant + a gaussian.

In that case, you could, for example, imitate Gaussian fit for Python . Except that in this example, the model is a Gaussian, when in your case it is not.

So what you want to fit with curve_fit looks more like

def GaussPlusConst(x, c, a, x0, sigma):
    return c + a * np.exp(-(x - x0)**2 / (2 * sigma**2))

Then, since you have an extra parameter (the constant), you need to call curve_fit with that extra parameter

popt,pcov = curve_fit(Gauss, x, y, p0=[min(y), max(y)-min(y), mean, sigma])

I used a rough adaptation of the initial guess (anyway, it is just an initial guess). Constant is the minimum, since, gaussian is so sharp that it is practically 0 on large part of the chart. Then amplitude of the gaussian is not just max(y) but max-min (it is the amplitude of what is added to this constant).

Then estimates for mean and sigma should also be adjusted to concern only the gaussian part (what is over the constant)

mean = sum(x*(y-min(y)))/n
sigma = sum((y-min(y))*(x-mean)**2)/n

But that is just an example (my estimate of the constant is very rough. Plus, you may want to change the model. Even that constant+gaussian is not that realistic)

chrslg
  • 9,023
  • 5
  • 17
  • 31