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 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.3.4.tar.gz (17.4 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.3.4-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for thetakde-0.3.4.tar.gz
Algorithm Hash digest
SHA256 0e3bacf620ff267f72b307c68b69baac406085338a205e0abc7fe835c0186d71
MD5 f7412537d8dd2ef7b6c8635a5c95e202
BLAKE2b-256 4eebdc5767a9cbe42caf32804b4b2d9258fd5c4f879dcef604c60e485453dced

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for thetakde-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 749bcb75b085dae0f050769fa768e57b4a0e94578075adba9fe9b2a9768574b4
MD5 84bf6fc915292b4e5a4de06dffdcbfda
BLAKE2b-256 983fd2b77cbb8ade2698155812d88720c85a04ce81b1b7a8da344357b2240580

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