Skip to main content

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

Metadata

Release files for calibrated-similarity 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for calibrated-similarity 0.1.1
File Size Uploaded Provenance Actions
calibrated_similarity-0.1.1.tar.gz 9.6 kB With provenance Details for calibrated_similarity-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for calibrated-similarity 0.1.1
File Interpreter ABI Platform Actions
calibrated_similarity-0.1.1-py3-none-any.whl Python 3 none any Details for calibrated_similarity-0.1.1-py3-none-any.whl

Total release size: 18.1 kB

Release files / calibrated_similarity-0.1.1.tar.gz

Download URL calibrated_similarity-0.1.1.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
27099459ebb5299550f05a7f98d8c3f379641c6d62c822f938f1091df4228343
BLAKE2b-256 checksum
How to use checksums
0ad8f9790bf2d27255500b8f18138c2a1c962ee47288f3eb46102337152e6a52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 2, 2026.

Transparency log

Release files / calibrated_similarity-0.1.1-py3-none-any.whl

Download URL calibrated_similarity-0.1.1-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be3e01dd8fa8db435fb9e5c60bed847dadbfb2c3311b6a172c9545bc17a1a916
BLAKE2b-256 checksum
How to use checksums
def4c76f96e70016626fdad487fed60013d97c009be9bacaaa50d68f084d3144
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 2, 2026.

Transparency log

Security for calibrated-similarity 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for calibrated_similarity-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page