Exchangeability Diagnostics

Conformal prediction assumes exchangeability between calibration and test data. The diagnostics module provides martingale-based tests to validate this assumption.

The Core Idea

Under exchangeability, conformal p-values are uniformly distributed on [0, 1]. Martingales are betting schemes that detect deviations from uniformity. If the martingale value exceeds a threshold c, the probability of this under exchangeability is at most 1/c (Ville’s inequality).

Basic Usage

from conformalpy.diagnostics import ExchangeabilityTest

# Test exchangeability between calibration and test scores
et = ExchangeabilityTest(method='simple_jumper', threshold=100)
result = et.test(calibration_scores, test_scores)

if result['rejected']:
    print("Warning: exchangeability may be violated!")
    print(f"Max martingale: {result['max_martingale']:.2f}")
else:
    print("No evidence against exchangeability")

Available Methods:

Method Description Use Case
simple_jumper Adaptive grid over epsilon values General use (recommended)
power Fixed epsilon power martingale When you know the best epsilon

Common Thresholds:

Threshold Significance Interpretation
20 ~5% Moderate evidence
100 ~1% Strong evidence
1000 ~0.1% Very strong evidence

Lower-Level API

For more control, use the martingale classes directly:

from conformalpy.diagnostics import (
    compute_conformal_p_values,
    PowerMartingale,
    SimpleJumper,
)

# Step 1: Compute p-values
p_values = compute_conformal_p_values(calib_scores, test_scores)

# Step 2: Run martingale test
sj = SimpleJumper()
values = sj.update(p_values)

# Step 3: Check threshold
if sj.exceeds_threshold(100):
    print(f"Exchangeability violated! Best epsilon: {sj.best_epsilon()}")

See the exchangeability test example for a complete exchangeability testing example with drift detection.

References

Fedorova, Valentina, Alexander Gammerman, Ilia Nouretdinov, and Vladimir Vovk. 2012. “Plug-in Martingales for Testing Exchangeability on-Line.” International Conference on Machine Learning, 1427–34.
Vovk, Vladimir. 2021. “Testing Randomness Online.” Statistical Science 36 (4): 595–611. https://doi.org/10.1214/20-STS817.
Vovk, Vladimir, Ilia Nouretdinov, and Alexander Gammerman. 2003. “Testing Exchangeability on-Line.” International Conference on Machine Learning, 768–75.