Histogram with normal distribution and fixed-width bins?
이전 댓글 표시
Hello
I am trying to plot a histogram of a data set with fixed-width bins and overplot a normal distribution on it/fit the data to a normal distribution and plot it. I've been using the histfit function for the latter purpose, however the histfit function does not seem to allow a way where fixed-width bins can be plotted, while the hist() function does. Can someone please help me with this problem, i.e. how do I plot a histogram with fixed-width bins and get a normal distribution overplotted on that (so that it gives me mean and stdev). Thanks.
댓글 수: 2
Ilya
2012년 7월 31일
Can you clarify what you mean by "fixed-width bins"? histfit function can plot data only using bins of equal width. Do you mean that you need to specify the bin width?
Marmi Afrin
2012년 7월 31일
답변 (2개)
This lets you specify the range and bin width:
% Generate data
N = 100;
x = normrnd(0,1,N,1);
% Fit
[muhat,sigmahat] = normfit(x);
% Bin
xmin = -5; % lower bound
xmax = 5; % upper bound
h = 0.5; % bin width
edges = xmin:h:xmax;
% Histogram
n = histc(x,edges);
figure;
bar(edges,n,'histc');
% Overlay the distribution
hold;
f = @(z) N*h*normpdf(z,muhat,sigmahat);
fplot(f,[xmin xmax],'color','r');
hold off;
댓글 수: 7
Marmi Afrin
2012년 8월 1일
편집: Walter Roberson
2012년 8월 1일
Walter Roberson
2012년 8월 1일
Can the readings be off by more than the width of the scanner? If not, then the error is not normally distributed. If the error cannot be of arbitrarily large magnitude then it is not normally distributed.
Marmi Afrin
2012년 8월 1일
Ilya
2012년 8월 1일
Marmi, your fits look good. You do not need to convince anyone that the error can be arbitrarily large because nothing ever is arbitrarily large in the real world. The proof for the normality of your data is in the fits. You might want to perform a formal goodness-of-fit test at some point. Or you can just say that you are satisfied with the normal model if there is consensus between you and your colleagues about its validity.
Walter Roberson
2012년 8월 1일
Marmi Afrin
2012년 8월 2일
Marmi Afrin
2012년 8월 6일
Walter Roberson
2012년 7월 31일
0 개 추천
Is your input data bounded or unbounded? If it is bounded, then it is a logical error to fit a normal distribution to it, as normal distributions are always unbounded. If your data is unbounded, then it is a logical error to attempt to plot it with fixed width bins in a finite display media.
댓글 수: 3
Marmi Afrin
2012년 7월 31일
Walter Roberson
2012년 7월 31일
Your data is bounded. By definition then, it is not normally distributed, so fitting a normal distribution to it will give you garbage.
Please examine the properties of the Normal Distribution. You will see that for any finite x, the probability that the distribution is less than or equal to x is always non-negative.
If your data is such that there is some finite lower bound to it, then for your actual distribution the probability that the data is less than the lower bound, would be 0, which would be a violation of the possibility that the data forms a normal distribution (in which case the probability might be very small but would be non-negative.)
Bounded data is never normal distribution.
If your data is bounded, you might perhaps have a beta distribution, but never a normal distribution.
Ilya
2012년 8월 1일
Walter, any sample is bounded. This does not imply that any sample is not drawn from a normal distribution.
R = 1000;
N = 100;
minofx = zeros(N,1);
for r=1:R
minofx(r) = min(randn(N,1));
end
hist(minofx);
There may be things Marmi is not telling us about the data, but most certainly you cannot conclude that his fits are garbage just based on what he said.
카테고리
도움말 센터 및 File Exchange에서 Exploration and Visualization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!