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

Uploaded Python 3

File details

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

File metadata

  • Download URL: calibrated_similarity-0.1.1.tar.gz
  • Upload date:
  • Size: 9.6 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.1.tar.gz
Algorithm Hash digest
SHA256 27099459ebb5299550f05a7f98d8c3f379641c6d62c822f938f1091df4228343
MD5 2d544d0a595b9cd9d7031ea6812b672f
BLAKE2b-256 0ad8f9790bf2d27255500b8f18138c2a1c962ee47288f3eb46102337152e6a52

See more details on using hashes here.

Provenance

The following attestation bundles were made for calibrated_similarity-0.1.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for calibrated_similarity-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 be3e01dd8fa8db435fb9e5c60bed847dadbfb2c3311b6a172c9545bc17a1a916
MD5 a7ea9478993ef5f4d8576e115438590d
BLAKE2b-256 def4c76f96e70016626fdad487fed60013d97c009be9bacaaa50d68f084d3144

See more details on using hashes here.

Provenance

The following attestation bundles were made for calibrated_similarity-0.1.1-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