Skip to main content

Differentiable Critical Bandwidth: Silverman's modality test as a differentiable PyTorch layer with IFT backward pass.

Project description

DCB — Differentiable Critical Bandwidth

PyPI License: MIT Python 3.9+

A PyTorch package that makes Silverman's critical bandwidth test (1981) fully differentiable, enabling end-to-end gradient-based optimization over the modal structure of continuous distributions.

Overview

The critical bandwidth h_crit is the minimum KDE bandwidth at which a distribution appears to have at most m modes — a classical nonparametric statistic for modality testing. DCB replaces every non-differentiable operation in its computation with a smooth surrogate, then uses the Implicit Function Theorem to compute exact gradients through the root-finding step at O(1) memory cost.

import torch
from dcb import DCBLayer

X = torch.randn(256, requires_grad=True)   # 1D samples
layer = DCBLayer(target_modes=1)
h_crit = layer(X)                          # differentiable scalar
h_crit.backward()                          # exact IFT gradients

Installation

pip install diffcb

Or from source:

git clone https://github.com/ryZhangHason/differentiable-critical-bandwidth
cd differentiable-critical-bandwidth
pip install -e ".[dev]"

Paper

Ruiyu Zhang. "Differentiable Critical Bandwidth: Making Silverman's Modality Test End-to-End Trainable." Journal of Machine Learning Research, 2026 (in preparation).

Confirmed Experimental Results

All results produced on Kaggle GPU (T4 / P100) — see experiments/ and outputs/.

Experiment Result Criterion
Validation (m≥2) R²=0.91, MAE=0.07, Spearman ρ=0.89 R²≥0.85, MAE≤0.10 ✓
Speedup vs scipy (n=8192) 10.5× on T4 ≥3× ✓
GAN mode preservation h_crit=1.232 >> 0.3 h_crit>0.3 ✓
Anomaly AUC (KDDCup99) DCB=0.9982 vs IF=0.9867 DCB≥IF ✓

Repository Structure

dcb/            Core PyTorch package (layer.py, solver.py, kde.py, utils.py)
experiments/    Reproduction scripts for all paper figures and tables
  phase1_validation.py   Figure 1: DCB vs reference h_crit scatter
  phase1_speedup.py      Figure 2: GPU speedup benchmark
  phase1_ablation.py     Figures S1–S2: ε/τ sensitivity heatmaps
  phase2_gan.py          Figure 3: GAN mode-collapse prevention
  phase3_anomaly.py      Table 2 + Figure 5: anomaly detection benchmark
tests/          Unit tests (pytest, 35/35 passing)
outputs/        All generated figures and tables (PDFs, PNGs, CSVs)
notebooks/      Quickstart and demo notebooks

Reproducing Paper Results

# Phase 1: validation, speedup, ablation
python experiments/phase1_validation.py
python experiments/phase1_speedup.py
python experiments/phase1_ablation.py

# Phase 2: GAN mode collapse experiment
python experiments/phase2_gan.py

# Phase 3: anomaly detection benchmark
python experiments/phase3_anomaly.py

For GPU runs, use the provided Kaggle kernels:

  • Phase 1–2: hsingle/dcb-full-experiments
  • Phase 3: hsingle/dcb-phase-3-anomaly-detection

Kaggle GPU Notes

Kaggle may assign a P100 (sm_60) instead of T4. The Phase 3 kernel handles this automatically by installing torch==2.2.2+cu118 (the earliest PyTorch release with both Python 3.12 and sm_60 support) when P100 is detected.

License

MIT — see LICENSE.

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

diffcb-0.1.1.tar.gz (32.1 kB view details)

Uploaded Source

Built Distribution

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

diffcb-0.1.1-py3-none-any.whl (27.8 kB view details)

Uploaded Python 3

File details

Details for the file diffcb-0.1.1.tar.gz.

File metadata

  • Download URL: diffcb-0.1.1.tar.gz
  • Upload date:
  • Size: 32.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.7

File hashes

Hashes for diffcb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aa30a4e1b77df535091fdea00ef921111a469744ac5f2f054a97a2165c267373
MD5 a06b96eceffa67f3771d2189ae6ccdda
BLAKE2b-256 eb206987fbc1332b2e5da851df0ad6d71970912cb9f7233afeccd7c03ee5e1fd

See more details on using hashes here.

File details

Details for the file diffcb-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: diffcb-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 27.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.7

File hashes

Hashes for diffcb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f9daa2f7a3667fbe70c23fff5dafc5c26279d4622d7b0b26d0b0ee6cbeb36837
MD5 4249bebef2906e090b4eeac3d95316fc
BLAKE2b-256 ab13f5176bc8bf0a3de465531ee748b0bcf6989297d0e2e931c233674790119d

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