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.2.0.tar.gz (10.6 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.2.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ummd-0.2.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","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 ummd-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7261098794a94086f844363eec9f8ee7462e882bb3dc9c36f72e6b5103067f6a
MD5 8a58f3070962d60e283d40365ba9214d
BLAKE2b-256 948bc52d336fe872c6fe5a480073c49a81cc92896e515e9eaa3e82377c2f23a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ummd-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","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 ummd-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b61dc41fffb8f68a506c51030ac3f492ff47346420747995f7391ac6b50602ef
MD5 b07193f87f7965d8e46ce973394977cf
BLAKE2b-256 4cdb47a2c1ec8eea77783ca6cc096d9b86b8f3351d0cf76ee570838acaa3dd12

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