.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples_regression/1-quickstart/plot_cqr_symmetry_difference.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_regression_1-quickstart_plot_cqr_symmetry_difference.py: ==================================== Plotting CQR with symmetric argument ==================================== An example plot of :class:`~mapie.quantile_regression.MapieQuantileRegressor` illustrating the impact of the symmetry parameter. .. GENERATED FROM PYTHON SOURCE LINES 8-18 .. code-block:: default import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import make_regression from sklearn.ensemble import GradientBoostingRegressor from mapie.metrics import regression_coverage_score from mapie.quantile_regression import MapieQuantileRegressor random_state = 2 .. GENERATED FROM PYTHON SOURCE LINES 19-20 We generate a synthetic data. .. GENERATED FROM PYTHON SOURCE LINES 20-57 .. code-block:: default X, y = make_regression(n_samples=500, n_features=1, noise=20, random_state=59) # Define alpha level alpha = 0.2 # Fit a Gradient Boosting Regressor for quantile regression gb_reg = GradientBoostingRegressor( loss="quantile", alpha=0.5, random_state=random_state ) # MAPIE Quantile Regressor mapie_qr = MapieQuantileRegressor(estimator=gb_reg, alpha=alpha) mapie_qr.fit(X, y, random_state=random_state) y_pred_sym, y_pis_sym = mapie_qr.predict(X, symmetry=True) y_pred_asym, y_pis_asym = mapie_qr.predict(X, symmetry=False) y_qlow = mapie_qr.estimators_[0].predict(X) y_qup = mapie_qr.estimators_[1].predict(X) # Calculate coverage scores coverage_score_sym = regression_coverage_score( y, y_pis_sym[:, 0], y_pis_sym[:, 1] ) coverage_score_asym = regression_coverage_score( y, y_pis_asym[:, 0], y_pis_asym[:, 1] ) # Sort the values for plotting order = np.argsort(X[:, 0]) X_sorted = X[order] y_pred_sym_sorted = y_pred_sym[order] y_pis_sym_sorted = y_pis_sym[order] y_pred_asym_sorted = y_pred_asym[order] y_pis_asym_sorted = y_pis_asym[order] y_qlow = y_qlow[order] y_qup = y_qup[order] .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/mapie/conda/v0.9.2/lib/python3.10/site-packages/sklearn/utils/deprecation.py:66: FutureWarning: Class MapieQuantileRegressor is deprecated; WARNING: Deprecated path to import MapieQuantileRegressor. Please prefer the new path: [from mapie.regression import MapieQuantileRegressor]. warnings.warn(msg, category=FutureWarning) INFO:root:The predictions are ill-sorted. INFO:root:The predictions are ill-sorted. INFO:root:The predictions are ill-sorted. .. GENERATED FROM PYTHON SOURCE LINES 58-62 We will plot the predictions and prediction intervals for both symmetric and asymmetric intervals. The line represents the predicted values, the dashed lines represent the prediction intervals, and the shaded area represents the symmetric and asymmetric prediction intervals. .. GENERATED FROM PYTHON SOURCE LINES 62-108 .. code-block:: default plt.figure(figsize=(14, 7)) plt.subplot(1, 2, 1) plt.xlabel("x") plt.ylabel("y") plt.scatter(X, y, alpha=0.3) plt.plot(X_sorted, y_qlow, color="C1") plt.plot(X_sorted, y_qup, color="C1") plt.plot(X_sorted, y_pis_sym_sorted[:, 0], color="C1", ls="--") plt.plot(X_sorted, y_pis_sym_sorted[:, 1], color="C1", ls="--") plt.fill_between( X_sorted.ravel(), y_pis_sym_sorted[:, 0].ravel(), y_pis_sym_sorted[:, 1].ravel(), alpha=0.2, ) plt.title( f"Symmetric Intervals\n" f"Target and effective coverages for " f"alpha={alpha:.2f}: ({1-alpha:.3f}, {coverage_score_sym:.3f})" ) # Plot asymmetric prediction intervals plt.subplot(1, 2, 2) plt.xlabel("x") plt.ylabel("y") plt.scatter(X, y, alpha=0.3) plt.plot(X_sorted, y_qlow, color="C2") plt.plot(X_sorted, y_qup, color="C2") plt.plot(X_sorted, y_pis_asym_sorted[:, 0], color="C2", ls="--") plt.plot(X_sorted, y_pis_asym_sorted[:, 1], color="C2", ls="--") plt.fill_between( X_sorted.ravel(), y_pis_asym_sorted[:, 0].ravel(), y_pis_asym_sorted[:, 1].ravel(), alpha=0.2, ) plt.title( f"Asymmetric Intervals\n" f"Target and effective coverages for " f"alpha={alpha:.2f}: ({1-alpha:.3f}, {coverage_score_asym:.3f})" ) plt.tight_layout() plt.show() .. image-sg:: /examples_regression/1-quickstart/images/sphx_glr_plot_cqr_symmetry_difference_001.png :alt: Symmetric Intervals Target and effective coverages for alpha=0.20: (0.800, 0.902), Asymmetric Intervals Target and effective coverages for alpha=0.20: (0.800, 0.868) :srcset: /examples_regression/1-quickstart/images/sphx_glr_plot_cqr_symmetry_difference_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 109-115 The symmetric intervals (`symmetry=True`) use a combined set of residuals for both bounds, while the asymmetric intervals use distinct residuals for each bound, allowing for more flexible and accurate intervals that reflect the heteroscedastic nature of the data. The resulting effective coverages demonstrate the theoretical guarantee of the target coverage level ``1 - α``. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.624 seconds) .. _sphx_glr_download_examples_regression_1-quickstart_plot_cqr_symmetry_difference.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_cqr_symmetry_difference.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_cqr_symmetry_difference.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_