Skip to main content

Choosing the bandwidth: the normal reference rule and cross-validation

Archive 2011–2014
Density estimation
Kernel methods
R
Two practical ways to pick h for a kernel density estimator: Silverman’s rule of thumb and least-squares cross-validation, with an R comparison.
Author

Maikol Solís

Published

March 18, 2012

Modified

November 30, 2026

This post belongs to a series of study notes I first published in 2012, during my PhD. I revised it in 2026: the mathematics was checked and corrected, the text rewritten, and an R example added.

The theory in the previous posts tells us how the bandwidth \(h\) should shrink with \(n\), but the optimal constant depends on the unknown density. This post covers two popular ways to choose \(h\) from the data:

Throughout, \(\hat f_h(x) = \frac1n\sum_i K_h(x - X_i)\) with \(K_h(u) = h^{-1}K(u/h)\), and \(K\) is a symmetric kernel.

The normal reference rule

The asymptotically optimal bandwidth for the MISE of a second-order kernel is

\[ h_{\text{AMISE}} = \left( \frac{\int K^2(u)\,du}{\big(\int u^2 K(u)\,du\big)^2\, \int f''(x)^2\,dx\; n} \right)^{1/5}. \]

It depends on \(f\) through \(\int (f'')^2\). The normal reference rule pretends that \(f\) is \(N(\mu, \sigma^2)\), computes \(\int (f'')^2\) for that density, and plugs it in. For the Gaussian kernel this gives

\[ h_{\text{NR}} = \left( \frac43 \right)^{1/5} \sigma\, n^{-1/5} \approx 1.06\, \sigma\, n^{-1/5}. \]

The scale \(\sigma\) has to be estimated. A robust choice is \(\hat\sigma = \min\{s,\ Q/1.34\}\), where \(s\) is the sample standard deviation and \(Q\) the interquartile range (75th minus 25th percentile). For normal data \(Q \approx 1.34\,\sigma\), so \(Q/1.34\) estimates \(\sigma\) consistently. Taking the minimum protects against heavy tails and outliers, which inflate \(s\). Summarizing,

\[ h_{\text{NR}} = 1.06\, \min\left\{ \sqrt{\frac{1}{n - 1}\sum_{i=1}^n (X_i - \bar X)^2},\ \frac{Q}{1.34} \right\} n^{-1/5}. \]

The rule works well when \(f\) is close to normal. For multimodal or strongly skewed densities \(\int (f'')^2\) is much larger than the normal value, and the rule oversmooths. Silverman’s popular variant replaces 1.06 by 0.9 to reduce this effect (it is R’s default bw.nrd0).

Least-squares cross-validation

Instead of assuming a shape for \(f\), cross-validation estimates the risk directly. 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 , \]

and the MISE is its expectation. The last term does not depend on \(h\), so minimizing the ISE in \(h\) is the same as minimizing

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

The first term depends only on the data. The second does not: \(\int \hat f_h f\) is the expectation of \(\hat f_h(X)\) for a new observation \(X \sim f\), independent of the sample. The naive estimate

\[ \frac1n\sum_{i=1}^n \hat f_h(X_i) = \frac{1}{n^2 h}\sum_{i=1}^n\sum_{j=1}^n K\!\left( \frac{X_i - X_j}{h} \right) \]

uses each \(X_i\) twice: once to build \(\hat f_h\) and once to evaluate it. The diagonal terms \(i = j\) contribute \(K(0)/(nh)\), which explodes as \(h \to 0\). The remedy is to leave the \(i\)-th observation out:

\[ \hat f_{h,-i}(x) = \frac{1}{n - 1}\sum_{j \ne i} K_h(x - X_j), \qquad \widehat{\int \hat f_h f} = \frac1n\sum_{i=1}^n \hat f_{h,-i}(X_i). \]

The first term has a closed form. Substituting \(u = (x - X_i)/h\) and using the symmetry of \(K\),

\[ \begin{aligned} \int \hat f_h^2(x)\,dx & = \frac{1}{n^2h^2}\sum_{i=1}^n\sum_{j=1}^n \int K\!\left( \frac{x - X_i}{h} \right) K\!\left( \frac{x - X_j}{h} \right) dx \\ & = \frac{1}{n^2 h}\sum_{i=1}^n\sum_{j=1}^n \int K(u)\, K\!\left( u + \frac{X_i - X_j}{h} \right) du = \frac{1}{n^2 h}\sum_{i=1}^n\sum_{j=1}^n (K * K)\!\left( \frac{X_i - X_j}{h} \right), \end{aligned} \]

where \(K * K\) is the convolution of \(K\) with itself. The cross-validation criterion is therefore

\[ \operatorname{CV}(h) = \frac{1}{n^2 h}\sum_{i=1}^n\sum_{j=1}^n (K * K)\!\left( \frac{X_i - X_j}{h} \right) - \frac{2}{n(n - 1)}\sum_{i=1}^n\sum_{j \ne i} K_h(X_i - X_j), \]

and \(\hat h_{\text{CV}} = \arg\min_h \operatorname{CV}(h)\). One can show that \(\mathbb{E}\,\operatorname{CV}(h) = \operatorname{MISE}(h) - \int f^2\): up to a constant, \(\operatorname{CV}(h)\) is an unbiased estimator of the MISE.

For the Gaussian kernel, \(K * K\) is the \(N(0, 2)\) density, so everything is explicit. For other kernels, \(K * K\) can be computed once, or \(\int \hat f_h^2\) can be integrated numerically.

Example

We compare both rules on a bimodal mixture, \(\tfrac12 N(-2, 1) + \tfrac12 N(2, 1)\), with \(n = 400\). bw.nrd implements the normal reference rule above and bw.ucv implements least-squares cross-validation.

set.seed(2012)
n <- 400
x <- c(rnorm(n / 2, -2), rnorm(n / 2, 2))
f <- function(t) {
  0.5 * dnorm(t, -2) + 0.5 * dnorm(t, 2)
}

h_nr <- bw.nrd(x) # 1.06 * min(s, IQR/1.34) * n^(-1/5)
h_cv <- bw.ucv(x, lower = 0.05, upper = 2)

ise <- function(h) {
  d <- density(x, bw = h, n = 2048, from = -7, to = 7)
  sum((d$y - f(d$x))^2) * diff(d$x[1:2])
}
data.frame(
  rule = c("Normal reference", "Cross-validation"),
  h = round(c(h_nr, h_cv), 3),
  ISE = signif(c(ise(h_nr), ise(h_cv)), 3)
)
              rule     h     ISE
1 Normal reference 0.738 0.00578
2 Cross-validation 0.411 0.00213
Code
grid <- seq(-6, 6, length.out = 500)
par(mar = c(4, 4, 1, 1))
plot(
  grid,
  f(grid),
  type = "l",
  lwd = 2,
  col = "grey40",
  xlab = "x",
  ylab = "Density",
  ylim = c(0, 0.25)
)
lines(density(x, bw = h_nr), lwd = 2, lty = 2, col = "#c0392b")
lines(density(x, bw = h_cv), lwd = 2, col = "#1f4e79")
rug(x, col = "grey70")
legend(
  "topright",
  c("True density", "Normal reference", "Cross-validation"),
  col = c("grey40", "#c0392b", "#1f4e79"),
  lty = c(1, 2, 1),
  lwd = 2,
  bty = "n"
)
Figure 1: Kernel density estimates of a bimodal mixture with two bandwidth rules, against the true density.

The normal reference rule treats the two modes as a single wide normal, so its bandwidth is too large: it flattens both peaks and partly fills in the valley, and its ISE is 2.7 times that of cross-validation. Cross-validation adapts to the shape and gives a smaller bandwidth. Its price is variability: \(\hat h_{\text{CV}}\) changes noticeably from sample to sample, which is a well-known weakness of least-squares cross-validation.

References

Further reading: Härdle et al. (2004, chap. 3); Tsybakov (2009, sec. 1.4).

Härdle, Wolfgang, Marlene Müller, Stefan Sperlich, and Axel Werwatz. 2004. Nonparametric and Semiparametric Models. Springer.
Silverman, B. W. 1986. Density Estimation for Statistics and Data Analysis. Chapman & Hall.
Tsybakov, Alexandre B. 2009. Introduction to Nonparametric Estimation. Springer.

Series: Density estimation · Previous: Sobolev classes · Next: Multivariate kernels