Skip to content

Conformal Prediction API

deepuq.methods.conformal

Conformal Prediction methods for distribution-free uncertainty quantification.

AdaptiveConformalPredictor

Online adaptive conformal predictor with coverage guarantees.

Parameters:

Name Type Description Default
model Module

Trained model.

required
target_coverage float

Target coverage level (default 0.9).

0.9
gamma float

Step size for online threshold updates.

0.01

calibrate

calibrate(cal_X: Tensor, cal_y: Tensor) -> None

Initial calibration on held-out data.

predict_set

predict_set(x: Tensor) -> tuple[torch.Tensor, torch.Tensor]

Predict intervals using current threshold.

update

update(x_new: Tensor, y_new: Tensor) -> None

Online update of threshold based on observed coverage.

Parameters:

Name Type Description Default
x_new Tensor
required
y_new Tensor
required

CQRPredictor

Bases: BaseConformalPredictor

Adaptive conformal intervals using quantile regression.

Requires a model that outputs (lower_quantile, upper_quantile) as a tensor of shape (N, 2) or a tuple.

ConformalClassifier

Bases: BaseConformalPredictor

Prediction sets with marginal coverage guarantee.

Supports Adaptive Prediction Sets (APS) and Regularized APS (RAPS).

ConformalUQWrapper

Bases: BaseConformalPredictor

Calibrate intervals from any predict_uq()-compatible method.

Wraps Laplace, Ensembles, MC Dropout, etc. to guarantee coverage.

SplitConformalRegressor

Bases: BaseConformalPredictor

Distribution-free prediction intervals for any regression model.

Guarantees P(Y in [lower, upper]) >= 1 - alpha for exchangeable data.

WeightedConformalPredictor

Conformal predictor with importance-weighted calibration.

Parameters:

Name Type Description Default
model Module

Trained model (callable on input tensors).

required
score_fn Callable[..., Tensor] | None

Callable (model, x, y) -> scores. Defaults to absolute residual.

None

calibrate

calibrate(
    cal_X: Tensor, cal_y: Tensor, weights: Tensor
) -> None

Calibrate using weighted nonconformity scores.

Parameters:

Name Type Description Default
cal_X Tensor
required
cal_y Tensor
required
weights Tensor
required

predict_set

predict_set(
    x: Tensor, alpha: float = 0.1
) -> tuple[torch.Tensor, torch.Tensor]

Compute prediction intervals.

Parameters:

Name Type Description Default
x Tensor
required
alpha float
0.1

Returns:

Type Description
Tuple of (lower, upper) bounds.

absolute_residual_score

absolute_residual_score(
    y_pred: Tensor, y_true: Tensor
) -> torch.Tensor

Absolute residual: |y - y_hat|.

check_coverage

check_coverage(
    y_true: Tensor, lower: Tensor, upper: Tensor
) -> float

Compute empirical coverage fraction.

conformal_quantile

conformal_quantile(scores: Tensor, alpha: float) -> float

Compute the conformal quantile of nonconformity scores.

Returns the ceil((n+1)*(1-alpha))/n quantile of the scores.

normalized_residual_score

normalized_residual_score(
    y_pred: Tensor, y_true: Tensor, sigma: Tensor
) -> torch.Tensor

Normalized absolute residual: |y - y_hat| / sigma.

quantile_score

quantile_score(
    y_true: Tensor, q_lo: Tensor, q_hi: Tensor
) -> torch.Tensor

CQR score: max(q_lo - y, y - q_hi).

signed_residual_score

signed_residual_score(
    y_pred: Tensor, y_true: Tensor
) -> torch.Tensor

Signed residual: y - y_hat.