Probability density function numpy histogram/scipy stats

DimKoim picture DimKoim · May 19, 2015 · Viewed 8k times · Source

We have the arraya=range(10). Using numpy.histogram:

hist,bins=numpy.histogram(a,bins=(np.max(a)-np.min(a))/1, range=np.min(a),np.max(a)),density=True)

According to numpy tutorial:

If density=True, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1.

The result is:

array([ 0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.2])

I try to do the same using scipy.stats:

mean = np.mean(a)
sigma = np.std(a)
norm.pdf(a, mean, sigma)

However the result is different:

array([ 0.04070852,  0.06610774,  0.09509936,  0.12118842,  0.13680528,0.13680528,  0.12118842,  0.09509936,  0.06610774,  0.04070852])

I want to know why.

Update:I would like to set a more general question. How can we have the probability density function of an array without using numpy.histogram for density=True ?

Answer

jtitusj picture jtitusj · May 19, 2015

If density=True, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1.

The "normalized" there does not mean that it will be transformed using a Normal Distribution. It simply says that each value in the bin will be divided by the total number of entries so that the total density would be equal to 1.