How to fit a gaussian to unnormalized data

16 visualizaciones (últimos 30 días)
Niklas Kurz
Niklas Kurz el 3 de Sept. de 2021
Editada: Niklas Kurz el 4 de Sept. de 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 comentarios
Jeff Miller
Jeff Miller el 4 de Sept. de 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 el 4 de Sept. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Paul
Paul el 4 de Sept. de 2021
Editada: Paul el 4 de Sept. de 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 comentario
Niklas Kurz
Niklas Kurz el 4 de Sept. de 2021
Editada: Niklas Kurz el 4 de Sept. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by