Tutorials › Statistical Learning (Graduate) › Histogram Density Estimators

Nonparametric Density Estimation · Tutorial 594 of 989

Histogram Density Estimators

Learn how histogram estimates are formed from bin counts and how to calculate their integral, expectation, and variance.

Intermediate 9 min read

What You'll Learn

  • Define a histogram density estimator on a regular rectangular partition of bounded support
  • Verify that the estimator integrates to one for every sample in the support
  • Derive the estimator’s expectation as the average density within its bin
  • Derive its pointwise variance from the bin count’s binomial distribution
  • Calculate estimates and sampling variances in one- and two-dimensional examples
  • Recognize why bin volume and the partition convention matter

From Bin Counts to a Density Estimate

A histogram density estimator turns observations into a piecewise-constant estimate of a density. The central calculation is simple but easy to mis-scale: a bin’s count must be divided by both the sample size and the bin’s volume. Dividing only by the sample size estimates the probability in the bin, not a density. The volume adjustment converts that probability into density per unit volume.

Let \(X_1,\ldots,X_n\) be independent and identically distributed random vectors in \(\mathbb{R}^d\), each with density \(f\) with respect to \(d\)-dimensional Lebesgue measure. Suppose the distribution is supported on the bounded rectangle \(D=\prod_{k=1}^d[a_k,b_k]\), meaning \(P(X_i\in D)=1\). Partition each coordinate interval into a finite number of equal subintervals. Their Cartesian products form a regular rectangular partition of \(D\). Use a disjoint boundary convention, such as half-open cells except for the final cell on each coordinate, so that every point of \(D\) belongs to exactly one cell.

Definition: Write the cells of the partition as \(B_1,\ldots,B_M\), each with the same volume \(v>0\). For cell \(B_j\), its count is \(N_j=\sum_{i=1}^n \mathbf{1}\{X_i\in B_j\}\). The histogram density estimator is \(\hat f(x)=N_j/(nv)\) when \(x\in B_j\), and \(\hat f(x)=0\) when \(x\notin D\).

For a regular rectangular grid, if the cell widths along the \(d\) coordinates are \(h_1,\ldots,h_d\), then \(v=\prod_{k=1}^d h_k\). In one dimension, the volume is simply the bin width. In multiple dimensions, it is the product of the side lengths, not their sum. The estimate is constant within each cell and changes only when \(x\) crosses a cell boundary.

The partition and \(v\) are fixed when calculating the estimator’s sampling properties below. In particular, the results do not require the density to be continuous or bounded. For each cell define its probability \(\pi_j=P(X_i\in B_j)=\int_{B_j}f(u)\,du\). The probability \(\pi_j\) is dimensionless; dividing it by \(v\) gives a quantity with the units of a density.

Normalization: Every Sample Produces Total Mass One

A density must be nonnegative and integrate to one. A histogram estimate is nonnegative by construction. Its normalization is also exact, not merely true on average.

Theorem: For every sample \(X_1,\ldots,X_n\) lying in \(D\), the histogram estimator satisfies \(\int_{\mathbb{R}^d}\hat f(x)\,dx=1\).

Proof. On cell \(B_j\), the estimator is \(N_j/(nv)\), and the cell has volume \(v\). The integral over that cell is therefore \(\int_{B_j}\hat f(x)\,dx=(N_j/(nv))v=N_j/n\). The estimator is zero outside \(D\), so summing over the cells gives \[ \int_{\mathbb{R}^d}\hat f(x)\,dx =\sum_{j=1}^M\frac{N_j}{n} =\frac{1}{n}\sum_{i=1}^n\sum_{j=1}^M\mathbf{1}\{X_i\in B_j\}. \] Each observation belongs to exactly one cell, so the inner sum equals one for every \(i\). The expression is consequently \(n/n=1\). Since the observations lie in \(D\) with probability one, this normalization holds almost surely under the stated model. \(\square\)

This calculation explains why the bin volume is essential: each cell contributes its estimated height multiplied by its volume, giving \(N_j/n\), the observed fraction of the sample in that cell. The contributions across cells add to one because the observed fractions do.

Worked Example: Four Equal Bins Under a Uniform Density

Suppose \(X_1,\ldots,X_{20}\) are sampled from the uniform distribution on \([0,1]\), and divide the interval into four equal bins. Each bin has width \(v=1/4\). If the observed bin counts are \(4,6,3,7\), find the four histogram heights and verify normalization.

For each bin, the estimate is its count divided by \(nv=20(1/4)=5\). Thus the heights are \(4/5=0.8\), \(6/5=1.2\), \(3/5=0.6\), and \(7/5=1.4\).

The integral is the sum of the height in each bin multiplied by \(1/4\): \((0.8+1.2+0.6+1.4)(1/4)=4(1/4)=1\). Equivalently, the bins contribute \(4/20,6/20,3/20,\) and \(7/20\), which sum to one. The estimated density need not be exactly one in each bin, even though the true density is one.

Expectation and Variance Within a Bin

Fix a point \(x\in D\), and let \(B_j\) be the cell to which the boundary convention assigns it. The random value \(\hat f(x)\) depends on the sample only through \(N_j\). Because each observation independently falls in \(B_j\) with probability \(\pi_j\), the count \(N_j\) has a binomial distribution with sample size \(n\) and success probability \(\pi_j\). This gives both the mean and variance of the estimate.

Theorem: For \(x\in B_j\), let \(\pi_j=\int_{B_j}f(u)\,du\). Then \[ E[\hat f(x)]=\frac{\pi_j}{v} =\frac{1}{v}\int_{B_j}f(u)\,du, \qquad \operatorname{Var}(\hat f(x)) =\frac{\pi_j(1-\pi_j)}{nv^2}. \] Thus the expectation is the average value of the true density over the cell, and the variance depends on the probability mass in that cell.

Proof. Define \(I_{ij}=\mathbf{1}\{X_i\in B_j\}\), so \(N_j=\sum_{i=1}^n I_{ij}\). Each indicator has expectation \(E[I_{ij}]=\pi_j\) and variance \(\operatorname{Var}(I_{ij})=\pi_j(1-\pi_j)\). Independence of the observations makes the indicators independent as \(i\) varies. Therefore \(E[N_j]=n\pi_j\) and \(\operatorname{Var}(N_j)=n\pi_j(1-\pi_j)\). Since \(\hat f(x)=N_j/(nv)\), scaling gives \[ E[\hat f(x)]=\frac{E[N_j]}{nv}=\frac{\pi_j}{v} \quad\text{and}\quad \operatorname{Var}(\hat f(x)) =\frac{\operatorname{Var}(N_j)}{n^2v^2} =\frac{\pi_j(1-\pi_j)}{nv^2}. \] Finally, \(\pi_j=\int_{B_j}f(u)\,du\), so \(\pi_j/v\) is the integral of \(f\) over the cell divided by its volume. This is precisely the cell average of \(f\). \(\square\)

The expectation is not, in general, the true density value at every point of the bin. It is the same cell average for every \(x\in B_j\), because the estimate itself is constant there. The formula holds for any integrable density supported on \(D\); interpreting \(f(x)\) at a particular point additionally depends on the chosen representative of a density, which is defined only up to changes on sets of measure zero.

Worked Example: A Nonuniform Density on Two Bins

Let \(f(x)=2x\) on \([0,1]\), and \(f(x)=0\) elsewhere. Divide \([0,1]\) into \(B_1=[0,1/2)\) and \(B_2=[1/2,1]\). With \(n=12\), calculate the expectation and variance of the histogram height in each bin.

First find the bin probabilities: \(\pi_1=\int_0^{1/2}2x\,dx=[x^2]_0^{1/2}=1/4\) and \(\pi_2=\int_{1/2}^{1}2x\,dx=[x^2]_{1/2}^{1}=1-1/4=3/4\). Each bin has volume \(v=1/2\).

The expected heights are \(\pi_1/v=(1/4)/(1/2)=1/2\) and \(\pi_2/v=(3/4)/(1/2)=3/2\). These are the average density values over the two bins. For either bin, \(\pi_j(1-\pi_j)=3/16\); hence \(\operatorname{Var}(\hat f(x))=(3/16)/(12(1/2)^2)=(3/16)/3=1/16\) for \(x\) in that bin. The two variances agree here because the bin probabilities are \(1/4\) and \(3/4\), which give the same product \(\pi_j(1-\pi_j)\).

Worked Example: A Rectangular Partition in Two Dimensions

Let \(X_i=(X_{i1},X_{i2})\) be uniform on \(D=[0,2]\times[0,1]\). Divide the first coordinate into two intervals of width \(1\), and the second into two intervals of width \(1/2\). Each of the four rectangular cells has volume \(v=1(1/2)=1/2\). Suppose \(n=40\), and the observed counts in the four cells are \(8,12,10,10\). Calculate the estimated heights, verify their integral, and give the pointwise variance in any cell.

The density of the uniform distribution is \(1/\operatorname{Vol}(D)=1/2\), so each cell has probability \((1/2)(1/2)=1/4\). The denominator for each estimated height is \(nv=40(1/2)=20\). The four heights are therefore \(8/20=0.4\), \(12/20=0.6\), \(10/20=0.5\), and \(10/20=0.5\).

Their integrated mass is \((0.4+0.6+0.5+0.5)(1/2)=2(1/2)=1\). For any cell, the variance is \(\pi_j(1-\pi_j)/(nv^2)=(1/4)(3/4)/(40(1/2)^2)=(3/16)/10=3/160=0.01875\). The expected height is \(\pi_j/v=(1/4)/(1/2)=1/2\), although an individual observed height, such as \(0.4\), can differ from it.

Interpreting the Formulas and Avoiding a Scaling Error

The variance formula can also be expressed in terms of the expected height. If \(\bar f_j=\pi_j/v\), then substituting \(\pi_j=v\bar f_j\) gives \(\operatorname{Var}(\hat f(x))=\bar f_j(1-\pi_j)/(nv)\) for \(x\in B_j\). This form makes the role of volume visible: a smaller cell generally has fewer observations, so the estimate can be more variable. The factor \(1-\pi_j\) also reflects that a bin count is binomial rather than an unconstrained count.

A frequent mistake is to estimate the density in \(B_j\) by \(N_j/n\). That is the empirical fraction of observations in the bin, and it estimates \(\pi_j\), the bin’s probability mass. To obtain a density height, divide that fraction by the bin volume: \((N_j/n)/v=N_j/(nv)\). Another error is to use the side length in place of volume in multiple dimensions. For a rectangle with side lengths \(h_1,\ldots,h_d\), its volume is their product, so the correct denominator is \(n\prod_{k=1}^d h_k\).

Key takeaway: On a regular partition, a histogram’s height in a cell is its observed sample fraction divided by the cell volume. Its integral is exactly one, its expectation is the true density averaged over the cell, and its variance is determined by that cell’s binomial count.

Check Your Understanding

Use the definitions and formulas above to answer these questions.

  1. A one-dimensional bin has width \(0.2\), contains 15 observations, and the sample size is 100. What histogram height does it contribute?
  2. Why does the expected estimate at a point in a bin equal a cell average rather than necessarily the true density at that point?
  3. For a cell with probability \(\pi_j=0.1\), sample size \(n=50\), and volume \(v=0.2\), calculate the pointwise variance.
  4. A two-dimensional cell has side lengths \(0.4\) and \(0.5\). What volume belongs in the estimator’s denominator?
  5. What property of the partition and sample ensures that the histogram estimate integrates to one?
Clinical Biostats