How Is Uncertainty Calculated for the Mean of a Gaussian Function in MATLAB?

  • Context: Undergrad 
  • Thread starter Thread starter kelly0303
  • Start date Start date
  • Tags Tags
    Error Gaussian Mean
Click For Summary

Discussion Overview

The discussion revolves around calculating the uncertainty on the mean of a Gaussian function in MATLAB, particularly in the context of experimental data with associated errors. Participants explore the implications of using a Gaussian versus a Lorentzian lineshape and the methods for fitting such functions to data.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant asks for a general formula for the uncertainty on the mean of a Gaussian function given N points with associated errors of ##\sqrt{y}##.
  • Another participant seeks clarification on the meaning of the error associated with each y value, suggesting a need for more context.
  • A participant describes a specific experimental setup involving counting experiments and how the uncertainty in counts relates to the Gaussian fitting process.
  • Some participants argue that the lineshape may be Lorentzian rather than Gaussian, suggesting that the fitting approach should reflect this distinction.
  • Others counter that atomic lines can be Gaussian, particularly when broadened by Doppler shifts, and mention the Voigt profile as a more general description.
  • A later reply discusses the use of MATLAB's nlinfit function to extract the covariance matrix, which can be used to determine the uncertainty in the fitted parameters.
  • Example MATLAB code is provided to illustrate the fitting process and how to calculate the uncertainty on the linewidth from the covariance matrix.

Areas of Agreement / Disagreement

Participants express differing views on whether the lineshape should be considered Gaussian or Lorentzian, indicating a lack of consensus on this aspect. The discussion remains unresolved regarding the best approach to calculate the uncertainty on the mean.

Contextual Notes

Some limitations include the potential conflation of Gaussian and Lorentzian shapes, and the dependence on the specific experimental context and definitions used in the discussion.

kelly0303
Messages
573
Reaction score
33
Hello! If I have N points (x,y) which I know they are described by a Gaussian i.e. y(x) is a Gaussian of unknown mean and standard deviation, and each y has an associated error of ##\sqrt{y}##, is there a general formula for the uncertainty on the mean of this function? Thank you!
 
Physics news on Phys.org
It is not clear to me what you are asking. In particular what does "each y has an associated error of ##\sqrt y##" mean? Perhaps you could further describe the system ?
 
hutchphd said:
It is not clear to me what you are asking. In particular what does "each y has an associated error of ##\sqrt y##" mean? Perhaps you could further describe the system ?
I meant that the uncertainty on y is ##\sqrt{y}##, which is usually the case with counting experiments. An example of such a system is the measurement of the transition between 2 levels of a system, where the lineshape is Gaussian. One sets the laser at a given frequency for a while and measures the number of counts (e.g. fluorescent photons from the induced transition), then the frequency is changed and the number of counts are measured again. In the end, one ends up with a plot of counts (or rate) vs frequency, where the uncertainty on the number of counts, N is ##\sqrt{N}##. Then, in order to get the central value of that transition, one needs to fit a Gaussian to N vs frequency. My question is, is there a general formula for the uncertainty on the mean of this gaussian under these circumstances?
 
kelly0303 said:
My question is, is there a general formula for the uncertainty on the mean of this gaussian under these circumstances?
I fear you are conflating two ideas here.
In your example, the lineshape I believe will be Laurentzian (not Gaussian) and the width arises from the intrinsic physics. Given that shape, the best fit of a Laurentzian to the data will provide an estimate of the transition frequency and width. It is not asymmetric distribution.
The uncertainty in the measurement at each frequency, as driven by by signal strength or integration time, will indeed go as ##\sqrt N## and the fitting procedure to a parameterized curve can be weighted accordingly. One can certainly derive a relationship for the goodness of this fit in terms of the uncertianty in the data. These depend upon slopes and curvatures of the fitted curve near the data points in question and the uncertainties.
 
hutchphd said:
In your example, the lineshape I believe will be Laurentzian (not Gaussian) and the width arises from the intrinsic physics. Given that shape, the best fit of a Laurentzian to the data will provide an estimate of the transition frequency and width. It is not asymmetric distribution.
Atomic lines aren't always Lorentzian. They are often Gaussian, and more generally they are described by a Voigt profile (a convolution of the two). One example is an atomic transition that is broadened by the Doppler shifts of all the atoms flying around with thermal velocities. This will produce a Gaussian lineshape, reflecting the Maxwell-Boltzmann velocity distribution.

kelly0303 said:
Hello! If I have N points (x,y) which I know they are described by a Gaussian i.e. y(x) is a Gaussian of unknown mean and standard deviation, and each y has an associated error of y, is there a general formula for the uncertainty on the mean of this function? Thank you!
What you want is to do a Levenburg-Marquardt (or whatever other fitting algorithm you prefer) to your data, and extract the covariance matrix. Matlab's "nlinfit" function does this, and I believe scipy.optimize.least_sqaures does the same when you set method='lm'. (I'm not very familiar with scipy, sorry!) For the scipy method, it only returns the jacobian, which you can use to generate the covariance matrix from the error bars on your data points via usual error propagation formulas. Essentially, the Jacobian is the derivative of the fit parameters versus each of your data points ##y_i##. Using those derivatives and the error bars on each ##y_i##, you can calculate the error bars on the fit parameters. Matlab does calculates the covariance matrix for you (it's the 4th output of nlinfit).

Once you have the covariance matrix, you'll want to look at the diagonal component that corresponds to the gaussian width. This will be the variance on that width, so if you take the square root you will get the standard deviation. I hope that's somewhat helpful!
 
I realize my description wasn't very good. Here's some example code for matlab. I wasn't able to check it because I don't have MATLAB at home. I believe nlinfit requires the statistics and machine learning toolbox. It's very useful though!
[CODE lang="matlab" title="GaussianFitting"]% Make some data
x = 0:0.1:5;
y = 5*exp(-((x-3)/0.71).^2) + 1.7;
yerr = 0.1 + 0.2*rand(size(y));

% Plot the data
figure(1); clf;
errorbar(x,y,yerr,'ks')

% define a gaussian function to fit to
fitfun = @(b,x) b(1)*exp(-((x-b(2))/b(3)).^2)+b(4);
% b(1) is the amplitude of the gaussian line
% b(2) is the center value
% b(3) is the linewidth
% b(4) is the background

% define a vector of inverse-variance weights
w = yerr.^(-2);

% define a guess for the parameters of the gaussian
b0 = [1,0,1,0];

% use nlinfit to fit the data to a gaussian
[b,~,~,covB] = nlinfit(x,y,fitfun,b0,'weights',w);

% plot the result
hold on
plot(x,fitfun(b,x),'k--')
hold off

% print the uncertainty on the linewidth to the command window
display(['The linewidth uncertainty is: ',num2str(sqrt(covB(3,3)))])[/CODE]
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
Replies
28
Views
4K
Replies
15
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K