Metadata-Version: 2.4
Name: dannce-density
Version: 0.1.0
Summary: Data-augmented nonparametric noise contrastive density estimation
Author: Chenghao Li
License-Expression: MIT
Project-URL: Paper, https://www.jmlr.org/papers/v27/25-0376.html
Project-URL: Research Code, https://github.com/Li-Chenqhao/DANNCE
Keywords: density estimation,noise contrastive estimation,DANNCE
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.26
Requires-Dist: torch>=2.7.1
Dynamic: license-file

# DANNCE density estimation

`dannce-density` fits the data-augmented nonparametric noise contrastive estimator of Li and Lin (2026) to continuous data and evaluates the estimated density at new points. See the [JMLR paper](https://www.jmlr.org/papers/v27/25-0376.html) and [research code](https://github.com/Li-Chenqhao/DANNCE).

## Installation

Use Python 3.11 or later with a compatible PyTorch build.

```sh
python -m pip install dannce-density
```

For a specific CPU or CUDA build, install [PyTorch](https://pytorch.org/get-started/locally/) first. `device="auto"` selects CUDA when available and CPU otherwise.

## Usage

```python
import numpy as np
from dannce_density import DANNCE

rng = np.random.default_rng(42)
centers = rng.choice([-1.5, 1.5], size=2000)
X = rng.normal(loc=centers, scale=0.4)

model = DANNCE(random_state=42).fit(X)
X_new = [-1.5, 0.0, 1.5]
print(model.pdf(X_new))
print(model.log_pdf(X_new))
```

Use an array of shape `(n_samples, n_features)` to fit multivariate data, with one observation per row. For univariate data, a one-dimensional array also works. For a single multivariate query, pass a vector of length `n_features`. `pdf` and `log_pdf` return a NumPy array with one entry for each query.

`pdf` sets negative estimates to zero and `log_pdf` returns `-inf` at those zeros. `log_pdf` evaluates in log space and can retain very small estimates that underflow in `pdf`. Use `pdf(X_new, clip=False)` for the signed estimate in equation (4), for example to rank anomalies from lowest to highest. Finite-sample estimates may fail to integrate to one, and tail estimates can be unreliable.

## Parameters

| Parameter | Default | Meaning |
| --- | --- | --- |
| `rho` | `0.3` | Ratio of augmentation draws to the number of observations. `rho=0` runs noise contrastive estimation without augmentation. |
| `nu` | `5` | Number of contrastive noise draws per augmented observation. |
| `reference_std` | Largest sample standard deviation across columns | Common standard deviation of the isotropic Gaussian reference, in the original units. The Gaussian is centered at the sample mean. |
| `output_scale` | Eight times the peak reference density | Upper bound of the scaled sigmoid that models the mixture density, in the original units. |
| `depth`, `hidden_width` | `4`, `32` | Number of hidden layers and units per layer in the LeakyReLU network. |
| `learning_rate` | `0.0002` | Adam learning rate. |
| `batch_size` | `128` | Maximum number of augmented observations per update. Each batch uses `nu` times as many contrastive draws. |
| `max_iter` | `2500` | Number of optimizer updates. |
| `device` | `"auto"` | `"auto"`, `"cpu"`, `"cuda"`, or a CUDA index such as `"cuda:1"`. |
| `random_state` | `None` | Supply a nonnegative integer to seed the reference draws, minibatch sampling, and network initialization. |

The fit draws `floor(rho * n_samples)` augmentation points. The density correction uses the actual ratio, available as `model.rho_`. The fitted reference standard deviation is available as `model.reference_std_`. If all observations are identical, supply a positive `reference_std` to fit. The default settings are starting points. Feature scale, reference spread, mixture-density bounds, and training length can all affect the fit. See Sections 2 and 5 of the [paper](https://www.jmlr.org/papers/v27/25-0376.html).

## Citation and license

Chenghao Li and Yuanyuan Lin (2026). A Data-Augmented Contrastive Learning Approach to Nonparametric Density Estimation. *Journal of Machine Learning Research*, 27(10), 1–47. [BibTeX](https://www.jmlr.org/papers/v27/25-0376.bib)

The package adapts the LeakyReLU network and contrastive training from the [MIT-licensed research code](https://github.com/Li-Chenqhao/DANNCE/tree/8bce06524e1f56725dbeb4b4f29c0821fe0691ee).
