Ask Your Question
1

Gauss distribution fit

asked 2012-02-18 23:19:04 -0500

v_2e gravatar image

Hello! I have a problem with fitting my data set with a Gauss (Normal) distribution.

data = [[90.00, 2.0], [97.40, 5.0], [104.8, 14.0], [112.2, 12.0], [119.6, 11.0], \
        [127.0, 6.0], [134.4, 3.0], [141.8, 1.0], [149.2, 2.0], [156.6, 1.0]]

var('sigma mu max x')
model(x) = max*(1/sqrt(2*pi*sigma**2))*exp(-(x-mu)**2/(2*sigma**2))

find_fit(data, model)

gives a result:

[sigma == 1.0, mu == 1.0, max == 1.0]

I tried to use a Python function instead of the symbolic one:

var('sigma mu max x')
def model(x,sigma,mu,max):
    return max*(1/(sigma*sqrt(2*pi)))*exp(-(x-mu)**2/(2*sigma**2))

find_fit(data, model, parameters=[sigma, mu, max], variables = [x])

and obtained the very same result:

[sigma == 1.0, mu == 1.0, max == 1.0]

Why does it give me this (obviously, incorrect) result? And what is a way to do it right?

Thanks.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
5

answered 2012-02-19 11:02:32 -0500

Volker Braun gravatar image

You need to give some hint for the initial values. Without any hint, Sage assumes the initial guess of 1 for all parameters. The evaluation of model(90) is then 0 to numerical precision because of the exponential suppression by the Gaussian. If you start with 100 as the midpoint then it works fine:

sage: find_fit(data, model, initial_guess=[1,100,1]) 
[max == 405.75796954829985, mu == 111.86913960269014, sigma == 11.968861052746961]
edit flag offensive delete link more

Comments

Thanks a lot! It works great!

v_2e gravatar imagev_2e ( 2012-02-23 01:17:40 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2012-02-18 23:19:04 -0500

Seen: 442 times

Last updated: Feb 19 '12