Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,56 @@ def denormalize_image(tensor_image: torch.Tensor) -> np.ndarray:
# target precision level, illustrating the effectiveness of MAPIE’s
# risk control for semantic segmentation tasks.
#

###############################################################################
# Bootstrap the mean precision over different samplings of the test set
# (resampling images with replacement).
#

N_BOOTSTRAP = 2000
BOOTSTRAP_SEED = 123
rng = np.random.default_rng(BOOTSTRAP_SEED)

bootstrap_means = np.empty(N_BOOTSTRAP, dtype=float)
n = precisions_array.size
for b in range(N_BOOTSTRAP):
bootstrap_sample = rng.choice(precisions_array, size=n, replace=True)
bootstrap_means[b] = bootstrap_sample.mean()

delta = round(1 - CONFIDENCE_LEVEL, 2)
quantile_confidence = np.quantile(bootstrap_means, delta)
print(f"Bootstrap {delta}-th quantile: {quantile_confidence:.4f}")

fig, ax = plt.subplots(figsize=(10, 6))
ax.hist(
bootstrap_means,
bins=40,
alpha=0.7,
color="slateblue",
edgecolor="black",
)
ax.axvline(
precisions_array.mean(),
color="orange",
linestyle="--",
linewidth=2,
label=f"Test mean ({precisions_array.mean():.3f})",
)
ax.axvline(
quantile_confidence,
color="green",
linestyle="--",
linewidth=2,
label=f"Bootstrap {delta}-th quantile ({quantile_confidence:.3f})",
)
ax.set_xlabel("Bootstrap mean precision", fontsize=12)
ax.set_ylabel("Frequency", fontsize=12)
ax.set_title(
"Bootstrap distribution of mean precision (test set resampling)",
fontsize=14,
fontweight="bold",
)
ax.legend(fontsize=10)
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()