How to fit a gaussian to unnormalized data
조회 수: 16 (최근 30일)
이전 댓글 표시
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
Jeff Miller
2021년 9월 4일
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
채택된 답변
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)
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!