set.seed(2011)
n <- 1000
x <- rnorm(n)
x0 <- 0.1 # evaluation point
norm_fp2 <- 1 / (4 * sqrt(pi)) # ||f'||_2^2 for N(0, 1)
hvec <- seq(0.05, 0.8, by = 0.001)
# Bins are [(j - 1)h, jh); probability of the bin that contains x0
p_bin <- function(h) {
j <- floor(x0 / h) + 1
pnorm(j * h) - pnorm((j - 1) * h)
}
p <- sapply(hvec, p_bin)
bias <- p / hvec - dnorm(x0)
variance <- p * (1 - p) / (n * hvec^2)
mse <- bias^2 + variance
amise_var <- 1 / (n * hvec)
amise_bias <- hvec^2 * norm_fp2 / 12
amise <- amise_var + amise_bias
h_mse <- hvec[which.min(mse)]
h_amise <- hvec[which.min(amise)]
h_opt <- (6 / (n * norm_fp2))^(1 / 3)Histograms IV: a simulation
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, the text rewritten, and the R code redone (the original was my very first R script).
This post puts the previous ones into practice. First, last week’s question.
The optimal binwidth for normal data
If \(f\) is the standard normal density \(\varphi(x) = \frac{1}{\sqrt{2\pi}} e^{-x^2/2}\), then \(f'(x) = -x\,\varphi(x)\) and
\[ \|f'\|_2^2 = \int x^2 \varphi(x)^2\, dx . \]
Note that \(\varphi(x)^2 = \frac{1}{2\pi} e^{-x^2} = \frac{1}{2\sqrt{\pi}}\, g(x)\), where \(g\) is the density of a \(N(0, \tfrac12)\) variable. So the integral is \(\frac{1}{2\sqrt\pi}\) times the variance of that variable:
\[ \|f'\|_2^2 = \frac{1}{2\sqrt{\pi}} \cdot \frac12 = \frac{1}{4\sqrt{\pi}} . \]
Plugging this into the formula from Part III,
\[ h_{\text{opt}} = \left( \frac{6}{n \|f'\|_2^2} \right)^{1/3} = \left( \frac{24\sqrt{\pi}}{n} \right)^{1/3} \approx 3.49\, n^{-1/3}. \]
For data with standard deviation \(\sigma\), the same argument gives \(h \approx 3.49\,\sigma\, n^{-1/3}\): this is Scott’s rule (Scott 1979). It is only exact for normal data, but it is a reasonable rule of thumb when the data look roughly normal. In R it is available as hist(x, breaks = "Scott").
Simulation
Take \(n = 1000\) draws from a standard normal. Because we know the true \(f\), we can compute the exact bias and variance of \(\hat f_h(x_0)\) at a point, using the formulas from Part II with \(p_j = \Phi(jh) - \Phi((j - 1)h)\), and the AMISE from Part III, for a grid of binwidths.
Code
par(mar = c(4, 4, 1, 1))
plot(hvec, mse, type = "l", lwd = 2, col = "#1f4e79",
ylim = range(0, mse), xlab = "Binwidth h", ylab = "")
lines(hvec, bias^2, lwd = 2, lty = 2, col = "#c0392b")
lines(hvec, variance, lwd = 2, lty = 3)
abline(v = h_mse, col = "grey50")
legend("topright", c("MSE", expression(Bias^2), "Variance"),
col = c("#1f4e79", "#c0392b", "black"), lwd = 2, lty = 1:3, bty = "n")
Code
par(mar = c(4, 4, 1, 1))
plot(hvec, amise, type = "l", lwd = 2, col = "#1f4e79",
ylim = range(0, amise), xlab = "Binwidth h", ylab = "")
lines(hvec, amise_bias, lwd = 2, lty = 2, col = "#c0392b")
lines(hvec, amise_var, lwd = 2, lty = 3)
abline(v = h_amise, col = "grey50")
legend("topright", c("AMISE", "Integrated squared bias", "Integrated variance"),
col = c("#1f4e79", "#c0392b", "black"), lwd = 2, lty = 1:3, bty = "n")
Both pictures show the trade-off from Part II: for small \(h\) the variance dominates, and for large \(h\) the bias does.
- The binwidth minimizing the MSE at \(x_0 = 0.1\) is \(h = 0.493\). It is large because \(x_0\) is close to the mode, where \(f'(x_0) \approx 0\) and the bias is small. A pointwise criterion gives a different answer at every \(x\).
- The binwidth minimizing the AMISE on the grid is \(h = 0.349\), which matches the closed-form \(h_{\text{opt}} = (24\sqrt\pi / n)^{1/3} = 0.3491\).
Finally, the histogram with \(h_{\text{opt}}\) against the true density:
Code
breaks <- seq(floor(min(x) / h_opt), ceiling(max(x) / h_opt)) * h_opt
par(mar = c(4, 4, 1, 1))
hist(x, breaks = breaks, freq = FALSE, main = "", xlab = "x",
col = "grey85", border = "white", ylim = c(0, 0.45))
curve(dnorm(x), add = TRUE, lwd = 2, col = "#1f4e79")
The histogram is still a step function, and it depends on where the bins start. The next part of the series moves to kernel density estimators, which remove that dependence and give smooth estimates.
References
Further reading: Härdle et al. (2004, chap. 2).
Series: Density estimation · Previous: Histograms III · Next: Kernel density estimation