Skip to main content

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

\[ \hat f_h(x) = \frac{1}{nh}\sum_{i=1}^n \sum_k \mathbf{1}(X_i \in B_k)\, \mathbf{1}(x \in B_k), \]

where the bins \(B_k\) form a partition of the real line into intervals of length \(h\). In Histograms III we minimized the AMISE and found

\[ h_{\text{opt}} = \left( \frac{6}{n\,\|f'\|_2^2} \right)^{1/3} \propto n^{-1/3}. \]

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.

The criterion

The integrated squared error is

\[ \operatorname{ISE}(h) = \int \big(\hat f_h(x) - f(x)\big)^2 dx = \int \hat f_h^2(x)\,dx - 2\int \hat f_h(x) f(x)\,dx + \int f^2(x)\,dx . \]

The last term does not depend on \(h\), so we can minimize

\[ J(h) = \int \hat f_h^2(x)\,dx - 2\int \hat f_h(x) f(x)\,dx \]

instead. The first term is computable from the data. The second involves \(f\), so we replace it with a leave-one-out estimate, as in the kernel case:

\[ \hat J(h) = \int \hat f_h^2(x)\,dx - \frac2n\sum_{i=1}^n \hat f_{h,-i}(X_i), \qquad \hat f_{h,-i}(x) = \frac{1}{(n - 1)h}\sum_k \sum_{j \ne i} \mathbf{1}(X_j \in B_k)\, \mathbf{1}(x \in B_k). \tag{1}\]

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

\[ \int \hat f_h^2(x)\,dx = \frac{1}{n^2h^2}\int \Big(\sum_k N_k\, \mathbf{1}(x \in B_k)\Big)^2 dx = \frac{1}{n^2h^2}\sum_k N_k^2 \int \mathbf{1}(x \in B_k)\,dx = \frac{1}{n^2 h}\sum_k N_k^2 . \tag{2}\]

Second term. Rearranging the sums,

\[ \sum_{i=1}^n \hat f_{h,-i}(X_i) = \frac{1}{(n - 1)h}\sum_k \sum_{i=1}^n \mathbf{1}(X_i \in B_k) \sum_{j \ne i} \mathbf{1}(X_j \in B_k). \]

The inner sum equals \(N_k - \mathbf{1}(X_i \in B_k)\). Since \(\mathbf{1}(\cdot)^2 = \mathbf{1}(\cdot)\),

\[ \sum_{i=1}^n \hat f_{h,-i}(X_i) = \frac{1}{(n - 1)h}\sum_k \big(N_k^2 - N_k\big) = \frac{1}{(n - 1)h}\Big(\sum_k N_k^2 - n\Big). \tag{3}\]

Putting it together. Substituting Equation 2 and Equation 3 into Equation 1,

\[ \hat J(h) = \frac{1}{n^2h}\sum_k N_k^2 - \frac{2}{n(n - 1)h}\Big(\sum_k N_k^2 - n\Big) = \frac{2}{(n - 1)h} - \frac{n + 1}{n^2(n - 1)h}\sum_k N_k^2 . \]

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 <- 400
x <- 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)
cross_validation            scott 
           0.760            1.069 
Code
f <- function(t) {
  0.5 * dnorm(t, -2) + 0.5 * dnorm(t, 2)
}
par(mfrow = c(1, 2), mar = c(4, 4, 1, 1))
plot(
  hgrid,
  J,
  type = "l",
  lwd = 2,
  col = "#1f4e79",
  xlab = "Binwidth h",
  ylab = expression(hat(J)(h))
)
abline(v = h_cv, col = "grey50")

brk <- function(h) {
  seq(min(x), max(x) + h, by = h)
}
hist(
  x,
  breaks = brk(h_cv),
  freq = FALSE,
  main = "",
  xlab = "x",
  col = "#cfe0f1",
  border = "white",
  ylim = c(0, 0.3)
)
hist(
  x,
  breaks = brk(h_scott),
  freq = FALSE,
  add = TRUE,
  col = NA,
  border = "#c0392b",
  lwd = 1.5
)
curve(f, add = TRUE, lwd = 2, col = "grey30")
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:

ise_hist <- function(h) {
  t <- seq(min(x) - 1, max(x) + 1, length.out = 20000)
  Nk <- tabulate(floor((x - min(x)) / h) + 1)
  idx <- floor((t - min(x)) / h) + 1
  fh <- ifelse(idx >= 1 & idx <= length(Nk), Nk[pmax(idx, 1)] / (n * h), 0)
  sum((fh - f(t))^2) * diff(t[1:2])
}
f <- function(t) {
  0.5 * dnorm(t, -2) + 0.5 * dnorm(t, 2)
}
signif(c(cross_validation = ise_hist(h_cv), scott = ise_hist(h_scott)), 3)
cross_validation            scott 
         0.00874          0.00872 

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.

References

Further reading: Rudemo (1982); Scott (1979).

Rudemo, Mats. 1982. “Empirical Choice of Histograms and Kernel Density Estimators.” Scandinavian Journal of Statistics 9 (2): 65–78.
Scott, David W. 1979. “On Optimal and Data-Based Histograms.” Biometrika 66 (3): 605–10. https://doi.org/10.1093/biomet/66.3.605.
Tsybakov, Alexandre B. 2009. Introduction to Nonparametric Estimation. Springer.
Wasserman, Larry. 2006. All of Nonparametric Statistics. Springer.

Series: Density estimation · Previous: Multivariate kernels