Skip to main content

Null-calibrated similarity metrics via permutation testing

Project description

calibrated-similarity

PyPI License: MIT

Null-calibrated similarity metrics via permutation testing.

This package implements the calibration algorithms from:

Revisiting the Platonic Representation Hypothesis: An Aristotelian View Gröger, Wen, Brbić.

Installation

pip install calibrated-similarity

The Problem

Representation similarity metrics (CKA, kNN overlap, RSA, etc.) have non-zero random baselines that depend on sample size, dimensionality, and the metric itself:

  • A raw score of 0.3 might be highly significant in one setting but within random chance in another
  • Comparing scores across studies with different configurations is misleading
  • Searching for the best-matching layer pair inflates scores even under the null

The Solution

Null calibration uses permutation testing to estimate what similarity scores look like under the null hypothesis (no true alignment). It returns a calibrated score that is zero under the null, enabling valid comparisons.

Quick Start

import torch
from calibrated_similarity import calibrate, calibrate_layers

# Define any similarity function
def cka(X, Y):
    X, Y = X - X.mean(0), Y - Y.mean(0)
    hsic_xy = (X @ X.T * (Y @ Y.T)).sum()
    hsic_xx = (X @ X.T * (X @ X.T)).sum()
    hsic_yy = (Y @ Y.T * (Y @ Y.T)).sum()
    return hsic_xy / torch.sqrt(hsic_xx * hsic_yy)

# Sample data
X = torch.randn(100, 64)
Y = torch.randn(100, 64)

# Calibrate the similarity
calibrated_score, p_value, threshold = calibrate(X, Y, cka)
print(f"Calibrated: {calibrated_score:.3f}, p={p_value:.3f}")

API Reference

calibrate() — Scalar Calibration

Calibrates a single similarity score against a permutation null distribution:

calibrated_score, p_value, tau = calibrate(
    X, Y, sim_fn,
    K=200,        # Number of permutations
    alpha=0.05,   # Significance level
    smax=1.0,     # Maximum possible similarity
)

Returns:

  • calibrated_score: Normalized score in [0, 1], zero under the null at level alpha
  • p_value: Add-one permutation p-value
  • tau: Critical threshold at the (1-alpha) quantile

calibrate_layers() — Aggregation-Aware Calibration

For comparing multiple layers between two models, applies the same permutation across all layers to properly control for multiple comparisons:

X_layers = [model_A.layer(i, data) for i in range(5)]
Y_layers = [model_B.layer(j, data) for j in range(5)]

calibrated_agg, p_value, tau = calibrate_layers(
    X_layers, Y_layers, sim_fn,
    agg="max",    # "max", "mean", or custom callable
)

Features

  • Any similarity function: Works with CKA, kNN, RSA, cosine similarity, or custom metrics
  • Valid p-values: Uses the add-one formula for proper permutation p-values
  • GPU support: Tensors stay on their original device
  • Reproducible: Optional generator parameter for deterministic results

Citation

@article{groger2026revisiting,
  title   = {Revisiting the Platonic Representation Hypothesis: An Aristotelian View},
  author  = {Gr{\"o}ger, Fabian and Wen, Shuo and Brbi{\'c}, Maria},
  journal = {arXiv preprint},
  year    = {2026},
}

License

MIT

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

calibrated_similarity-0.1.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

calibrated_similarity-0.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file calibrated_similarity-0.1.0.tar.gz.

File metadata

  • Download URL: calibrated_similarity-0.1.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for calibrated_similarity-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3ec4defd05abff5dfc9337f96ae7799b7ca4692ad27b17e91c62f7989ee5faa4
MD5 5ad3b29f41a1bbc2d4c933858efa1206
BLAKE2b-256 c279ec12891bc6a6ba76b332dcf97f6444a5eae01dfa90f403793f3a350a1144

See more details on using hashes here.

Provenance

The following attestation bundles were made for calibrated_similarity-0.1.0.tar.gz:

Publisher: release.yml on mlbio-epfl/Aristotelian

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file calibrated_similarity-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for calibrated_similarity-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e5566b50ffba12975384bc90d4c500636733fb6ae80ebc48580c266af5810d7
MD5 329c6ea60708f7b51711609904eb06dc
BLAKE2b-256 750f9a5d8b5a269d8af44b699e39412e8ff3ab1a545619cfb0648d761b282b54

See more details on using hashes here.

Provenance

The following attestation bundles were made for calibrated_similarity-0.1.0-py3-none-any.whl:

Publisher: release.yml on mlbio-epfl/Aristotelian

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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