-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstat_test.py
297 lines (274 loc) · 10.9 KB
/
stat_test.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import numpy as np
from multiprocessing import Pool
import numpy as np
from correlation import _cal_kendall, _cal_spearman, _cal_pearson, correlation_system, correlation_summ_values
def bootstraping(fn, refs, sys1, sys2, corr_func, sample_size=-1, repetitions=1e5, num_workers=32):
# perform bootstraping test, multi process
with Pool(processes=num_workers) as pool:
results = [pool.apply_async(fn, args=(refs, sys1, sys2, corr_func, sample_size, repetitions / num_workers)) for _ in range(num_workers)]
cnts = [res.get() for res in results]
return sum(cnts) / repetitions
### bootstrap tests for correlation significance values ###
## bootstraping significance values, system level
def _bootstraping_system(refs, sys1, sys2, corr_func, sample_size=-1, repetitions=1e5):
# perform bootstraping test, single process
np.random.seed()
if sample_size < 0:
sample_size = refs.shape[1]
cnt = 0
for i in range(int(repetitions)):
idx = np.random.choice(refs.shape[1], sample_size) # sample examples
refs_sample = refs[:, idx]
sys1_sample = sys1[:, idx]
sys2_sample = sys2[:, idx]
corr1 = correlation_system(refs_sample, sys1_sample, corr_func)
corr2 = correlation_system(refs_sample, sys2_sample, corr_func)
now_delta = corr1 - corr2 # calculate delta (difference)
if now_delta < 0:
cnt += 1
return cnt
def bootstrap_system(refs, sys1, sys2, corr_func, num_workers=1, verbose=False):
"""
refs: [num_system, num_summ] array
sys1, sys2: [num_system, num_summ] array
corr_func: name of correlation function
num_workers: number of processes
"""
if corr_func == "pearson":
corr_func = _cal_pearson
elif corr_func == "spearman":
corr_func = _cal_spearman
elif corr_func == "kendall":
corr_func = _cal_kendall
else:
raise NotImplementedError
corr_sys1 = correlation_system(refs, sys1, corr_func)
corr_sys2 = correlation_system(refs, sys2, corr_func)
diff = corr_sys1 - corr_sys2
if verbose:
print("sys1 correlation:", corr_sys1)
print("sys2 correlation:", corr_sys2)
print("correlation difference:", diff)
if diff < 0:
sys1, sys2 = sys2, sys1
p = bootstraping(_bootstraping_system, refs, sys1, sys2, corr_func, num_workers=num_workers)
if verbose:
print("p-value:", p)
return p, diff
## bootstraping significance values, summary level
def _bootstraping_summ_test(data, sample_size=-1, repetitions=1e5):
# perform bootstraping test, single process
np.random.seed()
if sample_size < 0:
sample_size = data.shape[0]
cnt = 0
for i in range(int(repetitions)):
idx = np.random.choice(data.shape[0], sample_size) # sample examples
samples = data[idx]
now_delta = samples.mean() # calculate delta (difference)
if now_delta < 0:
cnt += 1
return cnt
def _bootstraping_summ(diff, sample_size=-1, repetitions=1e5, num_workers=32):
# perform bootstraping test, multi process
with Pool(processes=num_workers) as pool:
results = [pool.apply_async(_bootstraping_summ_test, args=(diff, sample_size, repetitions / num_workers)) for _ in range(num_workers)]
cnts = [res.get() for res in results]
return sum(cnts) / repetitions
def bootstrap_summ(refs, sys1, sys2, corr_func, num_workers, verbose=False):
"""
refs: [num_system, num_summ] array
sys1, sys2: [num_system, num_summ] array
corr_func: name of correlation function
num_workers: number of processes
"""
if corr_func == "pearson":
corr_func = _cal_pearson
elif corr_func == "spearman":
corr_func = _cal_spearman
elif corr_func == "kendall":
corr_func = _cal_kendall
else:
raise NotImplementedError
corr_sys1, values1 = correlation_summ_values(refs, sys1, corr_func)
corr_sys2, values2 = correlation_summ_values(refs, sys2, corr_func)
diff = corr_sys1 - corr_sys2
if verbose:
print("sys1 correlation:", corr_sys1)
print("sys2 correlation:", corr_sys2)
print("correlation difference:", diff)
if diff < 0:
sys1, sys2 = sys2, sys1
values1, values2 = values2, values1
diffs = values1 - values2
# print(diffs)
p = _bootstraping_summ(diffs, num_workers=num_workers)
if verbose:
print("p-value:", p)
return p, diff
### permutation tests for correlation significance values ###
## permutation test, system level
def _permutation_system(refs, sys1, sys2, corr_func, sample_size=-1, repetitions=1e5):
# perform permutation test, single process
np.random.seed()
if sample_size < 0:
sample_size = refs.shape[1]
cnt = 0
corr_sys1 = correlation_system(refs, sys1, corr_func)
corr_sys2 = correlation_system(refs, sys2, corr_func)
delta = corr_sys1 - corr_sys2
for i in range(int(repetitions)):
idx = np.random.random(refs.shape[1]) < 0.5
sys1_sample = np.copy(sys1)
sys2_sample = np.copy(sys2)
sys2_sample[:, idx] = sys1[:, idx]
sys1_sample[:, idx] = sys2[:, idx]
corr1 = correlation_system(refs, sys1_sample, corr_func)
corr2 = correlation_system(refs, sys2_sample, corr_func)
now_delta = corr1 - corr2 # calculate delta (difference)
if now_delta > delta:
cnt += 1
return cnt
def permutation_system(refs, sys1, sys2, corr_func, num_workers, verbose=False):
"""
refs: [num_system, num_summ] array
sys1, sys2: [num_system, num_summ] array
corr_func: name of correlation function
num_workers: number of processes
"""
if corr_func == "pearson":
corr_func = _cal_pearson
elif corr_func == "spearman":
corr_func = _cal_spearman
elif corr_func == "kendall":
corr_func = _cal_kendall
else:
raise NotImplementedError
corr_sys1 = correlation_system(refs, sys1, corr_func)
corr_sys2 = correlation_system(refs, sys2, corr_func)
diff = corr_sys1 - corr_sys2
if verbose:
print("sys1 correlation:", corr_sys1)
print("sys2 correlation:", corr_sys2)
print("correlation difference:", diff)
if diff < 0:
sys1, sys2 = sys2, sys1
p = bootstraping(_permutation_system, refs, sys1, sys2, corr_func, num_workers=num_workers)
if verbose:
print("p-value:", p)
return p, diff
## bootstraping significance values, summary level
def _permutation_summ_test(corr_sys1, corr_sys2, sample_size=-1, repetitions=1e5):
# perform bootstraping test, single process
np.random.seed()
if sample_size < 0:
sample_size = corr_sys1.shape[0]
cnt = 0
delta = (corr_sys1 - corr_sys2).mean()
for i in range(int(repetitions)):
idx = np.random.random(sample_size) < 0.5
sys1_sample = np.copy(corr_sys1)
sys2_sample = np.copy(corr_sys2)
sys2_sample[idx] = corr_sys1[idx]
sys1_sample[idx] = corr_sys2[idx]
now_delta = (sys1_sample - sys2_sample).mean() # calculate delta (difference)
if now_delta > delta:
cnt += 1
return cnt
def _permutation_summ(x, y, sample_size=-1, repetitions=1e5, num_workers=32):
# perform bootstraping test, multi process
with Pool(processes=num_workers) as pool:
results = [pool.apply_async(_permutation_summ_test, args=(x, y, sample_size, repetitions / num_workers)) for _ in range(num_workers)]
cnts = [res.get() for res in results]
return sum(cnts) / repetitions
def permutation_summ(refs, sys1, sys2, corr_func, num_workers, verbose=False):
"""
refs: [num_system, num_summ] array
sys1, sys2: [num_system, num_summ] array
corr_func: name of correlation function
num_workers: number of processes
"""
if corr_func == "pearson":
corr_func = _cal_pearson
elif corr_func == "spearman":
corr_func = _cal_spearman
elif corr_func == "kendall":
corr_func = _cal_kendall
corr_sys1, values1 = correlation_summ_values(refs, sys1, corr_func)
corr_sys2, values2 = correlation_summ_values(refs, sys2, corr_func)
diff = corr_sys1 - corr_sys2
if verbose:
print("sys1 correlation:", corr_sys1)
print("sys2 correlation:", corr_sys2)
print("correlation difference:", diff)
if diff < 0:
sys1, sys2 = sys2, sys1
values1, values2 = values2, values1
corr_sys1, corr_sys2 = corr_sys2, corr_sys1
p = _permutation_summ(values1, values2, num_workers=num_workers)
if verbose:
print("p-value:", p)
return p, diff
### confidence interval ###
## bootstraping confidence intervals
def _confidence_system(refs, cands, corr_func, sample_size=-1, repetitions=1e5):
# perform bootstraping test, single process
np.random.seed()
if sample_size < 0:
sample_size = refs.shape[1]
results = []
for i in range(int(repetitions)):
idx = np.random.choice(refs.shape[1], sample_size) # sample examples
refs_sample = refs[:, idx]
cands_sample = cands[:, idx]
corr = correlation_system(refs_sample, cands_sample, corr_func)
results.append(corr)
return results
def _confidence_summ(refs, cands, corr_func, sample_size=-1, repetitions=1e5):
# perform bootstraping test, single process
np.random.seed()
if sample_size < 0:
sample_size = refs.shape[1]
results = []
_, values = correlation_summ_values(refs, cands, corr_func)
for i in range(int(repetitions)):
idx = np.random.choice(refs.shape[1], sample_size) # sample examples
corr = values[idx].mean()
results.append(corr)
return results
def confidence_interval(level, refs, cands, corr_func, sample_size=-1, repetitions=1e5, num_workers=1):
"""
calculate confidence interval (2.5%, 97.5%) for system level or summary level
level: "system" or "summ"
refs: [num_system, num_summ] array
cands: [num_system, num_summ] array
corr_func: name of correlation function
num_workers: number of processes
"""
if corr_func == "pearson":
corr_func = _cal_pearson
elif corr_func == "spearman":
corr_func = _cal_spearman
elif corr_func == "kendall":
corr_func = _cal_kendall
else:
raise NotImplementedError
if level == "system":
fn = _confidence_system
elif level == "summ":
fn = _confidence_summ
else:
raise NotImplementedError
with Pool(processes=num_workers) as pool:
results = [pool.apply_async(fn, args=(refs, cands, corr_func, sample_size, repetitions / num_workers)) for _ in range(num_workers)]
results = [res.get() for res in results]
_results = []
for res in results:
_results.extend(res)
results = _results
# compute confidence interval
results = np.array(results)
head = np.percentile(results, 2.5)
tail = np.percentile(results, 97.5)
print("confidence interval:", head, tail)
return head, tail