Skip to main content

A package for the theta kernel density estimator

Project description

Theta Kernel Density Estimation

This repository contains a Python implementation of the fast Theta Kernel Density Estimator (KDE) from [2] with the optimal bandwidth selection method from [1].

Introduction

Kernel Density Estimation (KDE) is a non-parametric method for estimating an unknown probability density function from a dataset of independent and identically distributed observations. The theta KDE [2] allows estimation of densities on bounded intervals in a way that achieves consistent estimation at the boundaries. It is highly efficient due to its use of the discrete cosine transform (DCT). A judicious choice of the bandwidth parameter ensures that the theta KDE avoids both oversmoothing and undersmoothing. This is accomplished through direct minimization of the mean integrated square error (MISE), as described in [1].

Function

kde(data, m=2**16, MIN=None, MAX=None, sig=None, resamp=False)

This function performs the theta kernel density estimation on the input data.

Parameters:

  • data (numpy array): Input data for density estimation.
  • m (int, optional): Number of grid points. Default is 2**16.
  • MIN (float, optional): Minimum value of the data range.
  • MAX (float, optional): Maximum value of the data range.
  • sig (float, optional): Bandwidth parameter. If not provided, it will be optimized.
  • resamp (bool, optional): Set to True if the data not iid, but has repeated values, e.g., due to resampling. Default is False.

Returns:

  • density (numpy array): Estimated density values.
  • mesh (numpy array): Grid points corresponding to the density values.
  • sig (float): Optimized bandwidth parameter.

Usage

Install the ThetaKDE package from the Python Package Index to access thetakde module:

pip install ThetaKDE

Example

Generate data from an exponential distribution and compare the KDE with the true PDF.

import numpy as np
from matplotlib import pyplot as plt
from thetakde import kde

# Generate exponential data
data = -np.log(np.random.uniform(size=5000))

# Estimate density
density, mesh, sig = kde(data)

# Plot
plt.plot(mesh, density,'r', label='KDE')
plt.plot(mesh, np.exp(-mesh), 'b--', label='True PDF')
plt.legend()
plt.show()

References

[1] Z.I. Botev, D.P. Kroese, and T. Taimre (2025). Data Science and Machine Learning: Mathematical and Statistical Methods, Second Edition. Chapman & Hall/CRC, Boca Raton.

[2] Z.I. Botev, J.F. Grotowski, and D.P. Kroese (2010). Kernel density estimation via diffusion. Annals of Statistics, 38(5), 2916–2957.

@Book{DSML2e,
  author = {Z. I. Botev and D.P. Kroese and T. Taimre},
  title = {Data Science and Machine Learning: Mathematical and Statistical Methods},
  publisher = {Chapman \&  Hall/CRC},
  year = {2025},
  address = {Boca Raton},
  edition = {2nd}
}

@ARTICLE{thetakde,
  author = {Z. I. Botev and J. F. Grotowski and D. P. Kroese},
  title = {{Kernel density estimation via diffusion}},
  journal = {Annals of Statistics},
  year = {2010},
  volume = {38},
  pages = {2916--2957},
  number = {5}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thetakde-0.2.2.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

thetakde-0.2.2-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file thetakde-0.2.2.tar.gz.

File metadata

  • Download URL: thetakde-0.2.2.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for thetakde-0.2.2.tar.gz
Algorithm Hash digest
SHA256 afe5aa34931031130360c83146bed9e14ec1c302f4fad895d900202b45a6a9e1
MD5 09b5501cc879f23fa694cc1b3e10e0d3
BLAKE2b-256 cbb06d1d0d8979e07c5b882516621410c8b74da09767f4d916e1214e6e1c0f3f

See more details on using hashes here.

File details

Details for the file thetakde-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: thetakde-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for thetakde-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 680018502a35d70e6943393ba57e9b317530bddf083c66e605a19ef5b1cb9cb8
MD5 f2e6775e087e4f83d861315c1ca72924
BLAKE2b-256 f4055cb0dabbd00ed73762dd0089a7e7dd71840b1dd706c8dfc3391e880f1339

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page