Skip to main content

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.

Python License Lint

DEFER samples recovering a 3-D spiral density

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 selection

Infer each kernel's hyperparameter posterior and use the evidence log Z to choose between a Matérn-3/2 and a sum of two Matérn-3/2. The data is generated from the sum kernel, and the evidence prefers it.

GP hyperparameter posterior

Black-box Gibbs density on a spiral

A 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.

Spiral density corner plot

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

deference-0.2.0.tar.gz (2.1 MB view details)

Uploaded Source

Built Distribution

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

deference-0.2.0-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

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

Hashes for deference-0.2.0.tar.gz
Algorithm Hash digest
SHA256 541a3e979b766deb3741f50f674c4e21cb507c788488a63088d54fa1c642c712
MD5 794176b02360870073b9f685fb440763
BLAKE2b-256 a40da3430cd3ca7ca025b65a19a59fb4bfc869eb1a8bd8960ecd0eefb8b731eb

See more details on using hashes here.

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

Hashes for deference-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2977d464b7d55abe1aff4efdd40568bca13cc0e1bdb5df098961a10b35aab27d
MD5 231ddbd4217a6543ae3b9d54fbff3ba4
BLAKE2b-256 ad57f497e4b200e9179e3ce4cd7a9cf86175299540ac829c8171ec025a3c20fe

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page