Histogram with normal distribution and fixed-width bins?
Mostra commenti meno recenti
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 Commenti
Ilya
il 31 Lug 2012
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
il 31 Lug 2012
Risposte (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 Commenti
Marmi Afrin
il 1 Ago 2012
Modificato: Walter Roberson
il 1 Ago 2012
Walter Roberson
il 1 Ago 2012
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
il 1 Ago 2012
Ilya
il 1 Ago 2012
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
il 1 Ago 2012
Marmi Afrin
il 2 Ago 2012
Marmi Afrin
il 6 Ago 2012
Walter Roberson
il 31 Lug 2012
0 voti
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 Commenti
Marmi Afrin
il 31 Lug 2012
Walter Roberson
il 31 Lug 2012
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
il 1 Ago 2012
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.
Categorie
Scopri di più su Exploration and Visualization in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!