Metadata-Version: 2.4
Name: diffcb
Version: 0.1.1
Summary: Differentiable Critical Bandwidth: Silverman's modality test as a differentiable PyTorch layer with IFT backward pass.
Project-URL: Homepage, https://github.com/ryZhangHason/differentiable-critical-bandwidth
Project-URL: Repository, https://github.com/ryZhangHason/differentiable-critical-bandwidth
Project-URL: Documentation, https://github.com/ryZhangHason/differentiable-critical-bandwidth#readme
Project-URL: Bug Tracker, https://github.com/ryZhangHason/differentiable-critical-bandwidth/issues
Author-email: Ruiyu Zhang <dhhhason@gmail.com>
License: MIT License
        
        Copyright (c) 2026 Ruiyu Zhang
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: PyTorch,anomaly detection,critical bandwidth,differentiable programming,generative models,kernel density estimation,mode counting,nonparametric statistics
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.9
Requires-Dist: matplotlib>=3.7.0
Requires-Dist: numpy>=1.24.0
Requires-Dist: scikit-learn>=1.3.0
Requires-Dist: scipy>=1.10.0
Requires-Dist: torch>=2.0.0
Provides-Extra: dev
Requires-Dist: black>=23.0.0; extra == 'dev'
Requires-Dist: pytest-cov>=4.1.0; extra == 'dev'
Requires-Dist: pytest>=7.4.0; extra == 'dev'
Requires-Dist: ruff>=0.1.0; extra == 'dev'
Provides-Extra: notebooks
Requires-Dist: ipywidgets>=8.0.0; extra == 'notebooks'
Requires-Dist: jupyter>=1.0.0; extra == 'notebooks'
Description-Content-Type: text/markdown

# DCB — Differentiable Critical Bandwidth

[![PyPI](https://img.shields.io/pypi/v/diffcb.svg)](https://pypi.org/project/diffcb/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
[![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/)

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.

```python
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

```bash
pip install diffcb
```

Or from source:

```bash
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

```bash
# 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](LICENSE).
