I'm trying to reimplement ScipPy's
gaussian_kde to understand how exactly the estimation works so that I can reimplement it in C++ later. I'm following this article and I assumed that I just need to sum a number of gaussians and that's my result.
points = np.array([3.0, 4.0, 6.0], dtype=float) def gaussian_kernel(u): return np.exp(-0.5 * u**2) / np.sqrt(2 * np.pi) bw = 0.6 x = np.linspace(0, 10, 100) y = np.zeros_like(x) for i in points: g = gaussian_kernel((x - i) / bw) / len(points) plt.plot(x, g, alpha=0.1) y += g plt.plot(x, y) kde = gaussian_kde(points, bw_method=bw) plt.plot(x, kde(x))
However, I realized that the bandwith factor in
gaussian_kde doesn't do what I expected it to do and I'm getting results like this:
Scaling of the gaussians in my code is clearly wrong, but also the width of the gaussians in
gaussian_kde depends on the number of points in the data set. I have tried reading the code, but the math is a bit above my head (I'm not very familiar with statistics).
Can someone explain what exactly does the
bw_factor parameter mean and how does it affect the individual gaussians?