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 and research code.
Installation
Use Python 3.11 or later with a compatible PyTorch build.
python -m pip install dannce-density
For a specific CPU or CUDA build, install PyTorch first. device="auto" selects CUDA when available and CPU otherwise.
Usage
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.
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
The package adapts the LeakyReLU network and contrastive training from the MIT-licensed research code.
Metadata
Release files for dannce-density 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dannce_density-0.1.0.tar.gz | 12.8 kB | Without provenance | Details for dannce_density-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dannce_density-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for dannce_density-0.1.0-py3-none-any.whl |
Total release size: 22.8 kB
Release files / dannce_density-0.1.0.tar.gz
| Download URL | dannce_density-0.1.0.tar.gz |
|---|---|
| Size | 12.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
23a6fb536f0fb117dfdad54e76b491d2cdfb4f2098f698a81bb48c5ecaffdc20
|
|
BLAKE2b-256 checksum How to use checksums |
827ecad8e7ec4318fef32b4c45edcc83186e5377c41267a45f22dab7c9ab2c46
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / dannce_density-0.1.0-py3-none-any.whl
| Download URL | dannce_density-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dc005702fc1d0f56c5c1b878e09b357a115533e23b10b71337347a16ac22bac4
|
|
BLAKE2b-256 checksum How to use checksums |
316fcd908294877716e0bbc5befbc60ba095e2d69aafd7ced8af889bf43c5810
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Security for dannce-density 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.