-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_biases.py
174 lines (151 loc) · 5.54 KB
/
test_biases.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from other_indices import Indices
from random_partition import RandomSetPartition
from mpmath import stirling2
from clustering_comparison_measures import (
standardized_rand_score,
standardized_mutual_info,
)
from other_indices.ConstantBaselineTests import check_constant_baseline
from other_indices.ValidationIndices import Score
from other_indices.Clustering import Clustering
from fractions import Fraction
from tqdm import tqdm
import warnings
warnings.filterwarnings("ignore")
class StandardizedMutualInformation(Score):
@classmethod
def score(cls, A, B):
A, B = (Clustering.FromAnything(C) for C in [A, B])
return standardized_mutual_info(A, B)
class StandardizedRandIndex(Score):
@classmethod
def score(cls, A, B):
A, B = (Clustering.FromAnything(C) for C in [A, B])
return standardized_rand_score(A, B)
def generate_k_set_partitions(ns, k):
"""Generate all k-set partitions of the set ns.
This algorithm is described in Donald Knuth, Computer Programming,
Volume 4, Fascicle 3B and the implementation is adapted from
https://codereview.stackexchange.com/questions/1526/finding-all-k-subset-partitions.
Args:
ns: list of integers
k: number of parts
"""
def visit(n, a):
ps = [[] for i in range(k)]
for j in range(n):
ps[a[j + 1]].append(ns[j])
return ps
def f(mu, nu, sigma, n, a):
if mu == 2:
yield visit(n, a)
else:
for v in f(mu - 1, nu - 1, (mu + sigma) % 2, n, a):
yield v
if nu == mu + 1:
a[mu] = mu - 1
yield visit(n, a)
while a[nu] > 0:
a[nu] = a[nu] - 1
yield visit(n, a)
elif nu > mu + 1:
if (mu + sigma) % 2 == 1:
a[nu - 1] = mu - 1
else:
a[mu] = mu - 1
if (a[nu] + sigma) % 2 == 1:
for v in b(mu, nu - 1, 0, n, a):
yield v
else:
for v in f(mu, nu - 1, 0, n, a):
yield v
while a[nu] > 0:
a[nu] = a[nu] - 1
if (a[nu] + sigma) % 2 == 1:
for v in b(mu, nu - 1, 0, n, a):
yield v
else:
for v in f(mu, nu - 1, 0, n, a):
yield v
def b(mu, nu, sigma, n, a):
if nu == mu + 1:
while a[nu] < mu - 1:
yield visit(n, a)
a[nu] = a[nu] + 1
yield visit(n, a)
a[mu] = 0
elif nu > mu + 1:
if (a[nu] + sigma) % 2 == 1:
for v in f(mu, nu - 1, 0, n, a):
yield v
else:
for v in b(mu, nu - 1, 0, n, a):
yield v
while a[nu] < mu - 1:
a[nu] = a[nu] + 1
if (a[nu] + sigma) % 2 == 1:
for v in f(mu, nu - 1, 0, n, a):
yield v
else:
for v in b(mu, nu - 1, 0, n, a):
yield v
if (mu + sigma) % 2 == 1:
a[nu - 1] = 0
else:
a[mu] = 0
if mu == 2:
yield visit(n, a)
else:
for v in b(mu - 1, nu - 1, (mu + sigma) % 2, n, a):
yield v
n = len(ns)
a = [0] * (n + 1)
for j in range(1, k + 1):
a[n - k + j] = j - 1
return f(k, n, 0, n, a)
AllIndices = Indices + [StandardizedMutualInformation, StandardizedRandIndex]
results = {index.__name__: 0 for index in AllIndices}
if __name__ == "__main__":
tqdm.write("Type I biased:")
p = tqdm(AllIndices, total=len(AllIndices), desc="Testing Type I bias")
for index in p:
p.set_description(f"Testing {index.__name__} for Type I bias")
# Choose n=20,30,40
ns = range(20, 51, 10)
# For each n, we consider balanced cluster sizes with k=sqrt(n) clusters.
n2gtk = {n: int(n**0.5) for n in ns}
# For each n, we consider candidates with balanced cluster sizes with
# k1=n^0.25, k2=n^0.5, k3=n^0.75.
n2ks = {n: [int(n**0.25), int(n**0.5), int(n**0.75)] for n in ns}
result = check_constant_baseline(
I=index, n2ks=n2ks, n2gtk=n2gtk, repeats=500, aggregate=True
)
if result["constant baseline p"] < 1e-6:
tqdm.write(f" - {index.__name__}")
p.close()
tqdm.write("Checking Type II bias with the following parameters:")
# Type II bias
n = 4
k1 = 2
k2 = 3
random_partition = RandomSetPartition(seed=42)
a = [int(i) for i in random_partition.random_partition(n, k=2)]
tqdm.write(f"n={n}, k1={k1}, k2={k2}")
tqdm.write(f"A = {a}")
for b in tqdm(
generate_k_set_partitions(list(range(n)), k1),
total=int(stirling2(n, k1)),
desc="Testing Type II bias",
):
for b_prime in generate_k_set_partitions(list(range(n)), k2):
for index in AllIndices:
val = index.score(a, b)
val_prime = index.score(a, b_prime)
results[index.__name__] += int(val > val_prime) - int(val < val_prime)
total = int(stirling2(n, k1) * stirling2(n, k2))
tqdm.write("\nType II biased:")
for key, value in results.items():
frac = Fraction(results[key] + total, 2 * total)
numerator, denominator = frac.as_integer_ratio()
if (numerator, denominator) != (1, 2):
tqdm.write(f"- {key}\tE = {numerator} / {denominator}")