# Finding the right model for non-linear least squares fitting of data

I have a set of data to which I would like to fit a function. As a starting point, I tried fitting the data to a standard normal distribution with $$2$$ parameters; a Gaussian function with amplitude $$A$$ and a variance term $$B$$, which can be written as $$y(x)=Ae^{-Bx^2}$$

The non-linear least squares process was carried out via scipy curve_fit package. Here is the result

Obviously this fit is no good. I suspect that this is because the trend in the data is not exactly a gaussian - however, this extremely flawed fit makes me think there is some other problem, perhaps the very small amplitude of the vertical data. The question is, then, what model should be used instead of the naive normal distribution? This is a new problem for me, and I can't see how it is possible to deduce the correct model that "bestfits" the data trend in a non-linear case.

• A scale mixture of two Cauchys, both centered just below 150, matches the shape here quite well (I could see right away that one wouldn't really work). I didn't do more than just try a few values (by eye) for the relative scale and mixing proportion (this one has 80% of a base scale and 20% of 9 times that scale), then scaled the height and width of the plot to approximately match the original by hand; an actual optimized fit should do much better than this. i.sstatic.net/kM5RCJb8.png Commented Jul 24 at 18:17
• That's not to say that some other function wouldn't do better still, but if you're just trying to do basic smoothing of the data or find the location of the peak this sort of function should work pretty well Commented Jul 24 at 18:23
• If you provide the data here, other people can try as well. When deciding what function you should use to fit data you should also take into account what the data represents. Should you expect a gaussian here? If you just try random exotic functions until you find a good fit, the final fit will be better than you should take credit for and this could be scientifically misleading. Having said that: a Lorentzian looks like a great fit. It's more narrow in the middle and the tails are fatter than for a Gaussian. Commented Jul 25 at 7:28
• @AccidentalTaylorExpansion My goal is to smooth the data; keeping the trend, while discarding the random variation or 'noise'. For this purpose, the double Cauchy suggested by Glen_b worked quite well. Commented Jul 25 at 11:36

Your model looks inconsistent with the data.

• $$Ae^{-Bx^2}$$ has a peak at $$x=0$$ for positive $$A,B$$, and then decreases for larger $$x$$, which is why your orange line is downward sloping.

• Your data has a strong peak near $$x=150$$, which is why the orange line is nowhere near the blue line for most of the distribution.

From your knowledge of the actual situation, can you explain why the data might have this peak away from $$0$$, and why you chose a model which does not?

A model like $$y=Ae^{-B(x-C)^2}$$ might perform better.

• +1 I think a plot from -300 to +300 would show a wide Gaussian centered at 0, consistent with the large second moment (so to speak) of the blue data, which would be shoehorned into the variance parameter.
– Dave
Commented Jul 24 at 13:46
• +1 Certainly adding a mean parameter to the Gaussian will help; that's a prime issue to sort out. However, the Gaussian is not nearly heavy-tailed enough for these values. Commented Jul 25 at 12:31