-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFairEvalClass.py
144 lines (100 loc) · 4.73 KB
/
FairEvalClass.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
import pandas as pd
import math
from scipy.special import rel_entr
class rND():
def __init__(self):
pass
@staticmethod
def rND_fairness_ranking_calculation(ranking_df):
pro_count = list(ranking_df['pro_count'].unique())[0]
con_count = list(ranking_df['con_count'].unique())[0]
N = pro_count + con_count
rnd_list = []
# For each ranking position
for i in range(1, 6):
temp_ranking = ranking_df[ranking_df['rank'].isin(range(1, i+1))]
stance_freq = temp_ranking['stance'].value_counts()
stance_freq_df = stance_freq.to_frame().reset_index().rename(columns={'stance': 'count', 'index': 'stance'})
protected_group = list(temp_ranking['protected_group'].unique())[0]
if len(stance_freq_df[stance_freq_df['stance'] == protected_group]['count']) == 0:
S_Plus_in_i = 0
else:
S_Plus_in_i = list(stance_freq_df[stance_freq_df['stance'] == protected_group]['count'])[0]
if protected_group == 'PRO':
S_plus = pro_count
else:
S_plus = con_count
intermediate_rnd = (1 / math.log(i + 1, 2)) * abs(abs(S_Plus_in_i / (i + 1)) - abs(S_plus / N))
rnd_list.append(intermediate_rnd)
final_rnd = sum(rnd_list)
return final_rnd
class rKL():
def __init__(self):
pass
@staticmethod
def rKL_fairness_ranking_calculation(ranking_df):
protected_group = list(ranking_df['protected_group'].unique())[0]
pro_count = list(ranking_df['pro_count'].unique())[0]
con_count = list(ranking_df['con_count'].unique())[0]
N = pro_count + con_count
rKL_list = []
for i in range(1, 6):
temp_ranking = ranking_df[ranking_df['rank'].isin(range(1, i+1))]
stance_freq = temp_ranking['stance'].value_counts()
stance_freq_df = stance_freq.to_frame().reset_index().rename(columns={'stance': 'count', 'index': 'stance'})
protected_group_list_in_i = stance_freq_df[stance_freq_df['stance'] == protected_group]['count']
group_list_in_i = stance_freq_df[stance_freq_df['stance'] != protected_group]['count']
# For P Vector Generation
if len(protected_group_list_in_i) == 0:
S_Plus_in_i = 0
else:
S_Plus_in_i = list(protected_group_list_in_i)[0]
# For Q Vector Generation
if len(group_list_in_i) == 0:
S_Minus_in_i = 0
else:
S_Minus_in_i = list(group_list_in_i)[0]
# P Calculation
P = [S_Plus_in_i / i, S_Minus_in_i / i]
# Q Calculation
if protected_group == 'PRO':
Q = [pro_count / N, con_count / N]
else:
Q = [con_count / N, pro_count / N]
kl_pq = rel_entr(P, Q)
rKL = sum(kl_pq) / math.log(i + 1, 2)
rKL_list.append(rKL)
final_rKL = sum(rKL_list)
return final_rKL
class rRD():
@staticmethod
def rRD_fairness_ranking_calculation(ranking_df):
pro_count = list(ranking_df['pro_count'].unique())[0]
con_count = list(ranking_df['con_count'].unique())[0]
rRD_list = []
for i in range(1, 6):
temp_ranking = ranking_df[ranking_df['rank'].isin(range(1, i+1))]
stance_freq = temp_ranking['stance'].value_counts()
stance_freq_df = stance_freq.to_frame().reset_index().rename(columns={'stance': 'count', 'index': 'stance'})
protected_group = list(temp_ranking['protected_group'].unique())[0]
if len(stance_freq_df[stance_freq_df['stance'] == protected_group]['count']) == 0:
S_Plus_in_i = 0
else:
S_Plus_in_i = list(stance_freq_df[stance_freq_df['stance'] == protected_group]['count'])[0]
if len(stance_freq_df[stance_freq_df['stance'] != protected_group]['count']) == 0:
S_Minus_in_i = 0
else:
S_Minus_in_i = list(stance_freq_df[stance_freq_df['stance'] != protected_group]['count'])[0]
if protected_group == 'PRO':
S_plus = pro_count
S_Minus = con_count
else:
S_plus = con_count
S_Minus = pro_count
if (S_Plus_in_i == 0) or (S_Minus_in_i == 0):
intermediate_rrd = (1 / math.log(i + 1, 2)) * abs(0 - abs(S_plus / S_Minus))
else:
intermediate_rrd = (1 / math.log(i + 1, 2)) * abs(abs(S_Plus_in_i / S_Minus_in_i) - abs(S_plus / S_Minus))
rRD_list.append(intermediate_rrd)
final_rrd = sum(rRD_list)
return final_rrd