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.2.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.2-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: thetakde-0.3.2.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.2.tar.gz
Algorithm Hash digest
SHA256 6bbbc502e505a6159fdca21abd7ae442b6c3882726816b25fb7ff746f55b3a56
MD5 af111f769876fa3b7c4307753becdd1e
BLAKE2b-256 e898caf19c195fb24b46d3aa656428b43b0d5a8272f6bf3a3e608f99a3a0f22d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: thetakde-0.3.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9befdf901b31a468acd3e3ec7ce879c582bcab53e2227273932fd4fd21994f54
MD5 dd9698aefa2c40cbe0f4f5a2a2f1cfc3
BLAKE2b-256 d3a561acbea22a3ab4c9526f6fa8383f45eb152a4dd5d3f4732edabd5e9fdea1

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