|
13 | 13 | from mapie.metrics import regression_coverage_score |
14 | 14 | from mapie.quantile_regression import MapieQuantileRegressor |
15 | 15 |
|
16 | | -# Generate synthetic data |
| 16 | +############################################################################## |
| 17 | +# We generate a synthetic data. |
| 18 | + |
17 | 19 | X, y = make_regression(n_samples=500, n_features=1, noise=20, random_state=59) |
18 | 20 |
|
19 | 21 | # Define alpha level |
|
36 | 38 |
|
37 | 39 | # Calculate coverage scores |
38 | 40 | coverage_score_sym = regression_coverage_score( |
39 | | - y, y_pis_sym[:, 0], y_pis_sym[:, 1] |
| 41 | +y, y_pis_sym[:, 0], y_pis_sym[:, 1] |
40 | 42 | ) |
41 | 43 | coverage_score_asym = regression_coverage_score( |
42 | | - y, y_pis_asym[:, 0], y_pis_asym[:, 1] |
| 44 | +y, y_pis_asym[:, 0], y_pis_asym[:, 1] |
43 | 45 | ) |
44 | 46 |
|
45 | 47 | # Sort the values for plotting |
|
50 | 52 | y_pred_asym_sorted = y_pred_asym[order] |
51 | 53 | y_pis_asym_sorted = y_pis_asym[order] |
52 | 54 |
|
53 | | -# Plot symmetric prediction intervals |
| 55 | +############################################################################## |
| 56 | +# We will plot the predictions and prediction intervals for both symmetric |
| 57 | +# and asymmetric intervals. The line represents the predicted values, the |
| 58 | +# dashed lines represent the prediction intervals, and the shaded area |
| 59 | +# represents the symmetric and asymmetric prediction intervals. |
| 60 | + |
54 | 61 | plt.figure(figsize=(14, 7)) |
55 | 62 |
|
56 | 63 | plt.subplot(1, 2, 1) |
|
61 | 68 | plt.plot(X_sorted, y_pis_sym_sorted[:, 0], color="C1", ls="--") |
62 | 69 | plt.plot(X_sorted, y_pis_sym_sorted[:, 1], color="C1", ls="--") |
63 | 70 | plt.fill_between( |
64 | | - X_sorted.ravel(), |
65 | | - y_pis_sym_sorted[:, 0].ravel(), |
66 | | - y_pis_sym_sorted[:, 1].ravel(), |
67 | | - alpha=0.2, |
| 71 | +X_sorted.ravel(), |
| 72 | +y_pis_sym_sorted[:, 0].ravel(), |
| 73 | +y_pis_sym_sorted[:, 1].ravel(), |
| 74 | +alpha=0.2, |
68 | 75 | ) |
69 | 76 | plt.title( |
70 | | - f"Symmetric Intervals\n" |
71 | | - f"Target and effective coverages for " |
72 | | - f"alpha={alpha:.2f}: ({1-alpha:.3f}, {coverage_score_sym:.3f})" |
| 77 | +f"Symmetric Intervals\n" |
| 78 | +f"Target and effective coverages for " |
| 79 | +f"alpha={alpha:.2f}: ({1-alpha:.3f}, {coverage_score_sym:.3f})" |
73 | 80 | ) |
74 | 81 |
|
75 | 82 | # Plot asymmetric prediction intervals |
|
81 | 88 | plt.plot(X_sorted, y_pis_asym_sorted[:, 0], color="C2", ls="--") |
82 | 89 | plt.plot(X_sorted, y_pis_asym_sorted[:, 1], color="C2", ls="--") |
83 | 90 | plt.fill_between( |
84 | | - X_sorted.ravel(), |
85 | | - y_pis_asym_sorted[:, 0].ravel(), |
86 | | - y_pis_asym_sorted[:, 1].ravel(), |
87 | | - alpha=0.2, |
| 91 | +X_sorted.ravel(), |
| 92 | +y_pis_asym_sorted[:, 0].ravel(), |
| 93 | +y_pis_asym_sorted[:, 1].ravel(), |
| 94 | +alpha=0.2, |
88 | 95 | ) |
89 | 96 | plt.title( |
90 | | - f"Asymmetric Intervals\n" |
91 | | - f"Target and effective coverages for " |
92 | | - f"alpha={alpha:.2f}: ({1-alpha:.3f}, {coverage_score_asym:.3f})" |
| 97 | +f"Asymmetric Intervals\n" |
| 98 | +f"Target and effective coverages for " |
| 99 | +f"alpha={alpha:.2f}: ({1-alpha:.3f}, {coverage_score_asym:.3f})" |
93 | 100 | ) |
94 | | - |
95 | 101 | plt.tight_layout() |
96 | 102 | plt.show() |
97 | 103 |
|
98 | | -# Explanation of the results |
99 | | -""" |
100 | | -The symmetric intervals (`symmetry=True`) are easier to interpret and |
101 | | -tend to have higher coverage but might not adapt well to varying |
102 | | -noise levels. The asymmetric intervals (`symmetry=False`) are more |
103 | | -flexible and better capture heteroscedasticity but can appear more jagged. |
104 | | -""" |
| 104 | +############################################################################## |
| 105 | +# The symmetric intervals (`symmetry=True`) are easier to interpret and |
| 106 | +# tend to have higher coverage but might not adapt well to varying |
| 107 | +# noise levels. The asymmetric intervals (`symmetry=False`) are more |
| 108 | +# flexible and better capture heteroscedasticity but can appear more jagged. |
0 commit comments