-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_group_fairness.py
345 lines (259 loc) · 14.2 KB
/
test_group_fairness.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# pylint: disable=import-error, wrong-import-position, wrong-import-order, invalid-name
"""Fairness metrics test suite"""
from typing import List, Optional
from common import *
from pytest import approx
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
import os
import joblib
import pathlib
from trustyai.metrics.fairness.group import statistical_parity_difference, disparate_impact_ratio, \
average_odds_difference, average_predictive_value_difference, statistical_parity_difference_model, \
average_odds_difference_model, average_predictive_value_difference_model
from trustyai.model import output, Model
from java.util import Random
jrandom = Random()
TEST_DIR = pathlib.Path(__file__).parent.resolve()
INCOME_DF_BIASED = pd.read_csv(os.path.join(TEST_DIR, "data/income-biased.zip"), index_col=False)
INCOME_DF_UNBIASED = pd.read_csv(
os.path.join(TEST_DIR, "data/income-unbiased.zip"), index_col=False)
AIF_DF = pd.read_csv(os.path.join(TEST_DIR, "data/data.csv"))
XGB_MODEL = joblib.load(os.path.join(TEST_DIR, "models/income-xgd-biased.joblib"))
def test_statistical_parity_difference_random():
"""Test Statistical Parity Difference (unbalanced random data)"""
df = create_random_dataframe()
privileged = df[df.x1 < 0]
unprivileged = df[df.x1 >= 0]
favorable = output("y", dtype="number", value=1)
score = statistical_parity_difference(privileged, unprivileged, [favorable])
assert score == approx(0.9, 0.09)
def test_statistical_parity_difference_random_numpy():
"""Test Statistical Parity Difference (unbalanced random data, NumPy)"""
data = create_random_dataframe().to_numpy()
privileged = data[np.where(data[:, 0] < 0)]
unprivileged = data[np.where(data[:, 0] >= 0)]
favorable = output("y", dtype="number", value=1)
score = statistical_parity_difference(privileged=privileged,
unprivileged=unprivileged,
favorable=[favorable],
feature_names=['x1', 'x2', 'y'])
assert score == approx(0.0, 0.09)
def test_statistical_parity_difference_income():
"""Test Statistical Parity Difference (income data)"""
df = INCOME_DF_BIASED.copy()
privileged = df[df.gender == 1]
unprivileged = df[df.gender == 0]
favorable = output("income", dtype="number", value=1)
score = statistical_parity_difference(privileged, unprivileged, [favorable])
assert score == approx(-0.15, abs=0.01)
def test_statistical_parity_difference_income_numpy():
"""Test Statistical Parity Difference (income data, NumPy)"""
arr = INCOME_DF_BIASED.to_numpy()
privileged = arr[np.where(arr[:, 2] == 1)]
unprivileged = arr[np.where(arr[:, 2] == 0)]
favorable = output("income", dtype="number", value=1)
score = statistical_parity_difference(privileged=privileged,
unprivileged=unprivileged,
favorable=[favorable],
feature_names=['age', 'race', 'gender', 'income'])
assert score == approx(-0.15, abs=0.01)
def test_statistical_parity_difference_AIF():
"""Test Statistical Parity Difference (AIF data)"""
df = AIF_DF.copy()
privileged = df[df.sex == 1]
unprivileged = df[df.sex == 0]
favorable = output("income", dtype="number", value=0)
score = statistical_parity_difference(privileged=privileged,
unprivileged=unprivileged,
favorable=[favorable])
assert score == approx(0.19643287553870947, abs=1e-5)
def test_statistical_parity_difference_model():
"""Test Statistical Parity Difference (XGBoost model)"""
df = INCOME_DF_BIASED.copy()
X = df[["age", "race", "gender"]]
favorable = output("income", dtype="number", value=1)
model = Model(XGB_MODEL.predict, dataframe_input=True, output_names=["approved"])
score = statistical_parity_difference_model(X, model, [2], [1], [favorable])
assert score == approx(0.0, abs=0.09)
def test_disparate_impact_ratio_random():
"""Test Disparate Impact Ratio (unbalanced random data)"""
df = create_random_dataframe(weights=[0.5, 0.5])
privileged = df[df.x1 < 0]
unprivileged = df[df.x1 >= 0]
favorable = output("y", dtype="number", value=1)
score = disparate_impact_ratio(privileged, unprivileged, [favorable])
assert score == approx(130.0, abs=5.0)
def test_disparate_impact_ratio_income():
"""Test Disparate Impact Ratio (income data)"""
df = INCOME_DF_BIASED.copy()
privileged = df[df.gender == 1]
unprivileged = df[df.gender == 0]
favorable = output("income", dtype="number", value=1)
score = disparate_impact_ratio(privileged, unprivileged, [favorable])
assert score == approx(0.4, abs=0.05)
def test_disparate_impact_ratio_income_numpy():
"""Test Disparate Impact Ratio (income data, NumPy)"""
data = INCOME_DF_BIASED.to_numpy()
privileged = data[np.where(data[:, 2] == 1)]
unprivileged = data[np.where(data[:, 2] == 0)]
favorable = output("income", dtype="number", value=1)
score = disparate_impact_ratio(privileged=privileged,
unprivileged=unprivileged,
favorable=[favorable],
feature_names=['age', 'race', 'gender', 'income'])
assert score == approx(0.4, abs=0.05)
def test_disparate_impact_ratio_AIF():
"""Test Disparate Impact Ratio (AIF data)"""
df = AIF_DF.copy()
privileged = df[df.sex == 1]
unprivileged = df[df.sex == 0]
favorable = output("income", dtype="number", value=0)
score = disparate_impact_ratio(privileged=privileged,
unprivileged=unprivileged,
favorable=[favorable])
assert score == approx(1.28, abs=1e-2)
def test_average_odds_difference():
"""Test Average Odds Difference (unbalanced random data)"""
PRIVILEGED_CLASS_GENDER = 1
UNPRIVILEGED_CLASS_GENDER = 0
PRIVILEGED_CLASS_RACE = 4
UNPRIVILEGED_CLASS_RACE = 2
score = average_odds_difference(INCOME_DF_BIASED, INCOME_DF_UNBIASED, [1, 2],
[PRIVILEGED_CLASS_RACE, PRIVILEGED_CLASS_GENDER], [1], [3])
assert score == approx(0.12, abs=0.1)
score = average_odds_difference(INCOME_DF_BIASED, INCOME_DF_UNBIASED, [1, 2],
[UNPRIVILEGED_CLASS_RACE, UNPRIVILEGED_CLASS_GENDER], [1], [3])
assert score == approx(0.2, abs=0.1)
def test_average_odds_difference_numpy():
"""Test Average Odds Difference (unbalanced random data, NumPy)"""
PRIVILEGED_CLASS_GENDER = 1
UNPRIVILEGED_CLASS_GENDER = 0
PRIVILEGED_CLASS_RACE = 4
UNPRIVILEGED_CLASS_RACE = 2
data_biased = INCOME_DF_BIASED.to_numpy()
data_unbiased = INCOME_DF_UNBIASED.to_numpy()
score = average_odds_difference(test=data_biased,
truth=data_unbiased,
privilege_columns=[1, 2],
privilege_values=[PRIVILEGED_CLASS_RACE, PRIVILEGED_CLASS_GENDER],
positive_class=[1],
outputs=[3],
feature_names=['age', 'race', 'gender', 'income'])
assert score == approx(0.12, abs=0.1)
score = average_odds_difference(test=data_biased,
truth=data_unbiased,
privilege_columns=[1, 2],
privilege_values=[UNPRIVILEGED_CLASS_RACE, UNPRIVILEGED_CLASS_GENDER],
positive_class=[1],
outputs=[3],
feature_names=['age', 'race', 'gender', 'income'])
assert score == approx(0.2, abs=0.1)
def test_average_odds_difference_model():
"""Test Average Odds Difference (XGBoost income model)"""
df = INCOME_DF_BIASED.copy()
X = df[["age", "race", "gender"]]
model = Model(XGB_MODEL.predict, dataframe_input=True, output_names=["approved"])
score = average_odds_difference_model(samples=X,
model=model,
privilege_columns=[2],
privilege_values=[1],
positive_class=[1])
assert score == approx(0.0, abs=0.09)
score = average_odds_difference_model(samples=X,
model=model,
privilege_columns=[2],
privilege_values=[0],
positive_class=[1])
assert score == approx(0.0, abs=0.09)
def test_average_odds_difference_model_numpy():
"""Test Average Odds Difference (XGBoost income model, NumPy)"""
arr = INCOME_DF_BIASED.to_numpy()
X = arr[:, 0:3]
model = Model(XGB_MODEL.predict,
feature_names=['age', 'race', 'gender'],
output_names=["approved"])
score = average_odds_difference_model(samples=X,
model=model,
privilege_columns=[2],
privilege_values=[1],
positive_class=[1])
assert score == approx(0.0, abs=0.09)
score = average_odds_difference_model(samples=X,
model=model,
privilege_columns=[2],
privilege_values=[0],
positive_class=[1])
assert score == approx(0.0, abs=0.09)
def test_average_predictive_value_difference():
"""Test Average Predictive Value Difference (unbalanced random data)"""
PRIVILEGED_CLASS_GENDER = 1
UNPRIVILEGED_CLASS_GENDER = 0
PRIVILEGED_CLASS_RACE = 4
UNPRIVILEGED_CLASS_RACE = 2
score = average_predictive_value_difference(INCOME_DF_BIASED, INCOME_DF_UNBIASED, [1, 2],
[PRIVILEGED_CLASS_RACE, PRIVILEGED_CLASS_GENDER], [1], [3])
assert score == approx(-0.3, abs=0.1)
score = average_predictive_value_difference(INCOME_DF_BIASED, INCOME_DF_UNBIASED, [1, 2],
[UNPRIVILEGED_CLASS_RACE, UNPRIVILEGED_CLASS_GENDER], [1], [3])
assert score == approx(-0.22, abs=0.05)
def test_average_predictive_value_difference_numpy():
"""Test Average Predictive Value Difference (unbalanced random data, NumPy)"""
data_biased = INCOME_DF_BIASED.to_numpy()
data_unbiased = INCOME_DF_UNBIASED.to_numpy()
PRIVILEGED_CLASS_GENDER = 1
UNPRIVILEGED_CLASS_GENDER = 0
PRIVILEGED_CLASS_RACE = 4
UNPRIVILEGED_CLASS_RACE = 2
score = average_predictive_value_difference(test=data_biased,
truth=data_unbiased,
privilege_columns=[1, 2],
privilege_values=[PRIVILEGED_CLASS_RACE, PRIVILEGED_CLASS_GENDER],
positive_class=[1],
outputs=[3],
feature_names=['age', 'race', 'gender', 'income'])
assert score == approx(-0.3, abs=0.1)
score = average_predictive_value_difference(test=data_biased,
truth=data_unbiased,
privilege_columns=[1, 2],
privilege_values=[UNPRIVILEGED_CLASS_RACE, UNPRIVILEGED_CLASS_GENDER],
positive_class=[1],
outputs=[3],
feature_names=['age', 'race', 'gender', 'income'])
assert score == approx(-0.22, abs=0.05)
def test_average_predictive_value_difference_model():
"""Test Average Predictive Value Difference (XGB income model)"""
df = INCOME_DF_BIASED.copy()
X = df[["age", "race", "gender"]]
model = Model(XGB_MODEL.predict, dataframe_input=True, output_names=["approved"])
score = average_predictive_value_difference_model(samples=X,
model=model,
privilege_columns=[2],
privilege_values=[1],
positive_class=[1])
assert score == approx(0.0, abs=0.09)
score = average_predictive_value_difference_model(samples=X,
model=model,
privilege_columns=[2],
privilege_values=[0],
positive_class=[1])
assert score == approx(0.0, abs=0.09)
def test_average_predictive_value_difference_model_numpy():
"""Test Average Predictive Value Difference (XGB income model, NumPy)"""
arr = INCOME_DF_BIASED.to_numpy()
X = arr[:, 0:3]
model = Model(XGB_MODEL.predict,
feature_names=['age', 'race', 'gender'],
output_names=["approved"])
score = average_predictive_value_difference_model(samples=X,
model=model,
privilege_columns=[2],
privilege_values=[1],
positive_class=[1])
assert score == approx(0.0, abs=0.09)
score = average_predictive_value_difference_model(samples=X,
model=model,
privilege_columns=[2],
privilege_values=[0],
positive_class=[1])
assert score == approx(0.0, abs=0.09)