-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasures.py
208 lines (141 loc) · 7.8 KB
/
measures.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
from utils import clauses_satisfied
def get_confusion_matrix_single(workload, subgroup):
subgroup_encoding = workload.create_subgroup_encoding_from_subgroup_single(subgroup)
match_TP = match_FP = match_TN = match_FN = 0
for entity_to_count in workload.entitites_to_count:
left_entity = list(entity_to_count)[:workload.find_border_in_key(entity_to_count)]
right_entity = list(entity_to_count)[workload.find_border_in_key(entity_to_count) + 1:]
left_entity_encoding = workload.create_subgroup_encoding_from_subgroup_single(left_entity)
right_entity_encoding = workload.create_subgroup_encoding_from_subgroup_single(right_entity)
if clauses_satisfied(subgroup_encoding, left_entity_encoding) or clauses_satisfied(subgroup_encoding,
right_entity_encoding):
match_TP += workload.entitites_to_count[entity_to_count][workload.TP]
match_TN += workload.entitites_to_count[entity_to_count][workload.TN]
match_FP += workload.entitites_to_count[entity_to_count][workload.FP]
match_FN += workload.entitites_to_count[entity_to_count][workload.FN]
return match_TP, match_FP, match_TN, match_FN
def get_confusion_matrix_pairwise(workload, subgroup):
encoding1, encoding2 = workload.create_subgroup_encodings_from_subgroup_pairwise(subgroup)
match_TP = match_FP = match_TN = match_FN = 0
for entity_to_count in workload.entitites_to_count:
left_entity = list(entity_to_count)[:workload.find_border_in_key(entity_to_count)]
right_entity = list(entity_to_count)[workload.find_border_in_key(entity_to_count) + 1:]
left_entity_encoding = workload.create_subgroup_encoding_from_subgroup_single(left_entity)
right_entity_encoding = workload.create_subgroup_encoding_from_subgroup_single(right_entity)
entity_encoding = left_entity_encoding + right_entity_encoding
if clauses_satisfied(encoding1, entity_encoding) or clauses_satisfied(encoding2, entity_encoding):
match_TP += workload.entitites_to_count[entity_to_count][workload.TP]
match_TN += workload.entitites_to_count[entity_to_count][workload.TN]
match_FP += workload.entitites_to_count[entity_to_count][workload.FP]
match_FN += workload.entitites_to_count[entity_to_count][workload.FN]
return match_TP, match_FP, match_TN, match_FN
def AP(TP, FP, TN, FN):
if (TP + TN + FP + FN) == 0: # denominator
return 1
else:
return (TP + TN) / (TP + TN + FP + FN)
def SP(TP, FP, TN, FN):
if (TP + FP + TN + FN) == 0: # denominator
return 1
else:
return TP / (TP + FP + TN + FN)
def TPR(TP, FP, TN, FN):
if (TP + FN) == 0: # denominator
return 1
else:
return TP / (TP + FN)
def FPR(TP, FP, TN, FN):
if (FP + TN) == 0: # denominator
return 1
else:
return FP / (FP + TN)
def FNR(TP, FP, TN, FN):
if (FN + TP) == 0: # denominator
return 1
else:
return FN / (FN + TP)
def TNR(TP, FP, TN, FN):
if (TN + FP) == 0: # denominator
return 1
else:
return TN / (TN + FP)
def PPV(TP, FP, TN, FN):
if (TP + FP) == 0: # denominator
return 1
else:
return TP / (TP + FP)
def NPV(TP, FP, TN, FN):
if (TN + FN) == 0: # denominator
return 1
else:
return TN / (TN + FN)
def FDR(TP, FP, TN, FN):
if (TP + FP) == 0: # denominator
return 1
else:
return FP / (TP + FP)
def FOR(TP, FP, TN, FN):
if (TN + FN) == 0: # denominator
return 1
else:
return FN / (TN + FN)
def accuracy_parity_single(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_single(workload, subgroup)
return AP(match_TP, match_FP, match_TN, match_FN)
def accuracy_parity_pairwise(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_pairwise(workload, subgroup)
return AP(match_TP, match_FP, match_TN, match_FN)
def statistical_parity_single(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_single(workload, subgroup)
return SP(match_TP, match_FP, match_TN, match_FN)
def statistical_parity_pairwise(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_pairwise(workload, subgroup)
return SP(match_TP, match_FP, match_TN, match_FN)
def true_positive_rate_parity_single(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_single(workload, subgroup)
return TPR(match_TP, match_FP, match_TN, match_FN)
def true_positive_rate_parity_pairwise(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_pairwise(workload, subgroup)
return TPR(match_TP, match_FP, match_TN, match_FN)
def false_positive_rate_parity_single(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_single(workload, subgroup)
return FPR(match_TP, match_FP, match_TN, match_FN)
def false_positive_rate_parity_pairwise(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_pairwise(workload, subgroup)
return FPR(match_TP, match_FP, match_TN, match_FN)
def false_negative_rate_parity_single(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_single(workload, subgroup)
return FNR(match_TP, match_FP, match_TN, match_FN)
def false_negative_rate_parity_pairwise(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_pairwise(workload, subgroup)
return FNR(match_TP, match_FP, match_TN, match_FN)
def true_negative_rate_parity_single(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_single(workload, subgroup)
return TNR(match_TP, match_FP, match_TN, match_FN)
def true_negative_rate_parity_pairwise(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_pairwise(workload, subgroup)
return TNR(match_TP, match_FP, match_TN, match_FN)
def positive_predictive_value_parity_single(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_single(workload, subgroup)
return PPV(match_TP, match_FP, match_TN, match_FN)
def negative_predictive_value_parity_single(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_single(workload, subgroup)
return NPV(match_TP, match_FP, match_TN, match_FN)
def positive_predictive_value_parity_pairwise(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_pairwise(workload, subgroup)
return PPV(match_TP, match_FP, match_TN, match_FN)
def negative_predictive_value_parity_pairwise(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_pairwise(workload, subgroup)
return NPV(match_TP, match_FP, match_TN, match_FN)
def false_discovery_rate_parity_single(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_single(workload, subgroup)
return FDR(match_TP, match_FP, match_TN, match_FN)
def false_omission_rate_parity_single(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_single(workload, subgroup)
return FOR(match_TP, match_FP, match_TN, match_FN)
def false_discovery_rate_parity_pairwise(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_pairwise(workload, subgroup)
return FDR(match_TP, match_FP, match_TN, match_FN)
def false_omission_rate_parity_pairwise(workload, subgroup):
(match_TP, match_FP, match_TN, match_FN) = get_confusion_matrix_pairwise(workload, subgroup)
return FOR(match_TP, match_FP, match_TN, match_FN)