Tutorials › Exam PA - Predictive Analytics › Density Curves and Bandwidth Choices

Exam PA · Visualization and Anomaly Detection · Tutorial 135 of 966

Density Curves and Bandwidth Choices

Create and interpret kernel density plots in R, calculate estimates at selected values, and explain how bandwidth changes apparent peaks, tails, and clusters.

Beginner 10 min read

What You'll Learn

  • Define a kernel density estimate and interpret its vertical scale and total area.
  • Explain how bandwidth controls smoothing and why its units match the variable’s units.
  • Calculate Gaussian-kernel density estimates at selected values.
  • Create comparable density curves in R by specifying bandwidths.
  • Recognize when smoothing changes the visual impression of peaks, tails, or clusters.
  • Identify common interpretation errors, including treating density as probability.

Why Bandwidth Matters

A kernel density curve gives a smooth visual summary of the distribution of a quantitative variable. It can make concentrations, gaps, tails, and possible clusters easier to see than a jagged display. But the curve is not determined by the observations alone: its smoothness depends on a setting called the bandwidth. A small bandwidth can make minor fluctuations look like distinct peaks, while a large bandwidth can blur meaningful differences.

For exploratory analysis, the aim is not to choose the bandwidth that produces the most attractive curve. It is to understand how reasonable bandwidth choices affect the story the plot appears to tell, then avoid treating a feature that exists only under one choice as established evidence.

Definition: Given observations \(x_1,\ldots,x_n\), a kernel density estimate at a value \(x\) is $$ \hat f_h(x)=\frac{1}{nh}\sum_{i=1}^{n}K\left(\frac{x-x_i}{h}\right), $$ where \(K\) is a kernel function and \(h>0\) is the bandwidth. A common choice is the standard normal density, \(K(u)=\phi(u)=\frac{1}{\sqrt{2\pi}}\exp(-u^2/2)\). The bandwidth determines how widely each observation’s kernel is spread.

The estimate is built by centering a scaled kernel at each observation and averaging the resulting contributions. With a Gaussian kernel, every observation contributes to the estimate at every \(x\), although observations farther away contribute less. The bandwidth \(h\) is measured in the same units as the data. For example, if \(x_i\) is measured in dollars, \(h\) is measured in dollars as well.

What the Curve Shows

A density curve’s height is a density, not the probability of observing exactly that value. The curve is nonnegative and has total area 1. Probabilities correspond to areas over intervals. A tall, narrow peak can therefore have the same area as a low, broad one. When interpreting a plot, attend to both the horizontal range and the amount of area represented, not height alone.

The kernel and bandwidth play different roles. The kernel specifies the shape placed around each observation; the Gaussian kernel is a standard smooth choice. The bandwidth sets the spread of those kernels and usually has the larger visual effect. A smaller \(h\) produces a more locally responsive curve, whereas a larger \(h\) averages across wider neighborhoods and smooths local variation.

A kernel density curve is a smoothed summary of the observed values, not a claim that the underlying variable follows a particular distribution. For data that are discrete or heavily rounded, the curve still provides a visual summary, but it should not be mistaken for a literal continuous data-generating process. For variables restricted to a range, such as nonnegative amounts, smoothing can also extend the curve beyond the feasible range; that boundary behavior deserves attention.

How Bandwidth Changes the Estimate

The following calculations use the Gaussian kernel. The standardized distance from an observation to the point being estimated is \((x-x_i)/h\). Each kernel contribution is divided by \(h\), and the contributions are then averaged over the \(n\) observations. This \(1/h\) factor is essential: increasing the bandwidth spreads each kernel over a wider range and lowers its height.

Worked Example: Calculating the Estimate at Two Bandwidths

A hypothetical sample of claim-processing times, in days, is \(0,0,2,4\). Calculate the Gaussian kernel density estimate at \(x=0\) for \(h=1\) and \(h=2\). State what changes and identify the calculation trap.

Method. Substitute each observation into the kernel estimate. For \(h=1\), the standardized distances are \(0,0,-2,-4\). The Gaussian kernel values are \(\phi(0)=0.39894\), \(\phi(-2)=0.05399\), and \(\phi(-4)=0.00013\). Thus $$ \hat f_1(0)=\frac{0.39894+0.39894+0.05399+0.00013}{4(1)} =\frac{0.85200}{4}=0.21300. $$ To four decimal places, the estimate is \(0.2130\) per day.

For \(h=2\), the standardized distances are \(0,0,-1,-2\). Using \(\phi(-1)=0.24197\), the kernel contributions sum to $$ 0.39894+0.39894+0.24197+0.05399=1.09384. $$ The denominator is \(nh=4(2)=8\), so $$ \hat f_2(0)=\frac{1.09384}{8}=0.13673. $$ To four decimal places, this is \(0.1367\) per day. The broader bandwidth lowers the estimate at this location by spreading kernel mass over a wider range; it does not change the observed data.

Common mistake avoided. Dividing the \(h=2\) kernel sum by \(n=4\) alone gives \(1.09384/4=0.27346\), which is wrong because it omits the bandwidth factor. The formula requires \(nh=8\), giving \(0.13673\). The calculation checks: the larger bandwidth has the required \(1/h\) scaling, and all four observations contribute.

This example compares density heights at one point; it does not establish that one bandwidth is universally better. A plot shows the estimate across many values of \(x\), and bandwidth comparisons should focus on whether important features persist across plausible choices.

Why the Estimate Has Total Area One

The normalization in the formula is chosen so the estimated curve is a density. Suppose the kernel is nonnegative and integrates to 1, as the standard normal density does. Each observation contributes a shifted and rescaled copy of that kernel. For one observation \(x_i\), its contribution is \(K((x-x_i)/h)/(nh)\).

To find the area under this one contribution, substitute \(u=(x-x_i)/h\), so \(dx=h\,du\). Then $$ \int_{-\infty}^{\infty}\frac{1}{nh}K\left(\frac{x-x_i}{h}\right)\,dx =\frac{1}{n}\int_{-\infty}^{\infty}K(u)\,du =\frac{1}{n}. $$ There are \(n\) such contributions. Adding their areas gives \(n(1/n)=1\). This also explains why the bandwidth changes the curve’s height and spread without changing its total area.

Reading Cluster Structure Carefully

A density plot can suggest multiple concentrations, but a visible peak is not automatically a distinct population or a meaningful subgroup. Small bandwidths can accentuate small gaps between nearby observations; large bandwidths can fill those gaps. Compare curves at several bandwidths and describe what is stable, what changes, and what the data alone cannot establish.

Worked Example: Checking a Possible Gap Between Clusters

A hypothetical sample of repair durations, in hours, is \(1.0,1.2,1.4,4.6,4.8,5.0\). Estimate the density at the midpoint \(x=3\) for bandwidths \(h=1\) and \(h=2\). Use the results to assess how smoothing affects the apparent gap.

For \(h=1\), the standardized distances are \(2,1.8,1.6,-1.6,-1.8,-2\). The corresponding Gaussian values are approximately \(0.05399,0.07895,0.11092,0.11092,0.07895,0.05399\). Their sum is \(0.48772\), so $$ \hat f_1(3)=\frac{0.48772}{6(1)}=0.08129. $$

For \(h=2\), the standardized distances are \(1,0.9,0.8,-0.8,-0.9,-1\). The kernel values are approximately \(0.24197,0.26609,0.28969,0.28969,0.26609,0.24197\). Their sum is \(1.59550\), giving $$ \hat f_2(3)=\frac{1.59550}{6(2)}=0.13296. $$

The estimate at the midpoint is higher with the wider bandwidth: smoothing has filled in more of the low-density region between the two groups of observations. This one-point calculation does not prove that the full curve has two peaks at either bandwidth; the curve must be examined. The appropriate interpretation is that the visual contrast across the gap is sensitive to smoothing. A common mistake is to report the two apparent concentrations as confirmed subpopulations without checking how the picture changes with bandwidth or considering the context.

Creating and Comparing Curves in R

In R, density(x, bw = h, kernel = "gaussian") computes a kernel density estimate for a numeric vector x. The argument bw specifies the bandwidth in the variable’s units. If it is omitted, R chooses a bandwidth automatically; that default is a starting point, not a guarantee that the resulting plot is the only reasonable view. The returned object contains grid locations and estimated heights, which can be drawn with plot().

To compare two choices on the same scale, calculate both estimates and add the second curve to the first plot. For example, for a vector named duration, use d1 <- density(duration, bw = 1, kernel = "gaussian") and d2 <- density(duration, bw = 2, kernel = "gaussian"). Then use plot(d1) and lines(d2, lty = 2), with a legend identifying the bandwidths. The values 1 and 2 are appropriate only if those bandwidths make sense in the units and range of duration.

R also provides adjust, which multiplies the selected bandwidth. If the underlying bandwidth is \(b\), setting adjust = 1.5 uses \(1.5b\); setting adjust = 0.5 uses \(0.5b\). This is convenient for a sensitivity check around an automatically selected value. To make a fair visual comparison, use the same data, kernel, horizontal scale, and vertical scale for the curves. Changing the display grid changes how finely the curve is drawn; it is not a substitute for choosing the bandwidth.

Worked Example: Interpreting R Curves at Two Settings

A hypothetical sample of three inspection times, in minutes, is \(10,10,12\). An analyst creates Gaussian density curves in R with bw = 1 and bw = 2. Calculate each curve’s estimate at 10 and 11 minutes, then state what the comparison can and cannot show.

At \(x=10\) with \(h=1\), the standardized distances are \(0,0,-2\). The kernel values sum to \(0.39894+0.39894+0.05399=0.85187\), so $$ \hat f_1(10)=\frac{0.85187}{3(1)}=0.28396. $$ At \(x=11\), the standardized distances are \(1,1,-1\), and all three kernel values equal approximately \(0.24197\). Thus \(\hat f_1(11)=0.72591/3=0.24197\).

For \(h=2\) at \(x=10\), the standardized distances are \(0,0,-1\). The sum is \(0.39894+0.39894+0.24197=1.03985\), giving \(\hat f_2(10)=1.03985/6=0.17331\). At \(x=11\), the distances are \(0.5,0.5,-0.5\). Since \(\phi(0.5)\) and \(\phi(-0.5)\) are both approximately \(0.35207\), the estimate is \(1.05620/6=0.17603\).

The broader bandwidth makes the estimated heights at 10 and 11 more similar in this comparison. In R, the corresponding calls are density(c(10, 10, 12), bw = 1, kernel = "gaussian") and density(c(10, 10, 12), bw = 2, kernel = "gaussian"). The comparison helps show the effect of smoothing, but three observations cannot support a confident claim about the population’s shape. The calculation checks by using the same three observations in both estimates and dividing by \(nh\) in each case.

Common Interpretation Traps

  • Calling height a probability. A density value is not the probability of an exact measurement. Probability is represented by area over an interval.
  • Forgetting the bandwidth in the denominator. Each estimate uses \(nh\), not just \(n\). This factor is essential to the height and area of the curve.
  • Comparing curves on different scales. If axes change between plots, a visual height comparison can be misleading. Overlay the curves or use consistent axes.
  • Treating every peak as a real subgroup. A peak that appears only at a very small bandwidth may reflect local variation rather than a stable feature.
  • Ignoring units and boundaries. A bandwidth of 2 means two units of the variable, not two percent. For a variable with a lower bound, a smooth curve can extend into impossible values.
Key takeaway: A kernel density curve is a normalized, smoothed summary of observed numeric values. State the bandwidth and units, compare plausible bandwidth choices on consistent axes, and treat peaks, tails, and gaps as exploratory features whose appearance may depend on smoothing.

Check Your Understanding

Work through each problem before checking its answer. Use the Gaussian kernel and round density estimates to four decimal places.

  1. A hypothetical sample is \(2,2,4\). Calculate the density estimate at \(x=2\) with \(h=1\), using \(\phi(0)=0.39894\) and \(\phi(-2)=0.05399\). (Answer: \(0.2840\) per data unit.)
  2. For the same observations, calculate the estimate at \(x=2\) with \(h=2\), using \(\phi(-1)=0.24197\). Include the \(nh\) denominator. (Answer: \(0.1733\) per data unit.)
  3. A sample contains \(0\) and \(2\). Find the estimated density at \(x=1\) when \(h=1\), using \(\phi(1)=\phi(-1)=0.24197\). (Answer: \(0.2420\) per data unit.)
  4. An R analyst has an automatically selected bandwidth \(b=0.8\) and sets adjust = 1.5. What bandwidth is used, and does this change the observations? (Answer: \(1.2\) data units; no, it changes the smoothing, not the observations.)
  5. A density curve for a hypothetical payment amount is about \(0.08\) per thousand dollars throughout an interval 2.5 thousand dollars wide. If the curve is approximately constant over that interval, estimate the probability in the interval. Explain why this is not the probability of one exact payment amount. (Answer: approximately \(0.08(2.5)=0.20\), or 20%; density times interval width approximates area, whereas a single exact value has no positive interval width.)
Clinical Biostats