mennthor / awkde

Adaptive Width KDE with Gaussian Kernels
MIT License
40 stars 21 forks source link

Adaptive Width KDE with Gaussian Kernels

Installation

This uses the awesome pybind11 package which makes creating C++ bindings super convenient. Only the evaluation is written in a small C++ snippet to speed it up, the rest is a pure python implementation.

The setup is taken from the example at https://github.com/pybind/python_example Just clone the repository and invoke pip:

git clone https://github.com/mennthor/awkde
pip install [--user] [-e] ./awkde

Note: --user and -e are optional. Use --user to install in your user site-package directory (recommended) and -e if you want to only link the files to the site-package directories, so that when you make any changes in your local copy, you don't have to reinstall all the time.

Try to run the example/examply.py:

cd awkde/example
python example.py

You should get this plot in the examples folder (you need matplotlib for the plot):

example plot

Algorithm

The unweighted kernel density estimator is defined as

kernel density formula

where the product h * lambda takes the role of a local sigma.

The kernel bandwith is choosen locally to account for variations in the density of the data. Areas with large density gets smaller kernels and vice versa. This smoothes the tails and gets high resolution in high statistics regions. The local bandwidth paramter is defined as

kernel density formula

where

kernel density formula

is some normalization and ^f(X_i) the KDE estimate at the data point X_i. The local bandwidth is multiplied to the global bandwidth for each kernel.

Furthermore different scales in data is accounted for by scaling it via its covariance matrix to an equal spread. First a global kernel bandwidth is applied to the transformed data and then based on that density a local bandwidth parameter is applied.

All credit for the method goes to [1] and to S. Schoenen and L. Raedel for huge parts of the implementation. For information on Silverman or Scott rule, see [2] or [3].

References