-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
58 lines (34 loc) · 2.38 KB
/
metrics.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
import numpy as np
from sklearn.metrics import accuracy_score, balanced_accuracy_score
def tpr_protected(y_true, y_pred, is_protected):
return np.sum((y_pred == 1) & (y_true == 1) & (is_protected == 1)) / np.sum((y_true == 1) & (is_protected == 1))
def tpr_non_protected(y_true, y_pred, is_protected):
return np.sum((y_pred == 1) & (y_true == 1) & (is_protected == 0)) / np.sum((y_true == 1) & (is_protected == 0))
def tnr_protected(y_true, y_pred, is_protected):
return np.sum((y_pred == 0) & (y_true == 0) & (is_protected == 1)) / np.sum((y_true == 0) & (is_protected == 1))
def tnr_non_protected(y_true, y_pred, is_protected):
return np.sum((y_pred == 0) & (y_true == 0) & (is_protected == 0)) / np.sum((y_true == 0) & (is_protected == 0))
def fpr_protected(y_true, y_pred, is_protected):
return np.sum((y_pred == 1) & (y_true == 0) & (is_protected == 1)) / np.sum((y_true == 0) & (is_protected == 1))
def fpr_non_protected(y_true, y_pred, is_protected):
return np.sum((y_pred == 1) & (y_true == 0) & (is_protected == 0)) / np.sum((y_true == 0) & (is_protected == 0))
def fnr_protected(y_true, y_pred, is_protected):
return np.sum((y_pred == 0) & (y_true == 1) & (is_protected == 1)) / np.sum((y_true == 1) & (is_protected == 1))
def fnr_non_protected(y_true, y_pred, is_protected):
return np.sum((y_pred == 0) & (y_true == 1) & (is_protected == 0)) / np.sum((y_true == 1) & (is_protected == 0))
def delta_fpr(y_true, y_pred, is_protected):
return fpr_non_protected(y_true, y_pred, is_protected) - fpr_protected(y_true, y_pred, is_protected)
def delta_fnr(y_true, y_pred, is_protected):
return fnr_non_protected(y_true, y_pred, is_protected) - fnr_protected(y_true, y_pred, is_protected)
def equalized_odds(y_true, y_pred, is_protected):
return abs(delta_fpr(y_true, y_pred, is_protected)) + abs(delta_fnr(y_true, y_pred, is_protected))
def calculate_metrics(y_true, y_pred, is_protected):
return {
'Accuracy': accuracy_score(y_true, y_pred),
'Bal. Acc.': balanced_accuracy_score(y_true, y_pred),
'Eq.Odds': equalized_odds(y_true, y_pred, is_protected),
'TPR Prot.': tpr_protected(y_true, y_pred, is_protected),
'TPR Non-Prot.': tpr_non_protected(y_true, y_pred, is_protected),
'TNR Prot.': tnr_protected(y_true, y_pred, is_protected),
'TNR Non-Prot.': tnr_non_protected(y_true, y_pred, is_protected)
}