PIT-CP conformal prediction with pivotal scores
Project description
🎯 PIT-CP
pitcp is a Python package for conformal prediction using probability integral transform (PIT) pivotal scores. Given any black-box nonconformity score, it fits a conditional density estimator on the score distribution and maps raw scores to PIT values, yielding valid marginal coverage at any user-specified level.
✨ Features
- PIT Conformal Prediction: Maps base nonconformity scores through a learned conditional CDF, producing asymptotically exact conditional coverage.
- Model-agnostic: Works with any callable nonconformity score
s(x, y), including distance, residual, or likelihood-based scores. - Flexible Density Estimation: Supports normalizing flows and mixture density networks from the zuko library.
- Marginal Coverage Guarantee: Provably valid conformal coverage at any target level via finite-sample calibration.
- scikit-learn: Native
BaseEstimatorintegration with a familiarfit/predictAPI.
🚀 Installation
pip install pitcp
🔧 Usage
Example
import torch
import zuko
from pitcp import PITCP
def std(x):
return torch.where((x > -0.9) & (x < 0.9), torch.cos(torch.pi * x / 2), 1.0)
def gen_data(n):
x = torch.rand(n, 1) * 2 - 1
return x, torch.randn(n, 1) * std(x)
torch.manual_seed(42)
(X_train, y_train), (X_cal, y_cal), (X_test, y_test) = [
gen_data(n) for n in (5000, 1000, 5000)
]
# Define a nonconformity score
def score(x, y):
return y.abs()
# Build a normalizing flow density estimator
model = zuko.flows.NSF(features=1, context=1, bins=4, hidden_features=(32, 32, 32))
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
# Fit and conformalize
pitcp = PITCP(score, model, optimizer, n_epochs=100, batch_size=64)
pitcp.fit(X_train, y_train)
pitcp.conformalize(X_cal, y_cal)
# Predict coverage at 90%
covered = pitcp.predict(X_test, y_test, quantile=0.9)
print(f"Empirical coverage: {covered.float().mean():.3f}")
📖 Learn More
For tutorials, API reference, visit the official site:
👉 pitcp's documentation
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
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 pitcp-0.2.0.tar.gz.
File metadata
- Download URL: pitcp-0.2.0.tar.gz
- Upload date:
- Size: 417.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c48755ae7dea8be26e9a420af3fb9a6053bfd02b8f042b1875a577d20d5f98af
|
|
| MD5 |
271c3d20e8c7425730cbd81604e4104e
|
|
| BLAKE2b-256 |
e55d0f15161b84dd73e67d8aa07cf1b1b46cbcc5ed41d87a42d6d9742ccc189e
|
File details
Details for the file pitcp-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pitcp-0.2.0-py3-none-any.whl
- Upload date:
- Size: 18.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5a2643ad9a058fba7cb8f77f602c9f8792a9f288ff5bb5a1e863528d30d64fd
|
|
| MD5 |
9ccbc2a350bc2d46a9939d8f48ddcacc
|
|
| BLAKE2b-256 |
b5d31bc72358c1015d62dcdf5cd62f2d1e3cc4c8d9e66fe07a54b1b260e7a29f
|