-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrelation_functions.py
More file actions
279 lines (225 loc) · 7.6 KB
/
correlation_functions.py
File metadata and controls
279 lines (225 loc) · 7.6 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
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
# <correlation_functions.py>
#
# Implementation of correlation functions.
#
# @Authors and Contributors:
# Lucas Pascotti Valem <lucas.valem@unesp.br>
# João Gabriel Camacho Presotto <joaopresotto@gmail.com>
# Nikolas Gomes de Sá <NIKOLAS567@hotmail.com>
# Daniel Carlos Guimarães Pedronette <daniel.pedronette@unesp.br>
#
# ------------------------------------------------------------------------------
#
# This file is part of Weakly Supervised Experiments Framework (WSEF).
# Official Repository: https://github.com/UDLF/WSEF
#
# WSEF is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# WSEF is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with WSEF. If not, see <http://www.gnu.org/licenses/>.
#
# ------------------------------------------------------------------------------
import math
import numpy as np
def get_correlation_func(correlation_measure):
if correlation_measure == "jaccard":
return compute_jaccard
if correlation_measure == "jaccard_k":
return compute_jaccard_k
if correlation_measure == "rbo":
return compute_rbo
if correlation_measure == "kendalltau":
return compute_kendalltau
if correlation_measure == "kendallw":
return compute_kendallw
if correlation_measure == "spearman":
return compute_spearman
if correlation_measure == "intersection":
return compute_intersection
print("\n ERROR: Unknown correlation measure:", correlation_measure)
exit(1)
def compute_jaccard(x, y, top_k):
return len(set(x[:top_k])
& set(y[:top_k])) / len(set(x[:top_k]) | set(y[:top_k]))
def get_index(i, x):
"""
Returns the position of the element 'i' in the ranked list 'x'
"""
if i in x:
return x.index(i)
else:
return len(x)
def check_sizes(x, y):
"""
Verifies if the ranked lists 'x' and 'y' have the same size
"""
if len(x) != len(y):
return False
return True
def compute_kendalltau(x: list, y: list, top_k: int):
x = x[:top_k]
y = y[:top_k]
inter = []
for elem in (set(x) | set(y)):
inter.append((get_index(elem, x), get_index(elem, y)))
ktau = 0
n = len(inter)
for i in range(0, n):
for j in range(i + 1, n):
comp1 = inter[i][0] >= inter[j][0]
comp2 = inter[i][1] >= inter[j][1]
if (comp1 != comp2):
ktau += 1
ktau = ktau / ((n * (n - 1)) / 2)
return (1 - ktau)
def get_pos_list(rks):
rks_pos = []
for rk in rks:
rk_pos = [get_index(i + 1, rk) + 1 for i, x in enumerate(rk)]
rks_pos.append(rk_pos)
return rks_pos
def kendall_w(rks, top_k):
# compute pos list from ranked lists
rks = [rk[:top_k] for rk in rks]
rks = get_pos_list(rks)
m = len(rks) # number of ranked lists to compare
n = len(rks[0]) # number of elements in each ranked list
# compute kendall w
r = np.sum(rks, axis=0)
a = np.sum(r) / n
d = [math.fabs(x - a) for x in r]
d2 = [x**2 for x in d]
s = np.sum(d2)
w = (12 * s) / (m**2 * (n) * (n**2 - 1))
# compute chi squared
# x2 = m*(n-1)*w
# v = chi2.isf(q=0.05, df=n-1)
# reject = x2 < v
return w
def compute_kendallw(x, y, top_k):
rks = [x, y]
return kendall_w(rks, top_k)
def compute_spearman(x, y, top_k):
x = x[:top_k]
y = y[:top_k]
inter = []
for elem in (set(x) | set(y)):
inter.append((get_index(elem, x), get_index(elem, y)))
spearman = 0
n = len(inter)
for i in range(0, n):
spearman += abs(inter[i][0] - inter[i][1])
spearman = spearman / (len(x) * (len(x) + 1))
return (1 - spearman)
def compute_jaccard_k(x: list, y: list, top_k: int):
score = 0
x_leftover = set()
y_leftover = set()
stored = set() # We only want unique values
stored_x = set()
stored_y = set()
cur_inter = 0
for i in range(top_k):
x_elm = x[i]
y_elm = y[i]
if x_elm not in stored and x_elm == y_elm:
cur_inter += 1
stored.add(x_elm)
stored_x.add(x_elm)
stored_y.add(y_elm)
else:
if x_elm not in stored:
if x_elm in y_leftover:
# x_elm was previously encountered in y
cur_inter += 1
stored.add(x_elm)
stored_x.add(x_elm)
y_leftover.remove(x_elm)
else:
x_leftover.add(x_elm)
stored_x.add(x_elm)
if y_elm not in stored:
if y_elm in x_leftover:
# y_elf was previously encountered in x
cur_inter += 1
stored.add(y_elm)
stored_y.add(y_elm)
x_leftover.remove(y_elm)
else:
y_leftover.add(y_elm)
stored_y.add(y_elm)
score += cur_inter / (len(stored_x)+len(stored_y)-cur_inter)
return score / top_k
def compute_intersection(x: list, y: list, top_k: int):
if x[:top_k] == y[:top_k]:
return 1
x_leftover = set()
y_leftover = set()
stored = set() # We only want unique values
acum_inter = 0
cur_inter = 0
for i in range(top_k):
x_elm = x[i]
y_elm = y[i]
if x_elm not in stored and x_elm == y_elm:
cur_inter += 1
stored.add(x_elm)
else:
if x_elm not in stored:
if x_elm in y_leftover:
# x_elm was previously encountered in y
cur_inter += 1
stored.add(x_elm)
y_leftover.remove(x_elm)
else:
x_leftover.add(x_elm)
if y_elm not in stored:
if y_elm in x_leftover:
# y_elf was previously encountered in a
cur_inter += 1
stored.add(y_elm)
x_leftover.remove(y_elm)
else:
y_leftover.add(y_elm)
acum_inter += cur_inter
return acum_inter / ((top_k * (top_k + 1)) / 2)
def compute_rbo(x, y, top_k):
x_leftover = set()
y_leftover = set()
stored = set() # We only want unique values
acum_inter = 0
score = 0
p = 0.9
for i in range(top_k):
x_elm = x[i]
y_elm = y[i]
if x_elm not in stored and x_elm == y_elm:
acum_inter += 1
stored.add(x_elm)
else:
if x_elm not in stored:
if x_elm in y_leftover:
# x_elm was previously encountered in y
acum_inter += 1
stored.add(x_elm)
y_leftover.remove(x_elm)
else:
x_leftover.add(x_elm)
if y_elm not in stored:
if y_elm in x_leftover:
# y_elf was previously encountered in x
acum_inter += 1
stored.add(y_elm)
x_leftover.remove(y_elm)
else:
y_leftover.add(y_elm)
score += (p**((i+1) - 1)) * (acum_inter / (i+1))
return (1 - p) * score