Kernel Density Estimation: Beyond the Histogram
A KDE plot is a smoothed histogram, revealing a variable's distribution by summing 'bumps' over each data point instead of using rigid bins. It's great for data exploration, but the 'smoothness' (bandwidth) choice can drastically alter the plot's story.
WHY IT EXISTS: Histograms are a default for viewing a variable's distribution, but their shape is highly sensitive to the number and placement of bins. A slight change in bin width can create a completely different visual story. Kernel Density Estimation (KDE) was created to estimate a distribution's shape from a data sample without the arbitrary constraint of bins.
THE MENTAL MODEL: A KDE plot is a 'sum of bumps.' Imagine your data points on a number line. At the location of each point, you place a small, symmetrical bump (the 'kernel,' typically a bell curve). Then, you sum all these bumps together. Where data points are clustered, the overlapping bumps create a high peak; where data is sparse, the curve is low. The result is a smooth curve representing the data's density.
HOW IT WORKS: The process involves two choices: the kernel and the bandwidth. The kernel is the shape of the bump placed on each data point (a Gaussian curve is standard). The bandwidth is more critical: it controls the width of each bump. A small bandwidth creates a spiky, noisy curve that overfits to the sample data. A large bandwidth creates an overly smooth curve that can hide important features. The final KDE plot is the normalized sum of all these individual kernel functions.
WHEN TO USE IT: Use KDE to visualize the distribution of a continuous variable when you want a smoother representation than a histogram. It is excellent for comparing the distributions of a variable across different categories (e.g., price distribution for products A and B). It's also used within algorithms like Naive Bayes classifiers to estimate class-conditional densities, which can improve model accuracy.
WHEN NOT TO USE IT: Be cautious with data that has hard boundaries (e.g., prices, which cannot be negative). A standard KDE might show probability density outside the valid range. Also, remember that the smoothness can be misleading; the curve is an estimate, not a ground truth, and is highly dependent on the bandwidth parameter.
ONE CANONICAL EXAMPLE: Visualizing user ages for a service. A histogram might show a large block of users in the 20-30 age range. A KDE plot could reveal a more nuanced picture, showing distinct peaks around age 22 (college students) and 28 (young professionals), a detail that a coarse histogram would completely miss.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.