[SciPy-User] Fitting a Gaussian

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Jan 17 18:49:55 EST 2011


On Mon, Jan 17, 2011 at 5:53 PM, Benedikt Riedel <briedel at wisc.edu> wrote:
> Yeah that is what I was trying to do. I am trying to get around using the
> least squares method, but I guess I am stuck with that. Thanks for the
> clarification.

This one http://www.physics.umd.edu/courses/Phys375/Appelbaum_Fall2009/LABS/LAB0/mygaussfit.m
uses only polyfit, but I didn't look carefully enough to understand
what it is doing.
It should by easy to translate into numpython.

Josef

>
> Cheers,
>
> Ben
>
> On Mon, Jan 17, 2011 at 16:50, David Baddeley <david_baddeley at yahoo.com.au>
> wrote:
>>
>> Sounds like you might be trying to fit a curve with a Gaussian, rather
>> than find the parameters of a set of Gaussian distributed observations as
>> Josef assumed. In this case you'll want to define a model function eg:
>> import numpy as np
>> def gauss(p, x):
>>     A, mu, sigma = p
>>     return A*np.exp(-(x-mu)**2/(2*sigma**2))
>> and then have a look at scipy.optimize.curvefit , or if you've got an
>> older version of scipy scipy.optimize.leastsq (in which case you'll need a
>> misfit function - ie y - f(x) instead
>> cheers,
>> David
>> ________________________________
>> From: Benedikt Riedel <briedel at wisc.edu>
>> To: SciPy Users List <scipy-user at scipy.org>
>> Sent: Tue, 18 January, 2011 11:29:37 AM
>> Subject: Re: [SciPy-User] Fitting a Gaussian
>>
>> Mygaussfit basically fits for the sigma, mu and A, for the function
>> f(x)=Aexp(-(x-mu)^2/(2sigma^2)). What I am most interested is the A in this
>> case and the sigma, since I am trying to compare two gaussians.
>>
>> Thanks for the input.
>>
>> Cheers,
>>
>> Ben
>>
>> On Mon, Jan 17, 2011 at 16:09, <josef.pktd at gmail.com> wrote:
>>>
>>> On Mon, Jan 17, 2011 at 4:55 PM, Benedikt Riedel <briedel at wisc.edu>
>>> wrote:
>>> > Hi all,
>>> >
>>> > I am a bit dumbfounded. I have been trying to figure out how to fit a
>>> > gaussian to a data set that I have. The only thing I have been able to
>>> > dig
>>> > up with so far is that I have to get scipy to compute the mean and
>>> > standard
>>> > deviation for me and then basically plug those into a gaussian. Just
>>> > seems a
>>> > little bit odd to me considering that Octave has the mygaussfit
>>> > function
>>> > included or is there an easier method?
>>>
>>> I don't know what a mygaussfit does, but we have this:
>>>
>>> >>> rvs = np.random.randn(200)
>>> >>> from scipy import stats
>>> >>> stats.norm.fit(rvs)
>>> (-0.0092383641087203858, 1.0567583206929831)
>>> >>> loc, scale = stats.norm.fit(rvs)
>>> >>> rvs.mean()
>>> -0.0092383641087203858
>>> >>> rvs.std()
>>> 1.0567583206929831
>>> >>> loc, scale
>>> (-0.0092383641087203858, 1.0567583206929831)
>>> >>> myfrozennorm = stats.norm(loc=loc, scale=scale)
>>> >>> myfrozennorm.cdf(2)
>>> 0.97137010763982468
>>> >>>
>>>
>>> This assumes that all observations are identically distributed and
>>> independent from each other. Otherwise, it get's a little bit more
>>> complicated.
>>>
>>> Josef
>>>
>>> >
>>> > Cheers,
>>> >
>>> > Ben
>>> >
>>> > _______________________________________________
>>> > SciPy-User mailing list
>>> > SciPy-User at scipy.org
>>> > http://mail.scipy.org/mailman/listinfo/scipy-user
>>> >
>>> >
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
>>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>



More information about the SciPy-User mailing list