Fitting Histogram Data to a Normal Distribution - How to incorporate chi-sq?
4 views (last 30 days)
Show older comments
Hi
I fitted a gaussian to a set of histogram data I had but my advisor told me that chi-sq is not minimized and the profile was computed using mu and stdev only and it isn't working with the three parameter mean stdev and chi-sq minimization. Can someone explain to me how normfit and normpdf do chi-sq minimization? I will admit that using built-in functions in matlab sometimes prevents me from being able to know what exactly is going on. Here is a jist of what my code is doing:
y=S(:,7)-Avg;
% Fit
N = sum(~isnan(y));
[muhat,sigmahat] = normfit(y);
% Bin
xmin = -5; % lower bound
xmax = 5; % upper bound
h = 0.1; % bin width
edges = xmin:h:xmax;
% Histogram
n = histc(y,edges);
bar(edges,n,'histc');
hbar = findobj(gca,'Type','patch');
set(hbar,'FaceColor','y','EdgeColor','k')
hold;
z=(-3*sigmahat+muhat:0.1*sigmaha:3*sigmahat+muhat);
f = N*h*normpdf(z,muhat,sigmahat);
plot(z,f,'r-','Linewidth',1.5);
xlim([-3.5 3.5]); set(gca,'XTick',[-3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3 3.5])
ylim([0 80]);grid on;
xlabel('Position Variations (um)');
ylabel('Frequency')
legend('Histogram','Normal Distribution Fit')
title('Average Subtracted Measurements - New Scanner (Data Set 1)');
text(-2.5,200,sprintf('mu = %1.2g\nstd = %1.4g',muhat,sigmahat));
hold off;
0 Comments
Answers (1)
Tom Lane
on 10 Aug 2012
The normfit function estimates mu and sigma from the raw data.
It's possible to estimate mu and sigma from the histogram bin counts instead. The histogram gives observed values O for each bin. Using mu and sigma you can compute expected values E for each bin. You could compute a chi-square measure such as sum(((O-E).^2 ./ E), and you could select mu and sigma to minimize that. But that's not what normfit does. You might be able to carry this out yourself using fminsearch.
2 Comments
Tom Lane
on 10 Aug 2012
I am thinking of E being a function of mu and sigma, and using something like fminsearch to select the values of mu and sigma that minimize the chi-square criterion.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!