Histogram with normal distribution and fixed-width bins?
24 ビュー (過去 30 日間)
古いコメントを表示
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?
回答 (2 件)
Ilya
2012 年 8 月 1 日
編集済み: Ilya
2012 年 8 月 1 日
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 件のコメント
Walter Roberson
2012 年 7 月 31 日
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 件のコメント
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!