Kernel Density Estimation (KDE)© Karobben

Kernel Density Estimation (KDE)

Kernel Density Estimation (KDE)

Kernel Density Estimation (KDE) is a non-parametric method to estimate the probability density function (PDF) of a random variable based on a finite set of data points. Unlike parametric methods, which assume that the underlying data follows a specific distribution (like normal, exponential, etc.), KDE makes no such assumptions and can model more complex data distributions.

How KDE Works:

  1. Kernel Function: The kernel function is a smooth, continuous, symmetric function that is centered on each data point. The most commonly used kernel is the Gaussian (normal) kernel, but other kernels like Epanechnikov, triangular, and uniform can also be used.

  2. Bandwidth (Smoothing Parameter): The bandwidth is a crucial parameter that controls the smoothness of the KDE. It determines the width of the kernel functions. A smaller bandwidth leads to a more sensitive, less smooth estimate, while a larger bandwidth produces a smoother, less sensitive estimate.

  3. Summation of Kernels: KDE constructs the overall density estimate by summing the contributions of each kernel function across all data points. Each data point contributes a small “bump” to the estimate, and the sum of these bumps forms the estimated density function.

KDE Formula:

Given a set of $ n $ data points $ x_1, x_2, \ldots, x_n $, the KDE at a point $ x $ is calculated as:

$$
\hat{f}(x) = \frac{1}{n \cdot h} \sum_{i=1}^{n} K\left(\frac{x - x_i}{h}\right)
$$

Where:

  • $ \hat{f}(x) $ is the estimated density at point $ x $.
  • $ n $ is the number of data points.
  • $ h $ is the bandwidth.
  • $ K $ is the kernel function.
  • $ x_i $ are the observed data points.

Example of KDE:

Imagine you have a dataset of people’s heights. Rather than assuming the heights follow a specific distribution (like normal), KDE allows you to estimate the distribution directly from the data, which may reveal subtle features like bimodal distributions (e.g., a mix of two distinct groups).

Advantages of KDE:

  • Flexible: KDE doesn’t assume any specific form of the distribution, making it suitable for complex and unknown distributions.
  • Smooth Estimation: It provides a smooth estimate of the density function, which can be more informative than histograms.

Disadvantages of KDE:

  • Choice of Bandwidth: The performance of KDE heavily depends on the choice of bandwidth. Too small a bandwidth can lead to overfitting, while too large a bandwidth can oversmooth important features.
  • Computationally Intensive: KDE can be computationally intensive, especially for large datasets and high-dimensional data.

Applications of KDE:

  • Data Visualization: KDE is often used to visualize the distribution of data, particularly in one-dimensional and two-dimensional cases.
  • Anomaly Detection: KDE can be used to detect outliers by identifying areas of low probability density.
  • Density-Based Clustering: In clustering methods like DBSCAN, KDE can help define regions of high density.

How Do It in Python

import numpy as np
from sklearn.neighbors import KernelDensity
import matplotlib.pyplot as plt
import seaborn as sns

# Step 2: Prepare Your Data
# Example list of values
data_list = [1.2, 2.3, 2.4, 2.5, 3.1, 3.6, 3.8, 4.0, 4.2, 5.0] * 30
# Convert the list to a NumPy array and reshape it for the model
data = np.array(data_list).reshape(-1, 1)
# Step 3: Fit the Kernel Density Estimation Model
# Fit the KDE model
kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(data)
# Step 4: (Optional) Plot the Estimated Density
# Define a range of values
x_range = np.linspace(0, 6, 1000).reshape(-1, 1)
# Estimate density for the entire range
log_density = kde.score_samples(x_range)
density = np.exp(log_density)
# Plot the density
sns.kdeplot(data_list, bw_adjust=0.5)
plt.plot(x_range, density)
plt.title("Kernel Density Estimation")
plt.xlabel("Value")
plt.ylabel("Density")
plt.show()
KDE plot

Save and Load the Model

import pickle

# Fit the KDE model (assuming you have already done this)
# kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(data)

# Save the model to a file
with open('kde_model.pkl', 'wb') as file:
pickle.dump(kde, file)

# Load the model from the file
with open('kde_model.pkl', 'rb') as file:
loaded_kde = pickle.load(file)

# Value for which you want to estimate the density
value = 3.5

# Estimate the density using the loaded model
log_density = loaded_kde.score_samples([[value]])
density = np.exp(log_density)
print(f"Density of the value {value} using loaded model: {density[0]:.6f}")
Author

Karobben

Posted on

2024-08-16

Updated on

2024-10-09

Licensed under

Comments