How to fit a gaussian to unnormalized data
이전 댓글 표시
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
Niklas Kurz
2021년 9월 4일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
