regressor.ConformalRegressor
regressor.ConformalRegressor(model, alpha=0.1, verbose=0, callbacks=None)Conformal regressor using split (inductive) conformal prediction.
Wraps a fitted regression model and provides prediction intervals with finite-sample coverage guarantees.
Parameters
model : object-
A fitted regression model with a
predictmethod. alpha : float = 0.1-
Miscoverage level. The prediction intervals will contain the true value with probability at least 1 - alpha. Must be in (0, 1).
Attributes
qhat_ : float-
The calibrated quantile threshold for interval construction. Set after calling
calibrate(). calibration_scores_ : np.ndarray-
The nonconformity scores from calibration data. Set after calling
calibrate(). n_calib_ : int-
Number of calibration samples used. Set after calling
calibrate().
Examples
>>> from sklearn.ensemble import RandomForestRegressor
>>> from conformalpy.regressor import ConformalRegressor
>>>
>>> # Train base model
>>> model = RandomForestRegressor().fit(X_train, y_train)
>>>
>>> # Create and calibrate conformal regressor
>>> cr = ConformalRegressor(model=model, alpha=0.1)
>>> cr.calibrate(X_calib, y_calib)
>>>
>>> # Get prediction intervals
>>> intervals = cr.predict_interval(X_test)
>>> # intervals[:, 0] = lower bounds
>>> # intervals[:, 1] = upper boundsNotes
The split conformal method uses absolute residuals as nonconformity scores:
\[ s_i = |y_i - \hat{y}_i| \]
The quantile threshold is computed as:
\[ \hat{q} = \text{Quantile}\left(s_1, ..., s_n; \frac{\lceil (n+1)(1-\alpha) \rceil}{n}\right) \]
Prediction intervals are then:
\[ C(x) = [\hat{y}(x) - \hat{q}, \hat{y}(x) + \hat{q}] \]
References
- Vovk, V., Gammerman, A., & Shafer, G. (2005). Algorithmic Learning in a Random World. Springer.
- Lei, J., G’Sell, M., Rinaldo, A., Tibshirani, R. J., & Wasserman, L. (2018). Distribution-free predictive inference for regression. Journal of the American Statistical Association, 113(523), 1094-1111.
Methods
| Name | Description |
|---|---|
| calibrate | Calibrate the conformal regressor on held-out data. |
| fit | Alias for calibrate() for sklearn API compatibility. |
| get_qhat | Get the calibrated quantile threshold. |
| predict | Return point predictions from the base model. |
| predict_interval | Predict intervals for new samples. |
| score | Compute coverage score on test data. |
calibrate
regressor.ConformalRegressor.calibrate(X, y)Calibrate the conformal regressor on held-out data.
Computes nonconformity scores (absolute residuals) and the quantile threshold for constructing prediction intervals.
Parameters
X : array-like of shape (n_samples, n_features)-
Calibration features.
y : array-like of shape (n_samples,)-
True target values for calibration.
Returns
scores : np.ndarray of shape (n_samples,)-
Nonconformity scores (absolute residuals) for each calibration sample.
fit
regressor.ConformalRegressor.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 for calibration.
Returns
self : ConformalRegressor-
Returns self for method chaining.
get_qhat
regressor.ConformalRegressor.get_qhat()Get the calibrated quantile threshold.
Returns
qhat : float-
The calibrated threshold for interval construction.
Raises
: ValueError-
If called before calibration.
predict
regressor.ConformalRegressor.predict(X)Return point predictions from the base model.
Parameters
X : array-like of shape (n_samples, n_features)-
Features of samples to predict.
Returns
predictions : np.ndarray of shape (n_samples,)-
Point predictions from the base model.
predict_interval
regressor.ConformalRegressor.predict_interval(X, *, alpha=None)Predict intervals for new samples.
Parameters
X : array-like of shape (n_samples, n_features)-
Features of samples to predict.
alpha : float, list of float, or None = None-
Significance level(s) for prediction. If None, uses self.alpha from init. If scalar, returns (n_samples, 2). If array-like, returns (n_samples, 2, n_alphas).
Returns
intervals : np.ndarray-
If alpha is None or scalar: shape (n_samples, 2). If alpha is array: shape (n_samples, 2, n_alphas). Column 0 is lower bound, column 1 is upper bound.
Raises
: ValueError-
If called before calibration.
score
regressor.ConformalRegressor.score(X, y)Compute coverage score 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
coverage : float-
Proportion of samples where true value falls within the prediction interval.