Skip to main content

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 BaseEstimator integration with a familiar fit / predict API.

🚀 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

pitcp-0.1.0.tar.gz (718.7 kB view details)

Uploaded Source

Built Distribution

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

pitcp-0.1.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file pitcp-0.1.0.tar.gz.

File metadata

  • Download URL: pitcp-0.1.0.tar.gz
  • Upload date:
  • Size: 718.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for pitcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8a65d2f0af833667b7ff0fe718ab357bcd49b9a5bcb50fc84cac5bb6a9a9facc
MD5 7a8936867d81a242cb7ecbfa74bac7bc
BLAKE2b-256 d41eb36c7d81f44e907944dc442ae3ff3651e725580e977c04353a8f14fcdab8

See more details on using hashes here.

File details

Details for the file pitcp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pitcp-0.1.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

Hashes for pitcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50787c47cb5af8ea0b2b7616a8e5e0377edf9e8cadcdec57ed96f0de244a923c
MD5 bee8820984649b17c236a646d3c10552
BLAKE2b-256 a6d3c86710af135fef70c541b9507e52ed0953f1d10d087bbe13c5425a0630e1

See more details on using hashes here.

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