Analytic-Hessian bandwidth selection for univariate kernel regression
Project description
Hessband: Analytic Bandwidth Selector
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hessband-0.2.0.tar.gz.
File metadata
- Download URL: hessband-0.2.0.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66979b4cfee7bfbfbd8198b176132992d83da70b313592ccd6c3792182d7bd7a
|
|
| MD5 |
6107f2d0164f6cdc6241a64a89618659
|
|
| BLAKE2b-256 |
1ee4075794149e5aca776f5fb6db3cad81d34853f1344a39334031fff6d25e56
|
Provenance
The following attestation bundles were made for hessband-0.2.0.tar.gz:
Publisher:
python-publish.yml on finite-sample/hessband
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hessband-0.2.0.tar.gz -
Subject digest:
66979b4cfee7bfbfbd8198b176132992d83da70b313592ccd6c3792182d7bd7a - Sigstore transparency entry: 778535262
- Sigstore integration time:
-
Permalink:
finite-sample/hessband@25a96ecfca4a6a9f2ce02dd3ba085b4f0c054e34 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/finite-sample
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@25a96ecfca4a6a9f2ce02dd3ba085b4f0c054e34 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file hessband-0.2.0-py3-none-any.whl.
File metadata
- Download URL: hessband-0.2.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0bb24a54dea6b28ea6b8db1d45a99b4cecd4b8671e093b1c97a53a7d129f78bf
|
|
| MD5 |
ea453ac0e172a01a4c6ba2655a7fee5d
|
|
| BLAKE2b-256 |
526da371f7d712a8f1b886376012c0ec760ca49579af9a341aa60ca83e4f645f
|
Provenance
The following attestation bundles were made for hessband-0.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on finite-sample/hessband
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hessband-0.2.0-py3-none-any.whl -
Subject digest:
0bb24a54dea6b28ea6b8db1d45a99b4cecd4b8671e093b1c97a53a7d129f78bf - Sigstore transparency entry: 778535270
- Sigstore integration time:
-
Permalink:
finite-sample/hessband@25a96ecfca4a6a9f2ce02dd3ba085b4f0c054e34 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/finite-sample
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@25a96ecfca4a6a9f2ce02dd3ba085b4f0c054e34 -
Trigger Event:
workflow_dispatch
-
Statement type: