Wrong width when fitting a gaussian with SciPy

Click For Summary

Discussion Overview

The discussion centers around fitting a Gaussian function to data using SciPy, specifically addressing issues encountered when fitting the photopeak of Co60. Participants explore the implementation of the fitting process, the definition of the Gaussian function, and the implications of normalization on the fitting results.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant shares their code for fitting a Gaussian to data and expresses confusion over the unexpected width of the fitted Gaussian.
  • Several participants point out a potential error in the code regarding the undefined variable 'f', suggesting it should be replaced with the correctly defined Gaussian function 'gauss'.
  • A participant mentions a recent article on Python debugging, though its relevance to the current issue is questioned.
  • Another participant proposes that the issue may stem from the variance being zero, based on the plot provided.
  • One participant reports a resolution by modifying the Gaussian function to include a normalization parameter, leading to a significant change in the fitting behavior. They speculate that the original function was normalized as a probability density function (PDF), while the data is not.

Areas of Agreement / Disagreement

Participants generally agree on the need to clarify the function used in the fitting process and acknowledge the impact of normalization on the fitting results. However, there is no consensus on the implications of the changes made to the Gaussian function or the nature of the data being fitted.

Contextual Notes

Participants discuss the potential for missing assumptions regarding the normalization of the Gaussian function and its effect on fitting, as well as the dependence on the specific implementation details in the code.

carllacan
Messages
272
Reaction score
3
Hi.

Has anybody here any experience with SciPy? I'm trying to get SciPy to adjust a gaussian function to some data. For more details its the photopeak of Co60. This is what I do:
Code:
        import numpy as np
        from scipy.optimize import curve_fit
        # counts is a numpy array which holds the number of counts for each channel
        # start is the position in the count array where the peak starts, and
        # end is the position where the peak ends, both guesstimated by eye

        # define the gaussian function
        gauss = lambda x, u, v: (1 / (v*np.sqrt(2*np.pi)) * np.exp(-(x-u)**2/(2*v**2)))

        # create the space over which the gaussian should be fitted
        x = np.linspace(start, end, end - start)

        # the initial parameters, estimated from the start and end positions
        a0 =[ (start + end)/2, (end - start)/(4*np.log(2))]
 
        # fit the gaussian function over the interval x to the datapoints counts[data:end]
        fit = curve_fit(f, x, counts[start:end], a0)
        mean = fit[0][0]
        var = fit[0][1]

The result is not what I would expect, though, though:

gaussian.png


The green line is my fit, the blue one the original data. The gaussian should b much wider!

What am I doing wrong?

Thank you for your time.
 
Technology news on Phys.org
Third line from the bottom of your code:
carllacan said:
Python:
fit = curve_fit(f, x, counts[start:end], a0)
What is f?
I don't it mentioned anywhere in your code. As a guess, maybe the first parameter should be gauss, the function you define previously.
 
What is f and how come you are not using the Gaussian lambda function you defined?
 
gsal said:
What is f and how come you are not using the Gaussian lambda function you defined?
Yes, its actually gauss in my code. I was tweaking things to try to make it work and I left that.

I can't edit my post any more... I wish there was a function to add a PD to the end or something.
Mark44 said:
Not to toot my own horn, but I just wrote an Insights article on Python debugging...
https://www.physicsforums.com/insights/simple-python-debugging-pdb-part-1/
Part 1 is published, and Part 2 should be published tomorrow sometime.
Well, it seems interesting, but does it apply to this?
 
carllacan said:
Yes, its actually gauss in my code. I was tweaking things to try to make it work and I left that.

I can't edit my post any more... I wish there was a function to add a PD to the end or something.

Well, it seems interesting, but does it apply to this?
Likely it does. You could see what values your program is using for mean and var. From your plot, it looks like var might be zero.
 
Well, problem solved in a weird way.

I tried changing the definition of the gaussian to
Python:
gauss = lambda x, a, u, v: a*np.exp(-(x-u)**2/(2*v**2))

That is, adding a parameter for the normalization instead of normalizing with the variance. And all of a sudden problem solved.

I'd like to know how could this possibly change the behaviour of the fit so dramatically. My guess: the first gaussian function I used is normalized so that its integral over all the space is 1 (because it is a PDF), while my data follows an unnormalized gaussian, not a PDF. Am I right?

Thank you for your time.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 10 ·
Replies
10
Views
4K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K