How to fit a gaussian to unnormalized data

8 vues (au cours des 30 derniers jours)
Niklas Kurz
Niklas Kurz le 3 Sep 2021
Modifié(e) : Niklas Kurz le 4 Sep 2021
I do know this question has been asked in several kinds plus it's rather a mathematical question for mathstack like sites.
But here I am, bothering you with my data-points.
I've got the X-values
X = -6:1:6;
and Y values, corresponding to how often each X value was hit.
Y = [1 3 1 8 5 16 18 10 6 2 1 1 0];
For later on I calculated Mean and standard deviation as followed:
mean = sum(X.*Y)/(sum(Y));
std = 0;
for i =1:1:size(Y,2)
std = std+ Y(i).*(X(i)-m).^2;
end
std = sqrt(std/(n-1));
Now to the crucial part: fitting the data to a gaussian curve.
First of I normalized the data: Heres probably my problem located:
Yn = Y/max(Y)
Actually the normalization should lead to a total area of one but
trapz(X,Yn)
is not equal to one. I use it anyways.
In cftool I rigorously typed in the gaussian distribution equation for fitting:
1/(sqrt(2*pi)*s)*exp(-(x-m)^2/(2*s^2)) % alias: s/std m/mean
It doesn't happen to fit the data points quite well.
Also it's deviating from plotting the eqatuion above with mean and std calculated
I still believe something with the normalization turned out wrong.
You can name what?
  2 commentaires
Jeff Miller
Jeff Miller le 4 Sep 2021
Why are you using cftool at all? The maximum likelihood estimates of the gaussian mu and sigma can be computed directly from the data, something like this (unchecked):
mu_est = sum(X.*Y)/(sum(Y));
sigma_est = sqrt( sum(Y.*((X-mu_est).^2)) / n); % note division by n rather than n-1 for maximum likelihood
Niklas Kurz
Niklas Kurz le 4 Sep 2021
Thank you for your simplified formula. I just dealed with normal distribution recently and mainly want to apply it to a distributed data set now. Determining μ and σ are subsidary. Merely of interest for comparision

Connectez-vous pour commenter.

Réponse acceptée

Paul
Paul le 4 Sep 2021
Modifié(e) : Paul le 4 Sep 2021
I think you need to normalize Y by it's sum (given the unit spacing of X), not its max
X = -6:1:6;
Y = [1 3 1 8 5 16 18 10 6 2 1 1 0];
Yn = Y/sum(Y);
Xvals = repelem(X,Y);
histogram(Xvals,'Normalization','pdf');
hold on
plot(X,Yn,'-o','LineWidth',1);
mu = mean(Xvals);
sigma = std(Xvals);
plot(X,normpdf(X,mu,sigma),'g-x','LineWidth',1)
  1 commentaire
Niklas Kurz
Niklas Kurz le 4 Sep 2021
Modifié(e) : Niklas Kurz le 4 Sep 2021
Yes! This is exactly what I am seeking. Truely thank you for proposing how to normalize data proberly but also suggesting that neat function repelem(X,Y). I'll probably ask the MathStack geeks seperately why this actually works but it kind of makes sense deviding by the total area bringing it down to one. It's just peculiar I havent't heard of the method before.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by