Skip to content

Commit 051d30c

Browse files
committed
chore: Update plot_cqr_symmetry_difference.py in regression examples
1 parent 501bade commit 051d30c

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

examples/regression/1-quickstart/plot_cqr_symmetry_difference.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
from mapie.metrics import regression_coverage_score
1414
from mapie.quantile_regression import MapieQuantileRegressor
1515

16-
# Generate synthetic data
16+
##############################################################################
17+
# We generate a synthetic data.
18+
1719
X, y = make_regression(n_samples=500, n_features=1, noise=20, random_state=59)
1820

1921
# Define alpha level
@@ -36,10 +38,10 @@
3638

3739
# Calculate coverage scores
3840
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]
4042
)
4143
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]
4345
)
4446

4547
# Sort the values for plotting
@@ -50,7 +52,12 @@
5052
y_pred_asym_sorted = y_pred_asym[order]
5153
y_pis_asym_sorted = y_pis_asym[order]
5254

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+
5461
plt.figure(figsize=(14, 7))
5562

5663
plt.subplot(1, 2, 1)
@@ -61,15 +68,15 @@
6168
plt.plot(X_sorted, y_pis_sym_sorted[:, 0], color="C1", ls="--")
6269
plt.plot(X_sorted, y_pis_sym_sorted[:, 1], color="C1", ls="--")
6370
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,
6875
)
6976
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})"
7380
)
7481

7582
# Plot asymmetric prediction intervals
@@ -81,24 +88,21 @@
8188
plt.plot(X_sorted, y_pis_asym_sorted[:, 0], color="C2", ls="--")
8289
plt.plot(X_sorted, y_pis_asym_sorted[:, 1], color="C2", ls="--")
8390
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,
8895
)
8996
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})"
93100
)
94-
95101
plt.tight_layout()
96102
plt.show()
97103

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

Comments
 (0)