-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplots.py
More file actions
91 lines (80 loc) · 3.14 KB
/
plots.py
File metadata and controls
91 lines (80 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import matplotlib.pyplot as plt
import numpy as np
from inference import cond_ent
import os
# Plots posterior probabilities and conditional entropy per query.
def plot_query_results(results):
queries = [r["query"] for r in results]
posteriors = [r["posterior"] for r in results]
entropies = [r["entropy"] for r in results]
os.makedirs("results/plots", exist_ok=True)
x = range(len(queries))
plt.figure()
plt.bar(x, posteriors)
plt.xticks(x, queries, rotation=45, ha="right")
plt.ylabel("Posterior Probability")
plt.title("Posterior Probability per Query")
plt.tight_layout()
plt.savefig("results/plots/posterior_per_query.png", dpi=300)
plt.show()
plt.figure()
plt.bar(x, entropies)
plt.xticks(x, queries, rotation=45, ha="right")
plt.ylabel("Conditional Entropy (bits)")
plt.title("Conditional Entropy per Query")
plt.tight_layout()
plt.savefig("results/plots/cond_entropy_per_query.png", dpi=300)
plt.show()
# Visualizes posterior probability and entropy as a function of Bloom filter false positive rate.
def plot_bloom_metrics(fpr_range, prior=0.1):
os.makedirs("results/plots", exist_ok=True)
posteriors = [prior / (prior + f * (1 - prior)) for f in fpr_range]
entropies = [cond_ent(p) for p in posteriors]
fig, ax1 = plt.subplots(figsize=(10, 6))
ax1.set_xlabel("Bloom Filter FPR")
ax1.set_ylabel("Posterior Probability")
ax1.plot(fpr_range, posteriors)
ax1.grid(True)
ax2 = ax1.twinx()
ax2.set_ylabel("Conditional Entropy (bits)")
ax2.plot(fpr_range, entropies, linestyle="--")
plt.title(f"Bloom Filter Trustworthiness (Prior={prior})")
plt.tight_layout()
plt.savefig("results/plots/bloom_filter_trustworth.png", dpi=300)
plt.show()
def plot_mi_threshold_analysis(thresholds, fprs, entropies):
os.makedirs("results/experiments/plots", exist_ok=True)
# FPR vs MI threshold
plt.figure()
plt.plot(thresholds, fprs, marker='o')
plt.xscale('log')
plt.xlabel("Mutual Information Threshold")
plt.ylabel("Empirical False Positive Rate")
plt.title("Effect of MI Threshold on Empirical FPR")
plt.grid(True)
plt.tight_layout()
plt.savefig("results/plots/mi_threshold_vs_fpr.png", dpi=300)
plt.show()
# Entropy vs MI threshold
plt.figure()
plt.plot(thresholds, entropies, marker='o', linestyle='--')
plt.xscale('log')
plt.xlabel("Mutual Information Threshold")
plt.ylabel("Average Conditional Entropy (bits)")
plt.title("Effect of MI Threshold on Classification Uncertainty")
plt.grid(True)
plt.tight_layout()
plt.savefig("results/plots/mi_threshold_vs_entropy.png", dpi=300)
plt.show()
# Trade-off plot (examiner favorite)
plt.figure()
plt.scatter(fprs, entropies)
for i, t in enumerate(thresholds):
plt.annotate(f"MI={t}", (fprs[i], entropies[i]))
plt.xlabel("Empirical False Positive Rate")
plt.ylabel("Average Conditional Entropy (bits)")
plt.title("Trade-off Between False Positives and Uncertainty")
plt.grid(True)
plt.tight_layout()
plt.savefig("results/plots/fpr_vs_entropy_tradeoff.png", dpi=300)
plt.show()