regressor.NormalizedConformalRegressor
regressor.NormalizedConformalRegressor(
model,
sigma_model,
alpha=0.1,
sigma_min=1e-06,
)Normalized Conformal Regressor with adaptive intervals.
Scales residuals by a local uncertainty estimate (sigma) to produce adaptive prediction intervals that are narrower where the model is confident and wider where it is uncertain.
Parameters
model : object-
A fitted regression model for point predictions. Must have a
predictmethod. sigma_model : object-
A fitted model that predicts local scale/uncertainty. Must have a
predictmethod returning positive values. Typically trained to predict absolute residuals or MAD. 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).
sigma_min : float = 1e-6-
Minimum value for sigma to avoid division by zero.
Attributes
qhat_ : float-
The calibrated quantile threshold for normalized scores. Set after calling
calibrate(). calibration_scores_ : np.ndarray-
The normalized 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 NormalizedConformalRegressor
>>>
>>> # Train main model
>>> model = RandomForestRegressor().fit(X_train, y_train)
>>>
>>> # Train sigma model on absolute residuals
>>> train_preds = model.predict(X_train)
>>> abs_residuals = np.abs(y_train - train_preds)
>>> sigma_model = RandomForestRegressor().fit(X_train, abs_residuals)
>>>
>>> # Create and calibrate
>>> ncr = NormalizedConformalRegressor(
... model=model, sigma_model=sigma_model, alpha=0.1
... )
>>> ncr.calibrate(X_calib, y_calib)
>>>
>>> # Get adaptive intervals
>>> intervals = ncr.predict_interval(X_test)Notes
The normalized nonconformity score is:
\[ R_i = \frac{|Y_i - \hat{\mu}(X_i)|}{\hat{\sigma}(X_i)} \]
The calibrated quantile is:
\[ \hat{q} = \text{Quantile}(R_1, ..., R_n; \frac{\lceil (n+1)(1-\alpha) \rceil}{n}) \]
Prediction intervals are:
\[ C(X) = [\hat{\mu}(X) - \hat{q} \cdot \hat{\sigma}(X), \hat{\mu}(X) + \hat{q} \cdot \hat{\sigma}(X)] \]
References
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.
Papadopoulos, H. & Haralambous, H. (2011). Reliable prediction intervals with regression neural networks. Neural Networks, 24(8), 842–851.
Methods
| Name | Description |
|---|---|
| calibrate | Calibrate 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 adaptive intervals for new samples. |
| score | Compute coverage score on test data. |
calibrate
regressor.NormalizedConformalRegressor.calibrate(X, y)Calibrate on held-out data.
Computes normalized nonconformity scores and the quantile threshold.
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,)-
Normalized nonconformity scores for each calibration sample.
fit
regressor.NormalizedConformalRegressor.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 : NormalizedConformalRegressor-
Returns self for method chaining.
get_qhat
regressor.NormalizedConformalRegressor.get_qhat()Get the calibrated quantile threshold.
Returns
qhat : float-
The calibrated threshold for normalized scores.
Raises
: ValueError-
If called before calibration.
predict
regressor.NormalizedConformalRegressor.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.NormalizedConformalRegressor.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.NormalizedConformalRegressor.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.