Histograms I: construction
This post belongs to a series of study notes I first published in 2011, at the start of my PhD. I revised it in 2026: the mathematics was checked and corrected and the text rewritten.
Let \(X_1, \ldots, X_n\) be i.i.d. random variables with an unknown density \(f\). The histogram is the simplest nonparametric estimator of \(f\).
Construction
- Choose an origin \(x_0\) and a binwidth \(h > 0\), and split the real line into the bins \[ B_j = \big[x_0 + (j - 1)h,\; x_0 + jh\big), \qquad j \in \mathbb{Z}. \]
- Count \(n_j\), the number of observations that fall in \(B_j\).
- Over each bin, draw a bar of width \(h\) and height \(\dfrac{n_j}{nh}\).
Dividing by \(nh\) rather than by \(n\) makes the total area of the bars equal to one, so the histogram is itself a density. Formally,
\[ \hat f_h(x) = \frac{1}{nh} \sum_{i=1}^{n} \sum_{j \in \mathbb{Z}} \mathbf{1}(X_i \in B_j)\, \mathbf{1}(x \in B_j), \]
where \(\mathbf{1}(\cdot)\) is the indicator function. For a given \(x\) only one bin contains it, so \(\hat f_h(x) = n_j / (nh)\) for \(x \in B_j\).
The estimator is constant on each bin: every \(x\) in \(B_j\) gets the same value as the bin’s midpoint \(m_j = x_0 + (j - \tfrac12)h\). That is restrictive, and both the choice of \(h\) and of the origin \(x_0\) change the picture. Kernel estimators, later in the series, remove the dependence on \(x_0\).
Why this formula?
The probability that an observation falls in \(B_j = [m_j - \tfrac h2, m_j + \tfrac h2)\) is the area under \(f\) over the bin:
\[ \mathbb{P}(X \in B_j) = \int_{m_j-h/2}^{m_j+h/2} f(u)\, du. \]
If \(f\) is continuous and \(h\) is small, this area is close to that of a rectangle of height \(f(m_j)\) and width \(h\):
\[ \int_{m_j-h/2}^{m_j+h/2} f(u)\, du \approx f(m_j)\, h. \tag{1}\]
A natural estimate of the same probability is the relative frequency of observations in the bin:
\[ \mathbb{P}(X \in B_j) \approx \frac{n_j}{n} = \frac{1}{n}\, \#\{i : X_i \in B_j\}. \tag{2}\]
Equating Equation 1 and Equation 2 and solving for \(f(m_j)\) gives
\[ \hat f_h(m_j) = \frac{1}{nh}\, \#\{i : X_i \in B_j\}, \]
which is exactly the histogram. The next post studies how far \(\hat f_h(x)\) is from \(f(x)\).
References
Further reading: Härdle et al. (2004, chap. 2).
Series: Density estimation · Previous: Density estimation · Next: Histograms II