-
Notifications
You must be signed in to change notification settings - Fork 1
/
weat.py
260 lines (205 loc) · 7.96 KB
/
weat.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Adapted from https://github.com/McGill-NLP/bias-bench
import itertools
import math
import numpy as np
import scipy.special
import scipy.stats
# X and Y are two sets of target words of equal size.
# A and B are two sets of attribute words.
def cossim(x, y):
return np.dot(x, y) / math.sqrt(np.dot(x, x) * np.dot(y, y))
def construct_cossim_lookup(XY, AB):
"""Args:
XY: Mapping from target string to target vector (either in X or Y).
AB: Mapping from attribute string to attribute vectore (either in A or B).
Returns:
An array of size (len(XY), len(AB)) containing cosine similarities
between items in XY and items in AB.
"""
cossims = np.zeros((len(XY), len(AB)))
for xy in XY:
for ab in AB:
cossims[xy, ab] = cossim(XY[xy], AB[ab])
return cossims
def s_wAB(A, B, cossims):
"""Returns:
Vector of s(w, A, B) across w, where
s(w, A, B) = mean_{a in A} cos(w, a) - mean_{b in B} cos(w, b).
"""
return cossims[:, A].mean(axis=1) - cossims[:, B].mean(axis=1)
def s_XAB(X, s_wAB_memo):
r"""Given indices of target concept X and precomputed s_wAB values,
return slightly more computationally efficient version of WEAT
statistic for p-value computation.
Caliskan defines the WEAT statistic s(X, Y, A, B) as
sum_{x in X} s(x, A, B) - sum_{y in Y} s(y, A, B)
where s(w, A, B) is defined as
mean_{a in A} cos(w, a) - mean_{b in B} cos(w, b).
The p-value is computed using a permutation test on (X, Y) over all
partitions (X', Y') of X union Y with |X'| = |Y'|.
However, for all partitions (X', Y') of X union Y,
s(X', Y', A, B)
= sum_{x in X'} s(x, A, B) + sum_{y in Y'} s(y, A, B)
= C,
a constant. Thus
sum_{x in X'} s(x, A, B) + sum_{y in Y'} s(y, A, B)
= sum_{x in X'} s(x, A, B) + (C - sum_{x in X'} s(x, A, B))
= C + 2 sum_{x in X'} s(x, A, B).
By monotonicity,
s(X', Y', A, B) > s(X, Y, A, B)
if and only if
[s(X', Y', A, B) - C] / 2 > [s(X, Y, A, B) - C] / 2,
that is,
sum_{x in X'} s(x, A, B) > sum_{x in X} s(x, A, B).
Thus we only need use the first component of s(X, Y, A, B) as our
test statistic.
"""
return s_wAB_memo[X].sum()
def s_XYAB(X, Y, s_wAB_memo):
r"""Given indices of target concept X and precomputed s_wAB values,
the WEAT test statistic for p-value computation.
"""
return s_XAB(X, s_wAB_memo) - s_XAB(Y, s_wAB_memo)
def p_val_permutation_test(X, Y, A, B, n_samples, cossims, parametric=False):
"""Compute the p-val for the permutation test, which is defined as
the probability that a random even partition X_i, Y_i of X u Y
satisfies P[s(X_i, Y_i, A, B) > s(X, Y, A, B)]
"""
X = np.array(list(X), dtype=int)
Y = np.array(list(Y), dtype=int)
A = np.array(list(A), dtype=int)
B = np.array(list(B), dtype=int)
assert len(X) == len(Y)
size = len(X)
s_wAB_memo = s_wAB(A, B, cossims=cossims)
XY = np.concatenate((X, Y))
if parametric:
s = s_XYAB(X, Y, s_wAB_memo)
samples = []
for _ in range(n_samples):
np.random.shuffle(XY)
Xi = XY[:size]
Yi = XY[size:]
assert len(Xi) == len(Yi)
si = s_XYAB(Xi, Yi, s_wAB_memo)
samples.append(si)
# Compute sample standard deviation and compute p-value by
# assuming normality of null distribution
(shapiro_test_stat, shapiro_p_val) = scipy.stats.shapiro(samples)
sample_mean = np.mean(samples)
sample_std = np.std(samples, ddof=1)
p_val = scipy.stats.norm.sf(s, loc=sample_mean, scale=sample_std)
return p_val
else:
s = s_XAB(X, s_wAB_memo)
total_true = 0
total_equal = 0
total = 0
num_partitions = int(scipy.special.binom(2 * len(X), len(X)))
if num_partitions > n_samples:
# We only have as much precision as the number of samples drawn;
# bias the p-value (hallucinate a positive observation) to
# reflect that.
total_true += 1
total += 1
for _ in range(n_samples - 1):
np.random.shuffle(XY)
Xi = XY[:size]
assert 2 * len(Xi) == len(XY)
si = s_XAB(Xi, s_wAB_memo)
if si > s:
total_true += 1
elif si == s: # use conservative test
total_true += 1
total_equal += 1
total += 1
else:
for Xi in itertools.combinations(XY, len(X)):
Xi = np.array(Xi, dtype=int)
assert 2 * len(Xi) == len(XY)
si = s_XAB(Xi, s_wAB_memo)
if si > s:
total_true += 1
elif si == s: # use conservative test
total_true += 1
total_equal += 1
total += 1
# if total_equal:
# print("Equalities contributed {}/{} to p-value".format(total_equal, total))
return total_true / total
def mean_s_wAB(X, A, B, cossims):
return np.mean(s_wAB(A, B, cossims[X]))
def stdev_s_wAB(X, A, B, cossims):
return np.std(s_wAB(A, B, cossims[X]), ddof=1)
def effect_size(X, Y, A, B, cossims):
"""Compute the effect size, which is defined as
[mean_{x in X} s(x, A, B) - mean_{y in Y} s(y, A, B)] /
[ stddev_{w in X u Y} s(w, A, B) ]
Args:
X, Y, A, B : sets of target (X, Y) and attribute (A, B) indices
"""
X = list(X)
Y = list(Y)
A = list(A)
B = list(B)
numerator = mean_s_wAB(X, A, B, cossims=cossims) - mean_s_wAB(
Y, A, B, cossims=cossims
)
denominator = stdev_s_wAB(X + Y, A, B, cossims=cossims)
return numerator / denominator
def convert_keys_to_ints(X, Y):
return (
dict((i, v) for (i, (k, v)) in enumerate(X.items())),
dict((i + len(X), v) for (i, (k, v)) in enumerate(Y.items())),
)
def run_test(encs, n_samples, parametric=False):
"""Run a WEAT.
Args:
encs (Dict[str: Dict]): dictionary mapping targ1, targ2, attr1, attr2
to dictionaries containing the category and the encodings
n_samples (int): number of samples to draw to estimate p-value
(use exact test if number of permutations is less than or
equal to n_samples)
"""
X, Y = encs["targ1"]["encs"], encs["targ2"]["encs"]
A, B = encs["attr1"]["encs"], encs["attr2"]["encs"]
# First convert all keys to ints to facilitate array lookups
(X, Y) = convert_keys_to_ints(X, Y)
(A, B) = convert_keys_to_ints(A, B)
XY = X.copy()
XY.update(Y)
AB = A.copy()
AB.update(B)
cossims = construct_cossim_lookup(XY, AB)
# suppress print statement but might be useful to later help with interpreting the results
'''
print(
"Null hypothesis: no difference between {} and {} in association to attributes {} and {}".format(
encs["targ1"]["category"],
encs["targ2"]["category"],
encs["attr1"]["category"],
encs["attr2"]["category"],
)
)
'''
pval = p_val_permutation_test(
X, Y, A, B, n_samples, cossims=cossims, parametric=parametric
)
esize = effect_size(X, Y, A, B, cossims=cossims)
return esize, pval
if __name__ == "__main__":
X = {"x" + str(i): 2 * np.random.rand(10) - 1 for i in range(25)}
Y = {"y" + str(i): 2 * np.random.rand(10) - 1 for i in range(25)}
A = {"a" + str(i): 2 * np.random.rand(10) - 1 for i in range(25)}
B = {"b" + str(i): 2 * np.random.rand(10) - 1 for i in range(25)}
A = X
B = Y
(X, Y) = convert_keys_to_ints(X, Y)
(A, B) = convert_keys_to_ints(A, B)
XY = X.copy()
XY.update(Y)
AB = A.copy()
AB.update(B)
cossims = construct_cossim_lookup(XY, AB)
pval = p_val_permutation_test(X, Y, A, B, cossims=cossims, n_samples=10000)
esize = effect_size(X, Y, A, B, cossims=cossims)