DEFER
DEnsity Function Estimation using Recursive partitioning
Approximate any black-box unnormalised density from point evaluations alone, and get samples, the normalising constant, and more. Runs on PyTorch or NumPy.
200k samples drawn from a DEFER approximation of a density concentrated on a 3-D spiral.
Overview
DEFER approximates an arbitrary unnormalised (log-)density over a bounded box using only pointwise evaluations: no gradients, no closed form, no conjugacy. It recursively partitions the domain, spending evaluations where probability mass concentrates, and returns a single object that provides:
- fast, constant-time sampling (via the alias method),
- the normalising constant
Z, i.e. the model evidence, which DEFER approximates, - closed-form expectations under the approximation, including the mean, variance, and differential entropy.
It is robust and fast, and applies to problems of up to around 20 dimensions. The budget is a
number of function evaluations: each evaluation adds one partition, so an approximation
with N partitions costs N evaluations of your density and takes O(N log N) time and
O(N) space. Every numerical step runs through a PyTorch backend (on a chosen device) or
NumPy. The method is described in Black-box density function estimation using recursive
partitioning (arXiv:2010.13632, ICML 2021).
Install
Managed with uv (Python 3.13):
uv sync
Quickstart
import numpy as np
from defer import defer
# Any unnormalised log-density you can evaluate pointwise (here a 2-D Gaussian).
def log_density(x):
return -0.5 * (x**2).sum(-1)
approx = defer(
log_density,
lower=np.array([-5.0, -5.0]),
upper=np.array([5.0, 5.0]),
num_fn_calls=2000, # density evaluations; one partition is added per evaluation
use_numpy=True, # or use_numpy=False, device="cpu" / "cuda"
)
approx.log_z # log normalising constant (the evidence)
approx.mean() # posterior mean
samples = approx.sampler()(10_000) # (10000, 2) samples, O(1) each
Set use_numpy=False, device="cuda" to run the same code on a GPU with PyTorch.
Examples
Runnable notebooks live in examples/ (uv sync installs the plotting extras):
Gaussian-process kernel selectionInfer each kernel's hyperparameter posterior and use the evidence |
Black-box Gibbs density on a spiralA density concentrated on a 3-D spiral, defined only by distance to the curve. DEFER recovers a sampler, the normalising constant, and the shape itself. |
Development
make lint # ruff check + format
make typecheck # pyright
make test # pytest (numpy and torch backends)
License
MIT, see LICENSE.
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 deference-0.2.0.tar.gz.
File metadata
- Download URL: deference-0.2.0.tar.gz
- Upload date:
- Size: 2.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
541a3e979b766deb3741f50f674c4e21cb507c788488a63088d54fa1c642c712
|
|
| MD5 |
794176b02360870073b9f685fb440763
|
|
| BLAKE2b-256 |
a40da3430cd3ca7ca025b65a19a59fb4bfc869eb1a8bd8960ecd0eefb8b731eb
|
File details
Details for the file deference-0.2.0-py3-none-any.whl.
File metadata
- Download URL: deference-0.2.0-py3-none-any.whl
- Upload date:
- Size: 20.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2977d464b7d55abe1aff4efdd40568bca13cc0e1bdb5df098961a10b35aab27d
|
|
| MD5 |
231ddbd4217a6543ae3b9d54fbff3ba4
|
|
| BLAKE2b-256 |
ad57f497e4b200e9179e3ce4cd7a9cf86175299540ac829c8171ec025a3c20fe
|