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.0.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.3.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: thetakde-0.3.0.tar.gz
  • Upload date:
  • Size: 15.0 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.0.tar.gz
Algorithm Hash digest
SHA256 c418255d713e71ee8f2472e601a5b527c34d18dfac5ad5361e1944268fd1db83
MD5 7ef86bfca7e426a6f3fb571dfc1017b6
BLAKE2b-256 127fa5966de00c99568a5653ef8cc477e2dbaae2cae2bc7c67b20f7a4d601cfb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: thetakde-0.3.0-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.5

File hashes

Hashes for thetakde-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a05ef16cd3558abb024e48fda81a2b73b071d94d108ead168931ed69d93ebb5f
MD5 72df65f7ca58e9b3b322c50339b65fea
BLAKE2b-256 e43109ad9b3e7b9639a033866558df7b82f0dd92a7fce46da2b1b6ec5dbe6bf0

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