How do I fit the Gaussian distribution?

The noise histogram is a Gaussian distribution as seen in the graph. I want to fit this histogram. I used 'histfit' command but it didn't give correct result. So I wrote code to manually fit it. However, as you can see, this does not give the correct result. How do I fit this histogram correctly? I would be very happy if you could help me with this subject.
noise_filt = cat(2,filt_noise{:});
mu = 0; %mean
noise_sigma = 29*10^-6; %std deviation
noise_varyans = noise_sigma^2;
x = -0.1:0.001:0.1;
pdf = (1/sqrt(2*pi*noise_sigma^2)) .* exp((-(x-mu).^2)/(2*noise_sigma^2));
histogram(noise_filt);
%histfit(noise_filt);
hold on;
plot(x,pdf,'LineWidth',2);
Undefined variable filt_noise.

 Accepted Answer

It gave a much better result when I ran it (R2021a) —
LD = load('noise_filt.mat');
noise_filt = LD.noise_filt;
figure
hhf = histfit(noise_filt)
df = fitdist(noise_filt(:), 'Normal')
df =
NormalDistribution
Normal distribution
mu = 3.27713e-05 [9.07426e-06, 5.64683e-05]
sigma = 0.00865728 [0.00864056, 0.00867407]
It won’t be exact unless it has a ‘perfect’ normal distribution. That’s likely as good as it gets.
.

8 Comments

Thank you for your help. do you know how to calculate this mean squared error for this fitting?
As always, my pleasure!
I don’t. The mean squared error (and similar statistics) are generally calculated with respect to models fitted to data, using some sort of least-squares approach. Fitting a distribution is different. The fitdist function (that histfit uses) calculates a Maximum likelihood estimation. Looking through that reference, I cannot see where something like that is calculated.
The best I can do is:
hhf = histfit(noise_filt)
df = fitdist(noise_filt(:), 'Normal')
y = normpdf(hhf(1).XData,df.mu,df.sigma);
mse = mean((y - hhf(1).YData).^2)
producing:
mse =
1.183472134374673e+07
However, I am not certain that is appropriate here.
.
Thank you very much for your help. Do you think it would be more helpful if I asked this as a separate question?
As always, my pleasure!
Do you think it would be more helpful if I asked this as a separate question?
You certainly can if you want to, and I appreciate your asking my opinion on it.
I am not certain that calculating the MSE of a MLE fit is appropriate, however others may have different ideas on how to do it. It would be interesting to see if anyone else has a better approach.
Please post the URL to that Question here to make it easier for me to find it and to follow the responses.
.
Hello again, I wanted to consult a few things with you before I create a question. I got a better image when I set the number of bars in the histogram to 100. You shared a code for the MSE account of this in the past years. "https://uk.mathworks.com/matlabcentral/answers/168984-how-to-get-the-error-value-when-fitting-a-gaussin-curve-to-a-data" can't something like this link be done? Also "https://uk.mathworks.com/matlabcentral/answers/1456419-how-can-i-calculate-mse-for-the-histogram?s_tid=srchtitle " I want to make an mse account for the weibull distribution in this link. I would be glad if you help.
You can use the same approach on the Weibull distribution, with the same cautions I mentioned earlier. I seriously doubt that with a maximum likelihood estimation that ‘mean square error’ or others are appropriate. That could work with respect to fitting the histogram, although I doubt the legitimacy of it.
In my previous 2015 Answer, that was not actually fitting the histogram, instead determining the statistics of the approximation drawn by histfit, with respect to the histogram values. If that approach appears to do what you want, it would likely be an appropriate way to proceed.
.
I manually fit in the weibull distribution. I didn't use the histfit command. That's why I couldn't find a solution in the mse account. If you know any other way, please tell me, I'm so desperate. I created a question for Gaussian. https://uk.mathworks.com/matlabcentral/answers/1458414-how-to-calculate-mse-for-gaussian-histogram
The histfit function should work with the Weibull distribution, and wblfit should work to estimate the parameters and confidence intervals on them. Getting the ‘typical’ statistics that relate to a nonlinear fit with respect to a histogram and a specific distribution may not be the correct approach.
The distribution fitting functions (using the maximum likelihood estimate) fit the parameters of the distribution, not the histogram. Ths histogram is simply provided in order to understand the nature of the fitted distribution and how it relates to the data. The mlecov function can provide a covariance matrix on the parameters.
I’m sort of lost here. I thought we already solved this problem with the nonlinear fit to the histogram.
.

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!