Null-calibrated similarity metrics via permutation testing
Project description
calibrated-similarity
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 alphap_value: Add-one permutation p-valuetau: 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
generatorparameter 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ec4defd05abff5dfc9337f96ae7799b7ca4692ad27b17e91c62f7989ee5faa4
|
|
| MD5 |
5ad3b29f41a1bbc2d4c933858efa1206
|
|
| BLAKE2b-256 |
c279ec12891bc6a6ba76b332dcf97f6444a5eae01dfa90f403793f3a350a1144
|
Provenance
The following attestation bundles were made for calibrated_similarity-0.1.0.tar.gz:
Publisher:
release.yml on mlbio-epfl/Aristotelian
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
calibrated_similarity-0.1.0.tar.gz -
Subject digest:
3ec4defd05abff5dfc9337f96ae7799b7ca4692ad27b17e91c62f7989ee5faa4 - Sigstore transparency entry: 900601536
- Sigstore integration time:
-
Permalink:
mlbio-epfl/Aristotelian@30b157ca5a5c38d6249676654a3a1791fcffa5be -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/mlbio-epfl
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@30b157ca5a5c38d6249676654a3a1791fcffa5be -
Trigger Event:
push
-
Statement type:
File details
Details for the file calibrated_similarity-0.1.0-py3-none-any.whl.
File metadata
- Download URL: calibrated_similarity-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e5566b50ffba12975384bc90d4c500636733fb6ae80ebc48580c266af5810d7
|
|
| MD5 |
329c6ea60708f7b51711609904eb06dc
|
|
| BLAKE2b-256 |
750f9a5d8b5a269d8af44b699e39412e8ff3ab1a545619cfb0648d761b282b54
|
Provenance
The following attestation bundles were made for calibrated_similarity-0.1.0-py3-none-any.whl:
Publisher:
release.yml on mlbio-epfl/Aristotelian
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
calibrated_similarity-0.1.0-py3-none-any.whl -
Subject digest:
7e5566b50ffba12975384bc90d4c500636733fb6ae80ebc48580c266af5810d7 - Sigstore transparency entry: 900601573
- Sigstore integration time:
-
Permalink:
mlbio-epfl/Aristotelian@30b157ca5a5c38d6249676654a3a1791fcffa5be -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/mlbio-epfl
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@30b157ca5a5c38d6249676654a3a1791fcffa5be -
Trigger Event:
push
-
Statement type: