mixle.ppl.conformal module¶
Split (inductive) conformal prediction — distribution-free, finite-sample valid prediction intervals and label sets around any already-fitted model.
Conformal prediction turns point predictions into calibrated sets using a held-out
calibration split, with a coverage guarantee that holds for any model and any data
distribution as long as the calibration and test points are exchangeable: a set built at
level alpha covers the truth with probability at least 1 - alpha. A wrong model
only makes the sets wider, never breaks the guarantee.
The machinery is a nonconformity score plus one order statistic. For regression the score
is the absolute residual |y - yhat| and the calibrated interval is
predict(x) +/- qhat; for classification the score is 1 - p(true class | x) and the
label set is {y : 1 - p(y | x) <= tau}. Both reduce to the conformal quantile
qhat / tau — the ceil((n + 1)(1 - alpha)) smallest calibration score, the
+1 being the finite-sample correction.
ConformalRegressor wraps a fitted RegressionResult
(anything exposing predict(given)); ConformalClassifier wraps a matrix of
per-class probabilities (e.g. the posterior of a mixle generative classifier). The
conformal() helper is the one-liner entry point for the regression case.
- conformal_quantile(scores, alpha)[source]
The level-
alphaconformal quantile of calibrationscores.Returns the
ceil((n + 1)(1 - alpha))smallest score (the finite-sample-corrected empirical1 - alphaquantile). Whenalphais too small for the calibration size —(n + 1)(1 - alpha) > n— no finite threshold gives the requested coverage andinfis returned, the honest “the set is everything” answer.
- class ConformalRegressor(result, y_cal, *, given, alpha=0.1)[source]
Bases:
objectSplit-conformal prediction intervals around a fitted regression
result.Calibrates the absolute-residual nonconformity score on held-out
(given, y_cal)and produces symmetric intervalspredict(x) +/- qhatwith marginal coverage at least1 - alpha.resultis any object with apredict(given)method returning the fitted mean (aRegressionResult, a location-scale result, or a GP regressor).- interval(given)[source]
Return
(lower, upper)arrays of the conformal interval at covariatesgiven.
- class ConformalClassifier(proba_cal, y_cal, *, alpha=0.1)[source]
Bases:
objectSplit-conformal label sets from per-class probabilities.
proba_calis an(n, K)matrix of calibration probabilitiesp(y | x)(any proper classifier — a mixle generative classifier’s class posterior, a softmax, …) andy_calthe integer labels. The nonconformity score is1 - p(true)and the calibrated set keeps every label whose score is within the conformal quantile, so it covers the true label with probability at least1 - alphaand grows from one label (confident) to several (hedging) as the model is unsure.- Parameters:
proba_cal (Any)
y_cal (Any)
alpha (float)
- predict_set(proba)[source]
Boolean
(n, K)label-inclusion matrix at probabilitiesproba.
- covers(proba, y)[source]
Boolean array: is each true label
yin the predicted set.
- class ConformalQuantileRegressor(lo, hi, y_cal, *, given, alpha=0.1)[source]
Bases:
objectConformalized quantile regression (Romano, Patterson, Candes 2019).
Combines two fitted quantile regressions (a lower and an upper conditional quantile) with a split-conformal calibration so the band has exact marginal coverage and the adaptive, heteroscedastic width of quantile regression — wide where the data is noisy, narrow where it is tight, unlike the constant-width absolute-residual band of
ConformalRegressor.The nonconformity score is the signed distance outside the predicted band,
E_i = max(qlo(x_i) - y_i, y_i - qhi(x_i))(negative wheny_iis comfortably inside), and the calibrated band is[qlo(x) - qhat, qhi(x) + qhat]withqhatthe conformal quantile of the calibration scores.loandhiare fitted quantile-regression results (from...fit(..., quantile=tau)), typically attau = alpha/2and1 - alpha/2.- interval(given)[source]
Return
(lower, upper)arrays of the calibrated adaptive band at covariatesgiven.
- class ConformalStructure(dist, calibration, *, alpha=0.1)[source]
Bases:
objectSplit-conformal credible sets over combinatorial structures (rankings, matchings, spanning trees, permutations, …) from a fitted mixle distribution’s exact log-density.
The nonconformity of a structure
sis-log p(s): the lower its model probability, the more surprising it is. Calibrating on held-out true structures yields a log-probability threshold, and the conformal set is{s : log p(s) >= threshold}— it contains the true structure with probability at least1 - alphawhenever the calibration and test structures are exchangeable (for example iid draws), with no assumption on the model being correct.distis any structure distribution exposinglog_density(PlackettLuceDistribution,MallowsDistribution,MatchingDistribution,SpanningTreeDistribution, …);calibrationis a sequence of observed structures. Membership is always available; listing or counting the set additionally needs the distribution’s exactenumerator().- Parameters:
dist (Any)
calibration (Any)
alpha (float)
- property log_prob_threshold: float
Structures with
log p(s)at or above this value are in the conformal set.
- contains(structure)[source]
Is
structurein the conformal set (its log-probability above the threshold).
- covers(structures)[source]
Boolean array: membership of each structure (use on held-out truths to check coverage).
- members()[source]
List the structures in the conformal set, highest-probability first.
Requires the distribution’s exact
enumerator()(raisesEnumerationErrorotherwise). The enumerator yields structures in descending log-probability, so the scan stops at the threshold.- Return type:
- class ConformalLinkPredictor(edge_prob, cal_edges, *, alpha=0.1)[source]
Bases:
objectSplit-conformal candidate-neighbor sets for a random-graph model from its edge-probability matrix
P(P[i, j] = p(edge i--j), e.g.X @ X.Tfrom a fitted RDPG, or an Erdos-Renyi / stochastic-block-model edge probability).The nonconformity of a present edge
(i, j)is1 - P[i, j]. Calibrating on held-out true edges gives a threshold; the predicted neighbor set of a node keeps every candidatejwith1 - P[i, j] <= tau, so it contains a true neighbor with probability at least1 - alphaover exchangeable held-out edges (a random split of the observed edges).- Parameters:
edge_prob (Any)
cal_edges (Any)
alpha (float)
- neighbor_set(i, candidates=None)[source]
Candidate nodes
jin nodei’s conformal neighbor set.
- covers(edges)[source]
Boolean array: is each held-out true edge’s endpoint in the predicted neighbor set.
- class ConformalKnowledgeGraph(kg, calibration, *, slot='tail', alpha=0.1)[source]
Bases:
objectSplit-conformal completion sets for a knowledge-graph model (any-slot UQ).
Calibrating on held-out true triples turns the model’s completion posterior into a set of candidate fillers that contains the true one with probability at least
1 - alphaover exchangeable held-out triples.slotselects which slot is predicted –'tail'for(h, r, ?),'head'for(?, r, t),'relation'for(h, ?, t)– using the model’stail_log_posterior/head_log_posterior/relation_log_posterior. The nonconformity of a triple is1 - p(true filler), so a confident model gives small completion sets and a recommended completion carries a coverage guarantee.- completion_set(h=None, r=None, t=None)[source]
Candidate fillers in the conformal set for the missing slot of a query.
- covers(triples)[source]
Boolean array: is each held-out true triple’s filler in the completion set.
- conformal(result, y_cal, *, given, alpha=0.1)[source]
Split-conformal calibration of a fitted regression
resultinto prediction intervals.Mirrors
fit’s convention (labels positional,given=keyword), a one-liner overConformalRegressor:m = Normal(free * Field("x") + free, free).fit(y_tr, given={"x": x_tr}) cp = conformal(m.result, y_cal, given={"x": x_cal}, alpha=0.1) lo, hi = cp.interval({"x": x_te}) cp.covers(y_te, given={"x": x_te}).mean() # ~ 0.9