MATLAB Answers

How to fit a gaussian to unnormalized data

조회 수: 2(최근 30일)
Niklas Kurz
Niklas Kurz 2021년 9월 3일
편집: Niklas Kurz 2021년 9월 4일
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
Niklas Kurz
Niklas Kurz 2021년 9월 4일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Paul
Paul 2021년 9월 4일
편집: Paul 2021년 9월 4일
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
Niklas Kurz
Niklas Kurz 2021년 9월 4일
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.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by