Skip to main content

Analytic-Hessian bandwidth selection for univariate kernel regression

Project description

Hessband: Analytic Bandwidth Selector

CI PyPI version PyPI Downloads Documentation License: MIT Python 3.11+

Hessband is a Python package for selecting bandwidths in univariate smoothing. It provides analytic gradients and Hessians of the leave‑one‑out cross‑validation (LOOCV) risk for Nadaraya–Watson regression and least‑squares cross‑validation (LSCV) for kernel density estimation (KDE). Bandwidth selectors include grid search, plug‑in rules, finite‑difference Newton, analytic Newton, golden‑section search, and Bayesian optimisation.

Installation

From PyPI (Recommended)

pip install hessband

From Source

To install from source, clone the repository and install:

git clone https://github.com/finite-sample/hessband.git
cd hessband
pip install .

Development Installation

For development, install in editable mode with test dependencies:

git clone https://github.com/finite-sample/hessband.git
cd hessband
pip install -e ".[dev]"

Usage Example

import numpy as np
from hessband import select_nw_bandwidth, nw_predict

# Generate synthetic data
X = np.linspace(0, 1, 200)
y = np.sin(2 * np.pi * X) + 0.1 * np.random.randn(200)

# Select the optimal bandwidth via the analytic-Hessian method
h_opt = select_nw_bandwidth(X, y, method='analytic', kernel='gaussian')

# Predict on the original points
y_pred = nw_predict(X, y, X, h_opt)

print("Selected bandwidth:", h_opt)
print("Mean squared error:", np.mean((y_pred - np.sin(2 * np.pi * X)) ** 2))

When running the example above, you should see a selected bandwidth around 0.16 and a mean squared error close to 8e-4. Results may vary slightly due to randomness in the synthetic data.

KDE Example

The package also supports bandwidth selection for univariate kernel density estimation using least‑squares cross‑validation (LSCV). For example:

import numpy as np
from hessband import select_kde_bandwidth

# Sample data from a bimodal distribution
x = np.concatenate([
    np.random.normal(-2, 0.5, 200),
    np.random.normal(2, 1.0, 200),
])

# Select bandwidth using analytic Newton for the Gaussian kernel
h_kde = select_kde_bandwidth(x, kernel='gauss', method='analytic')
print("Selected KDE bandwidth:", h_kde)

The select_kde_bandwidth function also supports Epanechnikov kernels (kernel='epan'), grid search (method='grid') and golden‑section optimisation (method='golden').

Simulation Results

In the accompanying paper, we compared several bandwidth selectors using simulated data from a bimodal mixture regression model. A subset of the results for the Gaussian kernel with noise level 0.1 and sample size 200 is given below:

Method MSE (×10⁻³) CV evaluations
Grid 0.87 ± 0.12 150 ± 0
Plug-in 6.31 ± 0.57 5 ± 0
Finite-diff Newton 6.31 ± 0.57 20 ± 0
Analytic Newton 0.86 ± 0.13 0 ± 0
Golden 0.86 ± 0.13 85 ± 0
Bayes 0.87 ± 0.14 75 ± 0

The analytic-Hessian method matches the accuracy of exhaustive grid search while requiring essentially no cross-validation evaluations.

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

hessband-0.1.2.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

hessband-0.1.2-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file hessband-0.1.2.tar.gz.

File metadata

  • Download URL: hessband-0.1.2.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hessband-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2d5291f7dd90dbf0f58e2cd78770440e579be301285ccae3e5eb568ff819a9e3
MD5 5ac94ee47bc5e140f4563361ff70d9a6
BLAKE2b-256 d1a3c93f19ba7e6e4cdf2364db8bf8b4289e9427f9060aa984ad60b2fd3ff5ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for hessband-0.1.2.tar.gz:

Publisher: python-publish.yml on finite-sample/hessband

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hessband-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: hessband-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hessband-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 df410b2f84bbd3957522e92c61c0cd7811a7de2f94ef42d0314f951c5012adfb
MD5 0463cb38b5c218763adcf389a00ba529
BLAKE2b-256 2e96910373a0b7a11a737203bd13a7797b5d0b342be0b509242c6b1ae8db693f

See more details on using hashes here.

Provenance

The following attestation bundles were made for hessband-0.1.2-py3-none-any.whl:

Publisher: python-publish.yml on finite-sample/hessband

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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