regressor.CQR
regressor.CQR(model_lower, model_upper, alpha=0.1, verbose=0, callbacks=None)Conformalized Quantile Regression.
CQR wraps two quantile regression models (lower and upper quantiles) and produces adaptive prediction intervals with coverage guarantees.
Unlike split conformal prediction which produces fixed-width intervals, CQR intervals adapt to the local uncertainty estimated by the quantile regression models.
Parameters
model_lower : object-
A fitted quantile regression model for the lower quantile. Must have a
predictmethod. model_upper : object-
A fitted quantile regression model for the upper quantile. Must have 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 conformity adjustment. Can be positive or negative. 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 GradientBoostingRegressor
>>> from conformalpy.regressor import CQR
>>>
>>> # Train quantile regression models
>>> model_lo = GradientBoostingRegressor(loss='quantile', alpha=0.05)
>>> model_hi = GradientBoostingRegressor(loss='quantile', alpha=0.95)
>>> model_lo.fit(X_train, y_train)
>>> model_hi.fit(X_train, y_train)
>>>
>>> # Create and calibrate CQR
>>> cqr = CQR(model_lower=model_lo, model_upper=model_hi, alpha=0.1)
>>> cqr.calibrate(X_calib, y_calib)
>>>
>>> # Get adaptive prediction intervals
>>> intervals = cqr.predict_interval(X_test)Notes
CQR uses the following nonconformity score:
\[ E_i = \max(\hat{q}_{\alpha/2}(X_i) - Y_i, Y_i - \hat{q}_{1-\alpha/2}(X_i)) \]
The conformity adjustment is:
\[ \hat{Q} = \text{Quantile}(E_1, ..., E_n; \frac{\lceil (n+1)(1-\alpha) \rceil}{n}) \]
Prediction intervals are:
\[ C(X) = [\hat{q}_{\alpha/2}(X) - \hat{Q}, \hat{q}_{1-\alpha/2}(X) + \hat{Q}] \]
References
- Romano, Y., Patterson, E., & Candes, E. (2019). Conformalized quantile regression. Advances in Neural Information Processing Systems, 32.
Methods
| Name | Description |
|---|---|
| calibrate | Calibrate CQR on held-out data. |
| fit | Alias for calibrate() for sklearn API compatibility. |
| get_qhat | Get the calibrated conformity adjustment. |
| predict | Return point predictions (midpoint of quantile predictions). |
| predict_interval | Predict adaptive intervals for new samples. |
| score | Compute coverage score on test data. |
calibrate
regressor.CQR.calibrate(X, y)Calibrate CQR on held-out data.
Computes nonconformity scores and the conformity adjustment 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 for each calibration sample.
fit
regressor.CQR.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 : CQR-
Returns self for method chaining.
get_qhat
regressor.CQR.get_qhat()Get the calibrated conformity adjustment.
Returns
qhat : float-
The calibrated adjustment for interval construction. Can be positive (intervals expand) or negative (intervals shrink).
Raises
: ValueError-
If called before calibration.
predict
regressor.CQR.predict(X)Return point predictions (midpoint of quantile predictions).
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 (midpoint of lower and upper quantiles).
predict_interval
regressor.CQR.predict_interval(X, *, alpha=None)Predict adaptive 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.CQR.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.