pylab.hist(data, normed=1). Normalization seems to work incorrect pylab.hist(data, normed=1). Normalization seems to work incorrect numpy numpy

pylab.hist(data, normed=1). Normalization seems to work incorrect


See my other post for how to make the sum of all bins in a histogram equal to one:https://stackoverflow.com/a/16399202/1542814

Copy & Paste:

weights = np.ones_like(myarray)/float(len(myarray))plt.hist(myarray, weights=weights)

where myarray contains your data


According to documentation normed: If True, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1. Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen; it is not a probability mass function. This is from numpy doc, but should be the same for pylab.

In []: data= array([1,1,2,3,3,3,3,3,4,5.1])In []: counts, bins= histogram(data, normed= True)In []: countsOut[]: array([ 0.488,  0.,  0.244,  0.,  1.22,  0.,  0.,  0.244,  0.,  0.244])In []: sum(counts* diff(bins))Out[]: 0.99999999999999989

So simply normalization is done according to the documentation like:

In []: counts, bins= histogram(data, normed= False)In []: countsOut[]: array([2, 0, 1, 0, 5, 0, 0, 1, 0, 1])In []: counts_n= counts/ sum(counts* diff(bins))In []: counts_nOut[]: array([ 0.488,  0.,  0.244,  0.,  1.22 ,  0.,  0.,  0.244,  0.,  0.244])


I think you are confusing bin heights with bin contents. You need to add the contents of each bin, i.e. height*width for all bins. That should = 1.