2
$\begingroup$

I have a sample of people's heights. If I histogram it, I would usually divide by the bin width, so that my result becomes independent of my bin width, and I would label the axis something like (number of observations per unit height). If I do a KDE instead, it comes as a normalised distribution. To get that into physical units, do I simply multiply by the number of samples that I have?

enter image description here

import requests # pip install requests for easy http request for CSV data
import csv

# Get sample data:
url = "http://www.randomservices.org/random/data/Galton.txt"
with requests.Session() as s:
    download = s.get(url)
    decoded_content = download.content.decode('utf-8')

data_iter = csv.reader(decoded_content.splitlines(), delimiter='\t')
data = [data for data in data_iter]

# Organise it:
height = np.zeros((len(data)-1,1))
for i in np.arange(1,len(data)):
    height[i-1] = data[i][4]

# Plot it:
for binWidth in [1,2,5]:
    bins,edges = np.histogram(height,bins=np.arange(55,80,binWidth))
    plt.bar(edges[:-1]+0.5*np.mean(np.diff(edges)),bins/binWidth,alpha=0.5,label='binWidth=%g'%binWidth,width=binWidth)
plt.xlabel('Height (inches)'); plt.ylabel('dN/dH (Count/Inch)')

kernel = stats.gaussian_kde(height.flatten())
plt.plot(np.linspace(40,90,901),len(height)*kernel(np.linspace(40,90,901)),'k',label='KDE $\\times$ Nsamples')
plt.xlim(40,90);
plt.legend()
$\endgroup$

1 Answer 1

3
$\begingroup$

Yes, you do. Both (normalized) KDE and a (normalized) histogram are approximations of the probability distribution of a random variable, however the former is discrete and the latter is continuous.

For a discrete i.i.d random variable, the expected number of observations that have a certain value is just $\langle n_i \rangle = n p_i$, since it is the expected value of a multinomial distribution.

For a continuous i.i.d random variable, you achieve the same effect by scaling the probability density. The physical meaning is that, for any interval $[a,b]$ of the random variable $x$, the expected number of observations falling into that interval is $n\int_a^b\rho(x)dx$, which is just the fraction of observations falling into that interval times the number of observations, same as in the discrete case.

$\endgroup$
2
  • $\begingroup$ Thanks! Is there a convention for naming the y-axis? As in I did dN/dH -- as in dNumber/dHeight such that integrating over all H will leave you with the number of observations. $\endgroup$
    – James
    Jun 28, 2022 at 22:23
  • $\begingroup$ @James Sorry I don't know. I think as long as you are consistent in your work, many sensible naming conventions are good enough. Yours seems fine. $\endgroup$ Jun 29, 2022 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.