Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Statistically validated hyperedges#
Frequent is not the same as significant: groups of very active vertices co-occur often by chance. Testing every observed group against the iterated hypergeometric null model (after Musciotto et al. 2021) keeps only the over-expressed hyperedges. A planted set of repeated groups among random noise is recovered.
import matplotlib.pyplot as plt
import numpy as np
import hatvis as hv
rng = np.random.default_rng(0)
nodes = [f"n{i}" for i in range(40)]
planted = [["n1", "n2", "n3"], ["n4", "n5", "n6"], ["n7", "n8"], ["n1", "n9", "n10", "n11"]]
edges = [g for g in planted for _ in range(6)]
edges += [list(rng.choice(nodes, size=rng.integers(2, 5), replace=False)) for _ in range(300)]
H = hv.Hypergraph(edges)
# [snippet:validate]
table = hv.patterns.validated_hyperedges(H, alpha=0.01, correction="bonferroni")
# [/snippet:validate]
print(table.head(8).to_string())
print("validated:", [sorted(s) for s in table.loc[table["validated"], "hyperedge"]])
hyperedge size multiplicity expected p_value tests validated
0 (n9, n11, n1, n10) 4 6 0.042161 8.711369e-14 91390 True
1 (n2, n3, n1) 3 6 0.111706 3.382625e-11 9880 True
2 (n4, n6, n5) 3 6 0.247472 1.339840e-08 9880 True
3 (n7, n8) 2 6 0.880435 9.025859e-06 741 True
4 (n14, n32, n9) 3 2 0.058166 1.206976e-03 9880 False
5 (n29, n37, n23) 3 2 0.071386 1.851325e-03 9880 False
6 (n37, n32, n34, n14) 4 1 0.002124 2.122291e-03 91390 False
7 (n25, n24, n14, n8) 4 1 0.002973 2.970453e-03 91390 False
validated: [['n1', 'n10', 'n11', 'n9'], ['n1', 'n2', 'n3'], ['n4', 'n5', 'n6'], ['n7', 'n8']]
fig, ax = plt.subplots(figsize=(6.5, 4))
ax.scatter(table["multiplicity"] + rng.normal(0, 0.05, len(table)), -np.log10(table["p_value"].clip(1e-300)),
c=np.where(table["validated"], hv.plotting.PALETTE[1], hv.plotting.GRAY), s=16)
ax.set_xlabel("multiplicity of the vertex set")
ax.set_ylabel(r"$-\log_{10}p$")
plt.tight_layout()

Total running time of the script: (0 minutes 0.380 seconds)