Conformal Risk Control
Standard conformal prediction controls coverage: \(P(Y \in C(X)) \geq 1 - \alpha\). Conformal Risk Control (CRC) (Angelopoulos et al. 2024) extends this to control the expected per-sample loss \(\mathbb{E}[L(Y, C(X))] \leq \alpha\) for any loss function \(L\) that is monotone non-increasing in set size (larger sets ⇒ smaller loss) and bounded in \([0, B]\).
The Risk Control Guarantee
Given a calibration set of size \(n\) and a one-parameter family \(\{ C_\lambda \}\) of nested prediction-set rules (larger \(\lambda\) ⇒ larger sets ⇒ smaller loss), CRC selects
\[ \hat{\lambda} \;=\; \inf \Bigl\{ \lambda \;:\; \frac{n \, \hat{R}(\lambda) + B}{n + 1} \;\leq\; \alpha \Bigr\}, \]
where \(\hat{R}(\lambda) = \frac{1}{n}\sum_{i=1}^n L(y_i, C_\lambda(x_i))\) is the empirical risk on the calibration set. With this choice the test-time guarantee is
\[\mathbb{E}\bigl[L(Y_{n+1}, C_{\hat{\lambda}}(X_{n+1}))\bigr] \;\leq\; \alpha,\]
and holds in finite samples for exchangeable data. Picking the smallest valid \(\lambda\) yields the smallest prediction sets that still meet the bound.
Basic Usage
from sklearn.ensemble import RandomForestClassifier
from conformalpy.risk_control import ConformalRiskController, fnr_loss
# Train base model
model = RandomForestClassifier().fit(X_train, y_train)
# Control the marginal FNR-loss at 5%
crc = ConformalRiskController(
model=model,
loss_function=fnr_loss,
alpha=0.05, # Target: E[fnr_loss] <= 5%
)
crc.calibrate(X_calib, y_calib)
# Get prediction sets with controlled FNR-loss
pred_sets = crc.predict(X_test)
# CRC controls the *marginal* mean of the per-sample loss (not the
# conditional FNR among positives). For `fnr_loss` this equals
# P(Y = positive AND positive ∉ C(X)).
losses = fnr_loss(y_test, pred_sets)
marginal_fnr_loss = losses.mean()
print(f"Marginal E[fnr_loss]: {marginal_fnr_loss:.1%} (CRC bound: 5%)")
# If you want the *conditional* FNR — P(positive ∉ C | Y = positive) —
# divide by the positive prevalence; it can exceed alpha when positives
# are rare.
positives = y_test == 1
conditional_fnr = losses[positives].mean()
print(f"Conditional FNR | Y=1: {conditional_fnr:.1%}")Available Loss Functions
conformalpy provides several loss functions:
from conformalpy.risk_control import (
miscoverage_loss, # Standard 0/1 loss (equivalent to coverage)
fnr_loss, # False Negative Rate
fdr_loss, # False Discovery Rate
precision_loss, # Same as FDR
recall_loss, # Same as FNR
)When to use each:
| Loss | Controls | Use Case |
|---|---|---|
| miscoverage_loss | P(Y not in C(X)) | Standard coverage |
| fnr_loss | Miss rate for positives | Medical diagnosis, fraud detection |
| fdr_loss | False discovery rate | Scientific testing, precision-critical |
The Monotonicity Requirement
Loss functions must be monotone non-increasing in set size:
- As prediction sets grow, loss should decrease
- This ensures the optimization is well-behaved
For example, FNR decreases when we include more classes (larger sets are less likely to miss the positive class).
Custom Loss Functions
You can define custom loss functions:
def custom_loss(y_true, prediction_sets, **kwargs):
"""
Compute loss for each sample.
Parameters
----------
y_true : ndarray
True labels.
prediction_sets : list of list of int
Prediction sets.
Returns
-------
losses : ndarray
Loss value for each sample.
"""
# Define class-specific costs
class_costs = {0: 10, 1: 100} # Cost of missing each class
losses = np.zeros(len(y_true))
for i, (y, s) in enumerate(zip(y_true, prediction_sets)):
if y not in s:
losses[i] = class_costs.get(y, 1)
return losses
# Use custom loss
crc = ConformalRiskController(
model=model,
loss_function=custom_loss,
alpha=0.1,
)When to Use CRC
Use CRC when:
- You need to control a specific loss (FNR, FDR, custom)
- Some errors are more costly than others
- Coverage alone doesn’t capture your goals
- Safety-critical domains (medical, fraud, security)
Use standard conformal when:
- Coverage is your primary concern
- All errors are equally important
- Simpler interpretation needed
CRC vs Standard CP: Key Difference
| Aspect | Standard CP | Conformal Risk Control |
|---|---|---|
| Target | P(Y in C(X)) >= 1-alpha | E[L(Y, C(X))] <= alpha |
| Loss | Miscoverage (implicit) | Any monotone loss |
| Use case | General coverage | Domain-specific risk |
See the risk control example for a complete CRC workflow with FNR control.
Multi-Label Risk Control
For multi-label classification (document tagging, medical diagnosis with comorbidities, etc.), conformalpy provides MultiLabelConformalClassifier with three calibration methods:
Methods
| Method | Guarantee | Loss Requirement | Best For |
|---|---|---|---|
| CRC | E[L] <= alpha | Monotone only | Tightest sets |
| RCPS | P(R <= alpha) >= 1-delta | Monotone only | Strongest guarantee |
| LTT | Hypothesis testing | Any bounded loss | Non-monotone losses |
Loss Functions
from conformalpy.risk_control import (
multilabel_recall_loss, # 1 - recall (MONOTONE)
multilabel_precision_loss, # 1 - precision (non-monotone)
multilabel_hamming_loss, # symmetric diff / K (non-monotone)
multilabel_f1_loss, # 1 - F1 (non-monotone)
)Basic Usage
from conformalpy.risk_control import MultiLabelConformalClassifier
# CRC: control recall at 10% loss
mlcc = MultiLabelConformalClassifier(
model=multilabel_model, # must have predict_proba()
alpha=0.1,
method='crc',
loss='recall',
)
mlcc.calibrate(X_calib, y_calib) # y_calib: (n_samples, n_labels) binary
pred = mlcc.predict(X_test) # returns (n_samples, n_labels) binary
# Verify risk is controlled
risk = mlcc.compute_risk(X_test, y_test)
print(f"Empirical recall: {1 - risk:.3f}")
# RCPS: stronger guarantee with delta parameter
rcps = MultiLabelConformalClassifier(
model=model, alpha=0.1, method='rcps', delta=0.1
)
# LTT: control precision (non-monotone loss!)
ltt = MultiLabelConformalClassifier(
model=model, alpha=0.2, method='ltt', loss='precision'
)Risk Curves
Visualize how risk varies with the threshold:
lambdas, risks = mlcc.get_risk_curve()
plt.plot(lambdas, risks)
plt.axhline(y=alpha, color='red', linestyle='--')
plt.axvline(x=mlcc.lambda_, color='blue', linestyle='--')See the multi-label risk control example for a complete multi-label risk control workflow.
References
- CRC: Angelopoulos et al. (2024) (Angelopoulos et al. 2024)
- RCPS / LTT: Bates et al. (2021) (Bates et al. 2021)