Histograms V: choosing the binwidth by cross-validation
Archive 2011–2014
Density estimation
Histograms
R
A fully data-driven binwidth for histograms: the leave-one-out criterion reduces to a simple formula in the bin counts.
Author
Maikol Solís
Published
April 17, 2014
Modified
November 30, 2026
This post belongs to a series of study notes I first published between 2011 and 2014. I revised it in 2026: the mathematics was checked and corrected, the text rewritten, and an R example added.
Back to histograms. Let \(X_1, \ldots, X_n\) be i.i.d. with unknown density \(f\). In Histograms I we built the estimator
But \(h_{\text{opt}}\) depends on the unknown \(\|f'\|_2\), so it cannot be used directly. In Histograms IV I cheated: I used the value of \(\|f'\|_2\) for the normal density, which worked only because the data really were normal. This post derives a fully data-driven binwidth using cross-validation, the same idea as in the bandwidth selection post, specialized to histograms.
Removing \(X_i\) from the estimator evaluated at \(X_i\) makes \(\hat f_{h,-i}\) independent of \(X_i\). One can then show that \(\mathbb{E}\,\hat J(h) = \mathbb{E}\,J(h) = \operatorname{MISE}(h) - \int f^2\)(Tsybakov 2009, sec. 1.4, gives the argument for kernel estimators, and it carries over to histograms unchanged). So \(\hat J\) is an unbiased estimate of the risk, up to a constant that does not depend on \(h\).
Equation 1 looks hard to minimize, but for histograms it simplifies a lot.
Simplifying for histograms
Let \(N_k = \sum_{i=1}^n \mathbf{1}(X_i \in B_k)\) be the number of observations in bin \(B_k\), so that \(\sum_k N_k = n\).
First term. For \(x \in B_k\), \(\hat f_h(x) = N_k/(nh)\). The bins are disjoint, so the cross products \(\mathbf{1}(x \in B_k)\mathbf{1}(x \in B_l)\) vanish for \(k \ne l\), and
This is a statistic that depends only on the bin counts. Writing \(\hat p_k = N_k/n\), it is the identity in Wasserman (2006, Theorem 6.15). We compute it on a grid of \(h\) values and take the minimizer.
Example
The formula takes a few lines of R. We test it on the same bimodal mixture as in the bandwidth post, \(\tfrac12 N(-2,1) + \tfrac12 N(2,1)\), and compare with Scott’s rule \(3.49\,\hat\sigma\, n^{-1/3}\) from Histograms IV.
set.seed(2014)n <-400x <-c(rnorm(n /2, -2), rnorm(n /2, 2))J_hat <-function(h, x) { n <-length(x) Nk <-tabulate(floor((x -min(x)) / h) +1) # bin counts, origin at min(x)2/ ((n -1) * h) - (n +1) / (n^2* (n -1) * h) *sum(Nk^2)}hgrid <-seq(0.05, 2, by =0.005)J <-sapply(hgrid, J_hat, x = x)h_cv <- hgrid[which.min(J)]h_scott <-3.49*sd(x) * n^(-1/3)round(c(cross_validation = h_cv, scott = h_scott), 3)
Figure 1: Left: the cross-validation criterion as a function of the binwidth. Right: histograms with the cross-validated binwidth (blue) and with Scott’s rule (red outline), against the true density.
Since we know the true \(f\) here, we can also compute the actual ISE of each histogram:
Scott’s rule assumes normal data. Here the sample standard deviation is inflated by the distance between the two modes, so the rule picks wider bins (\(h = 1.07\) against \(0.76\)). Even so, both histograms show the two modes, and in this sample their ISEs are almost identical. That is less surprising than it looks: the ISE of a histogram is flat around its minimum, so a range of binwidths performs about equally well.
The lesson is not that cross-validation always wins. It reached the same accuracy without assuming anything about \(f\), whereas Scott’s rule was lucky that a bimodal density with well-separated modes is still forgiving. The left panel shows the price of cross-validation: \(\hat J(h)\) is flat and noisy near its minimum, so the selected binwidth varies noticeably from sample to sample.