Skip to main content

Calibration and uncertainty quantification for ranking systems

Project description

rankcal

PyPI version Python 3.9+ License: MIT Docs Open In Colab

Calibration and uncertainty quantification for ranking systems. PyTorch-first.

Why rankcal?

Existing calibration libraries treat calibration as a classification problem. But ranking decisions happen at the top-k, and miscalibration there is what actually breaks business outcomes.

rankcal provides:

  • Ranking-aware calibration metrics - ECE@k, top-k reliability diagrams
  • Monotonic calibrators - Temperature scaling, isotonic regression, splines, neural networks
  • Decision analysis - Risk-coverage curves, utility optimization

Installation

pip install rankcal

For development:

pip install -e ".[dev]"

Quick Start

import torch
from rankcal import TemperatureScaling, ece_at_k, reliability_diagram

# Your ranking scores and binary relevance labels
scores = torch.tensor([0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1])
relevance = torch.tensor([1, 1, 0, 1, 0, 0, 1, 0, 0])

# Fit a calibrator
calibrator = TemperatureScaling()
calibrator.fit(scores, relevance)

# Calibrate scores
calibrated = calibrator(scores)

# Evaluate calibration at top-k
ece = ece_at_k(calibrated, relevance, k=5)
print(f"ECE@5: {ece:.4f}")

# Visualize calibration
fig = reliability_diagram(calibrated, relevance, k=5)
fig.savefig("reliability.png")

Calibrators

Calibrator Differentiable Parametric Description
TemperatureScaling Single learned temperature parameter
IsotonicCalibrator Non-parametric, piecewise constant
PiecewiseLinearCalibrator Monotonic piecewise linear interpolation
MonotonicNNCalibrator Neural network with monotonicity constraints

GPU Support

All calibrators are PyTorch nn.Module subclasses and support GPU acceleration:

import torch
from rankcal import TemperatureScaling

# Move calibrator to GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
calibrator = TemperatureScaling().to(device)

# Fit with data on GPU
scores = scores.to(device)
labels = labels.to(device)
calibrator.fit(scores, labels)

# Inference on GPU
test_scores = test_scores.to(device)
calibrated = calibrator(test_scores)

Run GPU tests with:

pytest tests/test_gpu.py --device cuda  # or --device mps on Mac

Metrics

  • ece(scores, labels) - Expected Calibration Error
  • ece_at_k(scores, labels, k) - ECE computed only on top-k items
  • reliability_diagram(scores, labels, k=None) - Visualization of calibration

Citation

If you use rankcal in academic work, please cite:

@software{hodge2026rankcal,
  author = {Hodge, John},
  title = {rankcal: Calibration for Ranking Systems},
  year = {2026},
  url = {https://github.com/jman4162/rankcal},
  version = {0.2.0}
}

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

rankcal-0.2.0.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

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

rankcal-0.2.0-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for rankcal-0.2.0.tar.gz
Algorithm Hash digest
SHA256 de7766b9461b87cc009deec472e5a97376d3bc4cf800ac24ef99f61a8a3c3508
MD5 af87db4a44221a8e1a376044c2779fea
BLAKE2b-256 78de0512246331cb5fa0766249d24f53f38915ea0adf4993771b7de196649f92

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankcal-0.2.0.tar.gz:

Publisher: publish.yml on jman4162/rankcal

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

File details

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

File metadata

  • Download URL: rankcal-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 23.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rankcal-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a2151dfe5a58a4b4c17431267c5afd31aa5225b515f186f3db60a58bc17bfdc
MD5 c741d686162edec1d134bcacf2aed212
BLAKE2b-256 899b14b8f1261bfcc75f9ebb01c8991623530e5a224f36de6fa7f69383a2feed

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankcal-0.2.0-py3-none-any.whl:

Publisher: publish.yml on jman4162/rankcal

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