A simple fixed-bandwidth 1D Gaussian KDE implementation for Python

2.9k Views Asked by At

I need a simple Kernel Density Estimation with fixed bandwidth and Gaussian kernel.

Alas, in scipy.stats.gaussian_kde I saw only an automatic bandwidth selection. The set_bandwidth method, as far as I see, only multiplies the auto-selected values with some correcting ratios. And I just need to set the bandwidth I want to set.

I don't want to use my hand-written Python code for KDE: it works a bit too slow.

Don't you know any replacements of scipy.stats.gaussian_kde?

4

There are 4 best solutions below

2
On

In scipy.stats.gaussian_kde, you can supply a scalar to the bw_method option. This will set the kde.factor variable. kde.factor is a number by which the covariance matrix is multiplied, so it should be the same as the bandwidth.

0
On

You might use the KernelSmoothing class in OpenTURNS. An example is provided in Non parametric distribution fitting and the theory is described here. There is an automatic multidimensional bandwidth rule, but the bandwidth can be set by the user. Also, the library provides the Sheather and Jones "solve-the-equation" plugin rule which is efficient for multimodal distributions and a mixed rule (which is less CPU consuming than the former).

0
On

You can also try the KDE's in scikit-learn (sklearn): http://scikit-learn.org/stable/modules/density.html It has a variety of different kernels you can try, and lets you directly set bandwidth, but apparently (!) has no methods for automatic bandwidth selection.

0
On

You might be interested in KDE in statsmodels. Example here.