Skip to main content

Efficient Maximum Mean Discrepancy two-sample testing for data with duplicate observations, scaling with unique values rather than sample size.

Project description

Unique Maximum Mean Discrepancy (uMMD)

An efficient implementation of the Maximum Mean Discrepancy two-sample test for datasets with duplicate observations via count-weighting of unique values. This implementation scales with unique data values rather than sample size.

Installation

pip install ummd

Quick start

import numpy as np
from ummd import MMD

rng = np.random.default_rng(0)
x = rng.integers(0, 10, size=500)   # sample from one distribution
y = rng.integers(2, 12, size=500)   # sample from a shifted distribution

result = MMD(x, y, unique=True, bandwidths=10, n_permutations=999)

print(result["biased_MMD"])   # MMD statistic per bandwidth
# [ 0.04408069 0.053788   0.06124013 0.06328209 0.06290089 0.0602459 0.04713144 0.02831863 0.01431563 0.0066321 ]

print(result["p-value"])    # combined p-value across bandwidths
# 0.001

Interpreting the result

MMD returns a dictionary with:

  • biased_MMD: the MMD statistic for each tested bandwidth
  • p-values_per_bandwidth: permutation p-value for each bandwidth
  • p-value: a single Cauchy-combined p-value across the bandwidths
  • bandwidths: the kernel bandwidths actually used

Custom kernels

By default MMD uses a Gaussian (RBF) kernel. You can supply your own via kernel_fn. A custom kernel should build the entire stacked kernel matrix. This is to allow control of how it is vectorised/optimised:

import numpy as np
from scipy.spatial.distance import cdist
from ummd import MMD

def my_rbf(x, y, bandwidths):
    gammas = 1.0 / (2.0 * bandwidths**2)
    D = cdist(x, y, metric="sqeuclidean")
    return np.exp(-gammas[:, None, None] * D[None, :, :])  # (len(bandwidths), m, n)

result = MMD(x, y, kernel_fn=my_rbf, bandwidths=10, n_permutations=999)

The contract is kernel_fn(x, y, bandwidths) -> ndarray of shape (len(bandwidths), m, n), one kernel matrix per bandwidth. bandwidths is the 1-D array of sigma length-scales that MMD resolves from the bandwidths= argument (median heuristic, geometric grid, or an explicit array); your kernel decides how to interpret them, and may ignore them entirely and return a single (1, m, n) matrix.

Why uMMD

A standard MMD test builds an N x N kernel matrix, so cost grows with sample size. When your data has many repeated values (counts, categories, discretised measurements), uMMD instead works over the u unique values, where u << n, giving the same test at a fraction of the cost.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ummd-0.3.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

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

ummd-0.3.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file ummd-0.3.0.tar.gz.

File metadata

  • Download URL: ummd-0.3.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for ummd-0.3.0.tar.gz
Algorithm Hash digest
SHA256 a975a585ac9f8838cc09f1b76bb4c5d1499d5a856e934aa25525750e6df1f26f
MD5 49a39ae6289b60d0254428fdefc92425
BLAKE2b-256 ff1eca9cce3763a05440e9e6187a9ef44141fa776e743168e9376d7e0892d424

See more details on using hashes here.

File details

Details for the file ummd-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: ummd-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for ummd-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 18d6dd1a210db582dbb580d761b0a5c7340d2b2880193f4e58ddf04965ff3c20
MD5 ef27a6efaed6dfcc8c7d0383a6ea79e7
BLAKE2b-256 ea5aecd831b6fdf3c50087bb651d3b06665caebd76bce2a3d4c35b014defd5d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page