Tutorials › Statistical Learning (Graduate) › Common Errors in Density-Risk Calculations

Nonparametric Density Estimation · Tutorial 615 of 989

Common Errors in Density-Risk Calculations

Learn to check the assumptions and scaling behind common density-estimation risk calculations, and to spot when a valid interior formula fails at a boundary.

Intermediate 12 min read

What You'll Learn

  • Verify when a kernel density estimator integrates to one and when restricting its domain changes its mass.
  • Calculate how boundary bias can alter both pointwise and integrated risk.
  • Distinguish pointwise mean squared error from integrated mean squared error.
  • Derive the kernel variance scaling in bandwidth and sample size.
  • Translate squared-error rates into root-mean-squared error rates without changing the loss.

Why Density-Risk Calculations Go Wrong

Many mistakes in density estimation are not errors in algebra so much as errors in scope. A formula derived at an interior point may fail at a support boundary; a density estimator normalized on the real line may not remain normalized after restriction; and a rate for squared error is not the rate for its square root. The calculations are manageable once the estimand, domain, and loss are stated explicitly.

We use the kernel density estimator based on independent observations \(X_1,\ldots,X_n\) with common density \(f\):

$$ \hat f_h(x)=\frac{1}{nh}\sum_{i=1}^n K\!\left(\frac{x-X_i}{h}\right),\qquad h>0. $$

Here \(K\) is an integrable kernel with \(\int K(u)\,du=1\). It need not be nonnegative: higher-order kernels may take negative values, so their estimators need not be densities in the nonnegative sense. The normalization and risk calculations below must therefore be read as statements about integrals and losses, not as automatic claims that every estimate is a probability density.

Normalization: Specify the Domain

Definition: The integrated squared error is \(\operatorname{ISE}=\int_D(\hat f_h(x)-f(x))^2\,dx\), where \(D\) is the domain of interest. The mean integrated squared error is \(\operatorname{MISE}=E[\operatorname{ISE}]\). At a fixed point \(x\), the pointwise mean squared error is \(\operatorname{MSE}_x=E[(\hat f_h(x)-f(x))^2]\). These are different risks, even when their rates happen to agree.

If \(K\) is integrable with unit integral, each kernel contribution integrates to one over the entire real line. In particular, for every observed value \(X_i\), changing variables \(u=(x-X_i)/h\) gives

$$ \int_{\mathbb R}\frac{1}{h}K\!\left(\frac{x-X_i}{h}\right)dx =\int_{\mathbb R}K(u)\,du=1. $$

Averaging over observations therefore gives \(\int_{\mathbb R}\hat f_h(x)\,dx=1\), including when \(K\) is signed. This does not imply that the estimator integrates to one over a restricted domain \(D\), such as the support \([0,1]\). Nor does it guarantee nonnegativity when \(K\) can be negative.

Worked Example: Full-Line Mass Versus Mass on the Support

Let \(X_i\) be uniform on \([0,1]\), and use \(K(u)=\tfrac12\mathbf{1}\{|u|\leq1\}\) with \(h=0.2\). Each observation contributes a uniform kernel on \([X_i-h,X_i+h]\), so its integral over the real line is exactly one.

On \([0,1]\), a kernel centered at \(X_i=x\) loses mass to the left when \(x<h\). That lost mass is \((h-x)/(2h)\). Averaging over \(X_i\), which is uniform on \([0,1]\), the expected loss on the left is

$$ \int_0^h \frac{h-x}{2h}\,dx =\frac{1}{2h}\left[hx-\frac{x^2}{2}\right]_0^h =\frac{h}{4}=0.05. $$

The right boundary contributes another \(h/4=0.05\). Thus \(E[\int_0^1\hat f_h(x)\,dx]=1-h/2=0.9\), although every estimate has mass exactly one over the real line. The missing mass has not disappeared; it lies outside \([0,1]\). Renormalizing the restriction is a separate operation and changes the estimator.

Boundary Bias Is Not an Interior Approximation

For a symmetric kernel, the familiar second-order bias calculation uses cancellation of the first moment, \(\int uK(u)\,du=0\), and a Taylor expansion of \(f(x-hu)\). It applies at points where the full kernel window lies in a region on which the density is smooth. At a support boundary, part of that window lies outside the support, where the density is zero; the same cancellation argument cannot simply be reused.

Worked Example: A Boundary Bias That Does Not Shrink Pointwise

Again let \(X_i\) be uniform on \([0,1]\), so \(f(0)=1\), and use the symmetric kernel \(K(u)=\tfrac12\mathbf{1}\{|u|\leq1\}\). For \(0<h<1\), the expectation at the left endpoint is

$$ E[\hat f_h(0)] =\frac{1}{h}E\!\left[K\!\left(-\frac{X_i}{h}\right)\right] =\frac{1}{2h}P(X_i\leq h) =\frac{1}{2}. $$

Consequently, the bias at zero is \(1/2-1=-1/2\), which does not tend to zero as \(h\) shrinks. The interior claim “bias is of order \(h^2\)” is false at this boundary for the uncorrected estimator.

The effect is also visible in integrated squared bias. For \(0\leq x\leq h\), the kernel window overlaps the support over a length \(x+h\), so \(E[\hat f_h(x)]=(x+h)/(2h)\). Its bias is \(-(h-x)/(2h)\), and the squared bias integrated over this boundary interval is

$$ \int_0^h \left(\frac{h-x}{2h}\right)^2 dx =\frac{1}{4h^2}\cdot\frac{h^3}{3} =\frac{h}{12}. $$

The right endpoint contributes the same amount. The total integrated squared bias is \(h/6\), not an order-\(h^4\) interior bias term. Boundary corrections address this issue, but an interior expansion alone does not.

Pointwise and Integrated Risk Are Different Quantities

For any estimator with finite second moments, adding and subtracting its expectation gives the exact pointwise decomposition

$$ \operatorname{MSE}_x =\operatorname{Var}(\hat f_h(x)) +\big(E[\hat f_h(x)]-f(x)\big)^2. $$

This identity follows because \(E[\hat f_h(x)-E\hat f_h(x)]=0\), so the cross-product vanishes after squaring and taking expectation. If \((\omega,x)\mapsto(\hat f_h(x,\omega)-f(x))^2\) is measurable, Tonelli's theorem also gives

$$ \operatorname{MISE} =E\!\left[\int_D(\hat f_h(x)-f(x))^2dx\right] =\int_D \operatorname{MSE}_x\,dx. $$

The nonnegativity of the squared integrand is what permits this exchange even before finiteness is established; a finite MISE is then an additional property of the estimator and model. The identity does not make the two risks interchangeable: one is evaluated at a specified point, while the other accumulates risk across a domain.

Worked Example: A Narrow Error Spike

On \(D=[0,1]\), consider a deterministic estimation error \(e_n(x)=\mathbf{1}\{0\leq x\leq 1/n\}\). At \(x=0\), the squared error is \(e_n(0)^2=1\) for every \(n\). Its integrated squared error, however, is

$$ \int_0^1 e_n(x)^2dx=\int_0^{1/n}1\,dx=\frac{1}{n}. $$

Thus a small integrated error does not imply small error at every point. Conversely, pointwise control at each fixed \(x\) does not by itself bound the supremum over \(x\) without further uniform control. When reading a rate, check whether the claim concerns \(\operatorname{MSE}_x\), MISE, or a stronger norm.

Kernel Variance: Track Both Factors of the Bandwidth

A common scaling error is to lose one factor of \(h\) when computing the variance. Put \(Y_i=K((x-X_i)/h)\). Independence gives \(\operatorname{Var}(\hat f_h(x))=\operatorname{Var}(Y_i)/(nh^2)\). If \(f\) is a density and \(K\) is square-integrable, a change of variables yields the exact formula

$$ \operatorname{Var}(\hat f_h(x)) =\frac{1}{nh}\int K(u)^2f(x-hu)\,du -\frac{1}{n}\left(\int K(u)f(x-hu)\,du\right)^2. $$

The first term has scale \(1/(nh)\), not \(1/n\): the change of variables in \(E[Y_i^2]\) contributes a factor \(h\), while the estimator itself has the prefactor \(1/h\). If \(f\) is bounded and continuous at an interior point \(x\), and \(K\) is square-integrable with suitable integrable support or tail control, the leading variance term is \(f(x)R(K)/(nh)\), where \(R(K)=\int K(u)^2du\). The subtracted term is of order \(1/n\) under the corresponding regularity conditions and is smaller than \(1/(nh)\) as \(h\to0\).

Worked Example: Checking the Exact Variance

Let \(X_i\) be uniform on \([0,1]\), take \(x=0.5\), \(h=0.1\), and use \(K(u)=\tfrac12\mathbf{1}\{|u|\leq1\}\). For \(n=100\), the event \(|X_i-x|\leq h\) has probability \(0.2\). If \(N\) counts observations in this interval, then \(N\sim\operatorname{Binomial}(100,0.2)\) and \(\hat f_h(x)=0.05N\). Therefore

$$ E[\hat f_h(x)]=0.05(20)=1,\qquad \operatorname{Var}(\hat f_h(x))=0.05^2(100)(0.2)(0.8)=0.04. $$

The variance formula gives the same check. Here \(R(K)=1/2\), and the first term is \((1/2)/(100\cdot0.1)=0.05\). Also \(\int K(u)f(x-hu)\,du=1\), so the subtracted term is \(1/100=0.01\). Their difference is \(0.05-0.01=0.04\). Keeping only the leading term gives \(0.05\), a useful approximation here, not the exact variance.

Squared-Error Rates and Error Rates

Under an interior, twice-smooth setting, a symmetric second-order kernel with finite second moment has bias of order \(h^2\); the leading bias is \(\tfrac12 h^2 f''(x)\int u^2K(u)\,du\) when the relevant Taylor expansion is valid. Together with variance of order \(1/(nh)\), this gives pointwise MSE of order \(h^4+1/(nh)\). For whole-line integrated risk, the same order is obtained under suitable smoothness and integrability conditions, for example when the density and its second derivative are square-integrable and the boundary does not introduce a larger bias term. These are not boundary-free guarantees for every support or estimator.

When both leading constants are positive, balancing the two terms gives \(h\) of order \(n^{-1/5}\), and the optimized squared-error risk is of order \(n^{-4/5}\). The corresponding root-MSE is of order \(n^{-2/5}\), because it is the square root of the MSE. In general, taking a square root changes the exponent of a squared-error rate; it does not leave the rate unchanged.

Worked Example: Converting the Rate Correctly

Suppose the interior MSE is bounded in order by \(h^4+1/(nh)\), with nonzero leading bias and variance constants. Balancing the terms means \(h^4\) and \(1/(nh)\) have the same order, so \(h^5\) is of order \(1/n\), or \(h\) is of order \(n^{-1/5}\). Substituting gives \(h^4\) and \(1/(nh)\) both of order \(n^{-4/5}\).

For \(n=10{,}000\), the bandwidth scale is \(n^{-1/5}\approx0.1585\), and the squared-error scale is \(n^{-4/5}\approx0.000631\). The root-MSE scale is its square root, \(n^{-2/5}\approx0.0251\). Calling \(n^{-4/5}\) an “error rate” without specifying squared error is ambiguous and often misleading.

Common Mistakes and a Reliable Check

Before accepting a density-risk calculation, identify the domain, the loss, the location relative to the boundary, and whether the expression is exact or asymptotic. The following errors are especially consequential:

  • Assuming restriction preserves normalization. Unit kernel mass on the real line does not imply unit mass on the support. Integrate over the stated domain.
  • Using an interior bias expansion at a boundary. The kernel window is truncated there; check its expectation directly or use a justified boundary correction.
  • Equating pointwise and integrated risk. One evaluates at a point and the other integrates over a domain. Neither generally controls a supremum without additional conditions.
  • Dropping a bandwidth factor in the variance. The estimator has a factor \(1/h\), and the change of variables in the second moment supplies another \(h\); the leading variance scale is \(1/(nh)\).
  • Reporting a squared-error exponent as an error exponent. MSE of order \(n^{-4/5}\) corresponds to root-MSE of order \(n^{-2/5}\).
  • Treating a leading approximation as exact. The variance expression includes a subtracted term; it may be lower order, but it is still part of the exact formula.
Key takeaway: A density-risk rate is meaningful only after its loss, domain, and regularity regime are clear. Check normalization over the actual domain, handle boundaries separately, and keep variance and squared-error scaling distinct from their square roots.

Check Your Understanding

For each question, state the assumptions or domain that matter to your answer.

  1. Why can a kernel estimator integrate to one on the real line but to less than one on the support of the density?
  2. For the uniform density on \([0,1]\) and the box kernel in this tutorial, what is \(E[\hat f_h(0)]\) when \(0<h<1\), and what is the resulting bias?
  3. State the exact relationship between MISE and pointwise MSE, including the condition that permits exchanging expectation and integration.
  4. Explain where the factor \(1/(nh)\) in the leading kernel variance comes from.
  5. If MSE is of order \(n^{-4/5}\), what is the corresponding root-MSE order? Why?
Clinical Biostats