Wrong width when fitting a gaussian with SciPy

In summary, the author is trying to fit a gaussian function to data, but it does not look correct. He tried changing the definition of the gaussian to normalize it, but that did not work.
  • #1
carllacan
274
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
  • #2
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.
 
  • #3
What is f and how come you are not using the Gaussian lambda function you defined?
 
  • #5
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?
 
  • #6
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.
 
  • #7
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.
 

Related to Wrong width when fitting a gaussian with SciPy

1. Why is the fitted gaussian curve not matching my data points?

There could be several reasons for this. One possibility is that the initial guess for the parameters of the gaussian curve is not close enough to the actual values. Another possibility is that the data points have a lot of noise or outliers, making it difficult for the curve fitting algorithm to accurately determine the parameters.

2. Can I manually adjust the parameters to improve the fit?

Yes, you can manually adjust the initial guess for the parameters or set bounds for the parameters to improve the fit. However, this may require some trial and error to find the optimal values.

3. What is the difference between a gaussian curve and a normal distribution?

A gaussian curve is a specific type of curve, also known as a bell curve, that follows the mathematical function of a gaussian distribution. A normal distribution is a type of probability distribution that is characterized by a bell-shaped curve.

4. How do I know if the fitted gaussian curve is a good fit for my data?

One way to evaluate the fit is by looking at the coefficient of determination (R-squared) value. A higher R-squared value indicates a better fit between the curve and the data points. Additionally, visually inspecting the curve and the data points can also give an idea of the fit.

5. Can I use SciPy to fit other types of curves to my data?

Yes, SciPy has various curve fitting functions, including polynomial, exponential, and power function fitting. You can use these functions to fit different types of curves to your data and choose the one that best describes the relationship between your variables.

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
948
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
8
Views
4K
Replies
2
Views
1K
  • Advanced Physics Homework Help
Replies
6
Views
1K
Replies
13
Views
2K
  • Special and General Relativity
3
Replies
75
Views
4K
Back
Top