[SciPy-User] Three-term gaussian fit to gaussian data using scipy
Otto Ngeke
lieemlieem at gmail.com
Thu Apr 6 02:56:56 EDT 2017
I tried to pay more attention to the initial guess as suggested, and found
a very nearly close fit! The new initial guess I used is
p0=[V.max(), std_dev, V.max(), 2]
See attached for new fit.
I then used the resulting values for my parameters ps1, ps2,ps3, ps4 as a
new initial guess, but surprisingly, there is no improvement in the fit. It
just remains the same, even after repeatedly using new parameters as
initial guess for like five successive times. Any way I can improve my
guess?
On Wed, Apr 5, 2017 at 6:39 PM, Matt Newville <newville at cars.uchicago.edu>
wrote:
>
>
> On Wed, Apr 5, 2017 at 10:41 AM, Daπid <davidmenhur at gmail.com> wrote:
>
>>
>> On 5 April 2017 at 17:24, Otto Ngeke <lieemlieem at gmail.com> wrote:
>>
>>> I still get the same fit as before. However, I am worried because my
>>> Gaussians are such that the amplitude of the second Gaussian term is the
>>> standard deviation of the first: some kind of coupling between the terms. I
>>> don't know if this new function definition actually represents that
>>> property.
>>
>>
>> It should be:
>>
>> def func(x, params):
>> ps1, ps2, ps3, ps4 = params
>> return ps1*np.exp(-(x/ps2)**2) + ps2*np.exp(-(x/ps3)**2) +
>> ps3*np.exp(-(x/ps4)**2)
>>
>>
>> Note that in your initialisation you have a symmetry between the first
>> and the third Gaussian:
>>
>> popt, pcov = optimize.curve_fit(func, r, V, p0=[50, std_dev, 50,
>> std_dev], maxfev=10000)
>>
>> So unless curve_fit adds some random noise, the two are going to be
>> always the same because you cannot break the symmetry.
>>
>
> That is not correct. Setting multiple parameters to the same initial
> values does not make them always have the same value. Each parameter here
> is separate, and there is no symmetry here at all.
>
> Instead, give them different values, for example:
>>
>> [50, 1.2 * std_dev, 30, std_dev / 1.2]
>>
>> (I have no idea if this particular values make sense, but they do will
>> allow you to get two independent Gaussians, not too far from your initial
>> guess.)
>>
>>
> Certainly giving initial values that are close as possible is a good idea,
> but they do not need to have values that are unique.
>
>
>> For hard fits (and arbitrary fits are surprisingly hard!), iminuit is a
>> quite powerful option.
>>
>>
> Lmfit might also be useful here. But the basic problem is that the model
> of multiple Gaussians with the same center will have a very hard time
> describing the data shown.
>
> --Matt
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at python.org
> https://mail.python.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-user/attachments/20170406/98301fd8/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gaussian_fit2.png
Type: image/png
Size: 24856 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/scipy-user/attachments/20170406/98301fd8/attachment-0001.png>
More information about the SciPy-User
mailing list