regressor.ConformalPredictiveDistribution
regressor.ConformalPredictiveDistribution(model, alpha=0.1)Conformal Predictive Distribution for regression.
Unlike standard conformal regression which produces intervals, CPD outputs a full predictive distribution (CDF) for each test point. This provides complete uncertainty quantification.
Parameters
model : object-
A fitted regression model with a
predictmethod. alpha : float = 0.1-
Default miscoverage level for prediction intervals.
Attributes
qhat_ : float-
Symmetric split-conformal threshold computed from
\|calibration_residuals_\|for the defaultalpha. This is the half-width of the symmetric interval[ŷ − qhat_, ŷ + qhat_]; it is provided as a convenience for downstream code that expects the standard split-conformal API. It is not used bypredict_distribution()/predict_interval(), which build asymmetric intervals from the full step-function CDF of the signed residuals. calibration_residuals_ : ndarray-
Signed residuals from calibration:
r_i = y_i − ŷ(x_i). Stored signed (not absolute) so that the predictive CDF can capture asymmetric error distributions. n_calib_ : int-
Number of calibration samples.
Examples
>>> from sklearn.ensemble import RandomForestRegressor
>>> from conformalpy.regressor import ConformalPredictiveDistribution
>>>
>>> # Train base model
>>> model = RandomForestRegressor().fit(X_train, y_train)
>>>
>>> # Create CPD
>>> cpd = ConformalPredictiveDistribution(model=model, alpha=0.1)
>>> cpd.calibrate(X_calib, y_calib)
>>>
>>> # Get predictive distribution for a test point
>>> dist = cpd.predict_distribution(X_test[0:1])[0]
>>>
>>> # Evaluate CDF at specific value
>>> prob = dist.cdf(100.0) # P(Y <= 100)
>>>
>>> # Get quantiles
>>> median = dist.quantile(0.5)
>>> q95 = dist.quantile(0.95)
>>>
>>> # Get prediction interval
>>> lower, upper = dist.interval(alpha=0.1)Notes
The conformal predictive distribution at test point x is defined by:
\[ F(y | x) = \frac{|\{i : r_i \leq y - \hat{y}(x)\}| + 1}{n + 1} \]
where \(r_i = y_i - \hat{y}(x_i)\) are the calibration residuals.
This gives a step-function CDF that can be evaluated at any y.
References
Vovk, V., Shen, J., Manokhin, V., & Xie, M. (2017). “Nonparametric Predictive Distributions Based on Conformal Prediction.” Machine Learning, 106(9), 1557–1576.
Vovk, V., Gammerman, A., & Shafer, G. (2005). Algorithmic Learning in a Random World. Springer, Chapter 6.
Methods
| Name | Description |
|---|---|
| calibrate | Calibrate the conformal predictive distribution. |
| fit | Alias for calibrate() for sklearn API compatibility. |
| get_qhat | Get the calibrated quantile threshold. |
| predict | Return point predictions from the base model. |
| predict_cdf | Evaluate CDF at specified y values for all test samples. |
| predict_distribution | Compute predictive distributions for test samples. |
| predict_interval | Compute prediction intervals from the predictive distribution. |
| predict_quantile | Compute quantiles of the predictive distributions. |
| score | Compute coverage on test data. |
calibrate
regressor.ConformalPredictiveDistribution.calibrate(X, y)Calibrate the conformal predictive distribution.
Parameters
X : array-like of shape (n_samples, n_features)-
Calibration features.
y : array-like of shape (n_samples,)-
True target values.
Returns
residuals : ndarray-
Signed residuals from calibration.
fit
regressor.ConformalPredictiveDistribution.fit(X, y)Alias for calibrate() for sklearn API compatibility.
Parameters
X : array-like of shape (n_samples, n_features)-
Calibration features.
y : array-like of shape (n_samples,)-
True target values.
Returns
: self
get_qhat
regressor.ConformalPredictiveDistribution.get_qhat()Get the calibrated quantile threshold.
predict
regressor.ConformalPredictiveDistribution.predict(X)Return point predictions from the base model.
Parameters
X : array-like of shape (n_samples, n_features)-
Test features.
Returns
: ndarray of shape (n_samples,)-
Point predictions.
predict_cdf
regressor.ConformalPredictiveDistribution.predict_cdf(X, y_values)Evaluate CDF at specified y values for all test samples.
Parameters
X : array-like of shape (n_samples, n_features)-
Test features.
y_values : array-like of shape (n_values,)-
Y values at which to evaluate the CDF.
Returns
: ndarray of shape (n_samples, n_values)-
CDF values for each sample and y value.
predict_distribution
regressor.ConformalPredictiveDistribution.predict_distribution(X)Compute predictive distributions for test samples.
Parameters
X : array-like of shape (n_samples, n_features)-
Test features.
Returns
: list of PredictiveDistribution-
Predictive distribution objects for each test sample.
predict_interval
regressor.ConformalPredictiveDistribution.predict_interval(X, *, alpha=None)Compute prediction intervals from the predictive distribution.
Parameters
X : array-like of shape (n_samples, n_features)-
Test features.
alpha : float, list of float, or None = None-
Miscoverage level(s). If None, uses self.alpha.
Returns
: ndarray-
If alpha is scalar: shape (n_samples, 2). If alpha is array: shape (n_samples, 2, n_alphas).
predict_quantile
regressor.ConformalPredictiveDistribution.predict_quantile(X, tau)Compute quantiles of the predictive distributions.
Parameters
X : array-like of shape (n_samples, n_features)-
Test features.
tau : float or array-like of shape (n_quantiles,)-
Quantile level(s) in [0, 1].
Returns
: ndarray-
If tau is scalar: shape (n_samples,). If tau is array: shape (n_samples, n_quantiles).
score
regressor.ConformalPredictiveDistribution.score(X, y)Compute coverage on test data.
Parameters
X : array-like of shape (n_samples, n_features)-
Test features.
y : array-like of shape (n_samples,)-
True target values.
Returns
: float-
Proportion of samples covered by the default (1-alpha) interval.