Histograms II: bias, variance and mean squared error
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 and the text rewritten.
To study the statistical properties of the histogram \(\hat f_h(x)\) we use the mean squared error (MSE), or quadratic risk:
\[ \operatorname{MSE}\big(\hat f_h(x)\big) = \mathbb{E}\big(\hat f_h(x) - f(x)\big)^2 . \]
It splits into variance and squared bias:
\[ \operatorname{MSE}\big(\hat f_h(x)\big) = \operatorname{Var}\big(\hat f_h(x)\big) + \operatorname{Bias}\big(\hat f_h(x)\big)^2, \tag{1}\]
where \(\operatorname{Var}(\hat f_h(x)) = \mathbb{E}\big(\hat f_h(x) - \mathbb{E}\hat f_h(x)\big)^2\) and \(\operatorname{Bias}(\hat f_h(x)) = \mathbb{E}\hat f_h(x) - f(x)\).
The two terms move in opposite directions with \(h\): wide bins give a stable but coarse estimate (small variance, large bias), and narrow bins give a detailed but noisy one (small bias, large variance). The question is how large \(h\) should be to balance them.
Throughout, take the origin \(x_0 = 0\), so \(B_j = [(j - 1)h, jh)\), and assume \(f\) has a continuous derivative.
Bias
Fix \(x \in B_j\). Since the \(X_i\) are i.i.d.,
\[ \mathbb{E}\hat f_h(x) = \frac{1}{nh} \sum_{i=1}^{n} \mathbb{E}\,\mathbf{1}(X_i \in B_j) = \frac{1}{h}\, \mathbb{P}(X_1 \in B_j) = \frac{p_j}{h}, \qquad p_j = \int_{(j-1)h}^{jh} f(u)\, du . \]
Each \(\mathbf{1}(X_i \in B_j)\) is a Bernoulli variable with parameter \(p_j\). In general \(p_j/h \neq f(x)\), so the histogram is biased:
\[ \operatorname{Bias}\big(\hat f_h(x)\big) = \frac{1}{h}\int_{(j-1)h}^{jh} f(u)\, du - f(x) = \frac{1}{h}\int_{(j-1)h}^{jh} \big(f(u) - f(x)\big)\, du . \]
A first-order Taylor expansion \(f(u) - f(x) = f'(x)(u - x) + o(h)\), uniformly for \(u \in B_j\), together with \(\frac1h\int_{(j-1)h}^{jh} (u - x)\,du = (j - \tfrac12)h - x\), gives
\[ \operatorname{Bias}\big(\hat f_h(x)\big) = f'(x)\Big(\big(j - \tfrac12\big)h - x\Big) + o(h). \tag{2}\]
The factor \((j - \tfrac12)h - x\) is the distance from \(x\) to the midpoint of its bin, at most \(h/2\) in absolute value. The bias is therefore of order \(h\): it grows with \(h\) (wherever \(f'(x) \neq 0\)) and vanishes at the bin midpoints to first order.
Variance
By independence,
\[ \operatorname{Var}\big(\hat f_h(x)\big) = \frac{1}{n^2 h^2} \sum_{i=1}^{n} \operatorname{Var}\,\mathbf{1}(X_i \in B_j) = \frac{p_j(1 - p_j)}{n h^2}. \]
As \(h \to 0\), \(p_j = h f(x) + O(h^2)\), so
\[ \operatorname{Var}\big(\hat f_h(x)\big) = \frac{f(x)}{nh} + o\!\left( \frac{1}{nh} \right). \tag{3}\]
The variance decreases as \(h\) grows, the opposite of the bias.
Mean squared error
Plugging Equation 2 and Equation 3 into Equation 1,
\[ \operatorname{MSE}\big(\hat f_h(x)\big) = \frac{f(x)}{nh} + f'(x)^2\Big(\big(j - \tfrac12\big)h - x\Big)^2 + o(h^2) + o\!\left( \frac{1}{nh} \right). \tag{4}\]
If \(h \to 0\) and \(nh \to \infty\) as \(n \to \infty\), both leading terms go to zero, so \(\operatorname{MSE}(\hat f_h(x)) \to 0\). The bins must shrink, but slowly enough that each one still collects many observations.
Question for you: is \(\hat f_h(x)\) a consistent estimator of \(f(x)\)?
The next post answers this and finds the binwidth that minimizes a global version of the MSE.
References
Further reading: Härdle et al. (2004, chap. 2).
Series: Density estimation · Previous: Histograms I · Next: Histograms III