|
| 1 | +""" |
| 2 | +==================================== |
| 3 | +Plotting CQR with symmetric argument |
| 4 | +==================================== |
| 5 | +An example plot of :class:`~mapie.quantile_regression.MapieQuantileRegressor` |
| 6 | +illustrating the impact of the symmetry parameter. |
| 7 | +""" |
| 8 | +import numpy as np |
| 9 | +from matplotlib import pyplot as plt |
| 10 | +from sklearn.datasets import make_regression |
| 11 | +from sklearn.ensemble import GradientBoostingRegressor |
| 12 | + |
| 13 | +from mapie.metrics import regression_coverage_score |
| 14 | +from mapie.quantile_regression import MapieQuantileRegressor |
| 15 | + |
| 16 | +random_state = 2 |
| 17 | + |
| 18 | +############################################################################## |
| 19 | +# We generate a synthetic data. |
| 20 | + |
| 21 | +X, y = make_regression(n_samples=500, n_features=1, noise=20, random_state=59) |
| 22 | + |
| 23 | +# Define alpha level |
| 24 | +alpha = 0.2 |
| 25 | + |
| 26 | +# Fit a Gradient Boosting Regressor for quantile regression |
| 27 | +gb_reg = GradientBoostingRegressor( |
| 28 | + loss="quantile", alpha=0.5, random_state=random_state |
| 29 | +) |
| 30 | + |
| 31 | +# MAPIE Quantile Regressor |
| 32 | +mapie_qr = MapieQuantileRegressor(estimator=gb_reg, alpha=alpha) |
| 33 | +mapie_qr.fit(X, y, random_state=random_state) |
| 34 | +y_pred_sym, y_pis_sym = mapie_qr.predict(X, symmetry=True) |
| 35 | +y_pred_asym, y_pis_asym = mapie_qr.predict(X, symmetry=False) |
| 36 | +y_qlow = mapie_qr.estimators_[0].predict(X) |
| 37 | +y_qup = mapie_qr.estimators_[1].predict(X) |
| 38 | + |
| 39 | +# Calculate coverage scores |
| 40 | +coverage_score_sym = regression_coverage_score( |
| 41 | + y, y_pis_sym[:, 0], y_pis_sym[:, 1] |
| 42 | +) |
| 43 | +coverage_score_asym = regression_coverage_score( |
| 44 | + y, y_pis_asym[:, 0], y_pis_asym[:, 1] |
| 45 | +) |
| 46 | + |
| 47 | +# Sort the values for plotting |
| 48 | +order = np.argsort(X[:, 0]) |
| 49 | +X_sorted = X[order] |
| 50 | +y_pred_sym_sorted = y_pred_sym[order] |
| 51 | +y_pis_sym_sorted = y_pis_sym[order] |
| 52 | +y_pred_asym_sorted = y_pred_asym[order] |
| 53 | +y_pis_asym_sorted = y_pis_asym[order] |
| 54 | +y_qlow = y_qlow[order] |
| 55 | +y_qup = y_qup[order] |
| 56 | + |
| 57 | +############################################################################## |
| 58 | +# We will plot the predictions and prediction intervals for both symmetric |
| 59 | +# and asymmetric intervals. The line represents the predicted values, the |
| 60 | +# dashed lines represent the prediction intervals, and the shaded area |
| 61 | +# represents the symmetric and asymmetric prediction intervals. |
| 62 | + |
| 63 | +plt.figure(figsize=(14, 7)) |
| 64 | + |
| 65 | +plt.subplot(1, 2, 1) |
| 66 | +plt.xlabel("x") |
| 67 | +plt.ylabel("y") |
| 68 | +plt.scatter(X, y, alpha=0.3) |
| 69 | +plt.plot(X_sorted, y_qlow, color="C1") |
| 70 | +plt.plot(X_sorted, y_qup, color="C1") |
| 71 | +plt.plot(X_sorted, y_pis_sym_sorted[:, 0], color="C1", ls="--") |
| 72 | +plt.plot(X_sorted, y_pis_sym_sorted[:, 1], color="C1", ls="--") |
| 73 | +plt.fill_between( |
| 74 | + X_sorted.ravel(), |
| 75 | + y_pis_sym_sorted[:, 0].ravel(), |
| 76 | + y_pis_sym_sorted[:, 1].ravel(), |
| 77 | + alpha=0.2, |
| 78 | +) |
| 79 | +plt.title( |
| 80 | + f"Symmetric Intervals\n" |
| 81 | + f"Target and effective coverages for " |
| 82 | + f"alpha={alpha:.2f}: ({1-alpha:.3f}, {coverage_score_sym:.3f})" |
| 83 | +) |
| 84 | + |
| 85 | +# Plot asymmetric prediction intervals |
| 86 | +plt.subplot(1, 2, 2) |
| 87 | +plt.xlabel("x") |
| 88 | +plt.ylabel("y") |
| 89 | +plt.scatter(X, y, alpha=0.3) |
| 90 | +plt.plot(X_sorted, y_qlow, color="C2") |
| 91 | +plt.plot(X_sorted, y_qup, color="C2") |
| 92 | +plt.plot(X_sorted, y_pis_asym_sorted[:, 0], color="C2", ls="--") |
| 93 | +plt.plot(X_sorted, y_pis_asym_sorted[:, 1], color="C2", ls="--") |
| 94 | +plt.fill_between( |
| 95 | + X_sorted.ravel(), |
| 96 | + y_pis_asym_sorted[:, 0].ravel(), |
| 97 | + y_pis_asym_sorted[:, 1].ravel(), |
| 98 | + alpha=0.2, |
| 99 | +) |
| 100 | +plt.title( |
| 101 | + f"Asymmetric Intervals\n" |
| 102 | + f"Target and effective coverages for " |
| 103 | + f"alpha={alpha:.2f}: ({1-alpha:.3f}, {coverage_score_asym:.3f})" |
| 104 | +) |
| 105 | +plt.tight_layout() |
| 106 | +plt.show() |
| 107 | + |
| 108 | +############################################################################## |
| 109 | +# The symmetric intervals (`symmetry=True`) use a combined set of residuals |
| 110 | +# for both bounds, while the asymmetric intervals use distinct residuals for |
| 111 | +# each bound, allowing for more flexible and accurate intervals that reflect |
| 112 | +# the heteroscedastic nature of the data. The resulting effective coverages |
| 113 | +# demonstrate the theoretical guarantee of the target coverage level |
| 114 | +# :math:`1 - \alpha`. |
0 commit comments