Kernel Density Estimation

Using SciPy to estimate the probability density function of a random variable

Python
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

# Generate sample data
np.random.seed(42)
data = np.concatenate([np.random.normal(-2, 1, 200), np.random.normal(2, 1, 200)])

# Perform kernel density estimation
kde = stats.gaussian_kde(data)

# Create points for evaluation
x_eval = np.linspace(data.min() - 1, data.max() + 1, 100)

# Plot the results
plt.figure(figsize=(10, 6))
plt.hist(data, bins=30, density=True, alpha=0.7, label='Histogram')
plt.plot(x_eval, kde(x_eval), 'r-', label='KDE')
plt.title("Kernel Density Estimation")
plt.xlabel("Value")
plt.ylabel("Density")
plt.legend()
plt.show()
Click Run or press shift + ENTER to run code.