Normalize histogram of normally distributed data
이전 댓글 표시
How can I normalize a histogram of normally distributed points? I have tried using histnorm from the file exchange and a few other suggestions in the help forums, but the height of my bins are on the order of 10^4.
I am using normrand to distribute data points, but the height of the histogram (or height of the pdf when I use histfit) always depends upon the number of data points and the number of bins I use.
Thanks
답변 (2개)
Honglei Chen
2013년 3월 12일
You just need to normalize it toward the number of points
x = randn(1024,1)
[n,b] = hist(x)
bar(b,n/sum(n))
댓글 수: 2
Bernoulli Lizard
2013년 3월 13일
Honglei Chen
2013년 3월 13일
편집: Honglei Chen
2013년 3월 14일
Then what is your rule of normalization, normalize towards what?
Tom Lane
2013년 3월 14일
0 개 추천
I would normalize it to area 1. The usual histogram has area equal to binwidth*sum(n), so divide by that. Probably binwidth=b(2)-b(1).
댓글 수: 6
Peter
2013년 6월 27일
What do you mean divide by "that"? Divide what by what?
In the above example 'n' is the number of particles in a given bin. So if I divide that vector by the bin width the height of the histogram depends upon the total number of particles that are being binned. The height is also many orders of magnitude greater than 1, whereas it should of course always be <= 1.
Peter
2013년 6월 27일
Since I want the histogram to represent the PDF, The actual height of the histogram should not change based upon the number of particles, the number of bins, or the width of the bins.
Image Analyst
2013년 6월 27일
Just divide by the total count, like Honglei said. Would that be what you want?
[counts, bins] = hist(data(:));
normalizedDataPDF = counts/sum(counts);
Peter
2013년 6월 27일
편집: Image Analyst
2013년 6월 27일
No, it is not. The histogram does not match the PDF. Also, it changes if I change the number of bins.
Here is the code that I am using:
% random positions FOR THE Z-AXIS
wz = 5.8118*10^-06;
PDF = @(z) 1 - exp( -beta*exp(-4*z.^2/wz^2) );
PDF_discrete = PDF(-span*wz:sizeZ:span*wz);
[~ , draw_samples] = histc(rand(numpart , 1), [0 cumsum(PDF_discrete)./sum(PDF_discrete)]);
position(:,3) = draw_samples .*sizeZ -(span*wz);
% Plot histogram of numbers distributed according to the PDF:
numbin = 1000;
[counts,bins] = hist(x,numbin);
binwidth = bins(2)-bins(1);
sum(counts/(binwidth*numpart*numbin))
bar(bins, counts/sum(counts) )
% Plot the PDF
cbl = @(z) 1 - exp( -beta*exp(-4*z.^2/wz^2) );
ezplot(cbl, [-3*wz, 3*wz]);
Image Analyst
2013년 6월 27일
We can't run this - it says span is undefined. The histogram is a quantized PDF. You won't get the true PDF unless you have an infinite number of infinitely thin histogram bins. So of course the histogram changes as you change the bin width - you have a finite number of samples that are counted. And you can't get infinitely thin, infinitely many bins with digitized, discrete data because that's just how a computer works.
Peter Perkins
2013년 6월 27일
You want to compare a histogram to a PDF. Tom's suggestion is the correct one. The histogram integrates to binwidth*numObservations, the PDF to 1. The easiest thing to do is to scale your PDF by multiplying by binwidth*numObservations, but you can also call hist, get the bin counts, normalize them by dividing by binwidth*numObservations, and call bar.
If your PDF is a fit to the data, I'd recommend plotting the data and fitting the model using the dfittool GUI, which makes the plot your looking for (as well as many others) automatically.
Hope this helps.
카테고리
도움말 센터 및 File Exchange에서 Histograms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!