-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbalancers.py
1608 lines (1356 loc) · 65.7 KB
/
balancers.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Debiasing binary predictions with linear programming.
Binary implementation based on work by Hardt, Srebro, & Price (2016):
https://arxiv.org/pdf/1610.02413.pdf
Multiclass implementation based on forthcoming publication at SafeAI 2022
"""
import pandas as pd
import numpy as np
import scipy as sp
import itertools
import seaborn as sns
from matplotlib import pyplot as plt
from itertools import combinations
from copy import deepcopy
from sklearn.metrics import roc_curve
import tools
class BinaryBalancer:
def __init__(self,
y,
y_,
a,
data=None,
summary=True,
threshold_objective='j'):
"""Initializes an instance of a PredictionBalancer.
Parameters
----------
y : array-like of shape (n_samples,) or str
The true labels, either as a binary array or as a string \
specifying a column in data.
y_ : array-like of shape (n_samples,) or str
The predicted labels, either as an int (predictions) or float \
(probabilities) array, or as a string specifying a column in data.
a : array-like of shape (n_samples,) or str
The protected attribute, either as an array, or as a string \
specifying the column in data.
data : pd.DataFrame instance, default None
(Optional) DataFrame from which to pull y, y_, and a.
summary : bool, default True
Whether to print pre-adjustment false-positive and true-positive \
rates for each group.
threshold_objective : str, default 'j'
Objective to use in evaluating thresholds when y_ contains \
probabilities. Default is Youden's J index, or TPR - (1 - FPR) + 1.
Attributes
----------
actual_loss : float
Loss on the current set of predictions in y_adj.
con : ndarray
The coefficients of the constraint matrix for the linear program.
goal : {'odds', 'opportunity'}, default 'odds'
The fairness constraint to satisfy. Options are equalized odds \
or equal opportunity. Set during .adjust().
group_rates : dict
The unadjusted tools.CLFRates object for each group.
overall_rates : tools.CLFRates object
The unadjusted CLFRates for the data overall.
p : ndarray of shape (n_groups,)
The proportions for each level of the protected attribute.
pya : ndrray of shape (n_groups, 2)
(P(y~=1 | y_=0), P(y~=1 | y_=1)) for each group after adjustment. \
Set during .adjust().
opt : SciPy.Optimize.OptimizeResult
Optimizer solved by .adjust().
rocs : list of sklearn.metrics.roc_curve results
The roc curves for each group. Set only when y_ contains \
probabilities.
roc : tuple
The theoretical optimum for (fpr, tpr) under equalized odds. Set \
during .adjiust().
theoretical_loss : float
The theoretical optimum for loss given the constraints.
y_adj : ndarray of shape (n_samples,)
Predictions generated using the post-adjustment probabilities in \
pya. Set on .adjust().
"""
# Optional pull from a pd.DataFrame()
if data is not None:
y = data[y].values
y_ = data[y_].values
a = data[a].values
# Setting the targets
self.y = y
self.y_ = y_
self.a = a
self.rocs = None
self.roc = None
self.con = None
self.goal = None
self.thr_obj = threshold_objective
# Getting the group info
self.groups = np.unique(a)
group_ids = [np.where(a == g)[0] for g in self.groups]
self.p = [len(cols) / len(y) for cols in group_ids]
# Optionally thresholding probabilities to get class predictions
if np.any([0 < x < 1 for x in y_]):
print('Probabilities detected.\n')
probs = deepcopy(y_)
self.rocs = [roc_curve(y[ids], probs[ids]) for ids in group_ids]
self.__roc_stats = [tools.loss_from_roc(y[ids],
probs[ids],
self.rocs[i])
for i, ids in enumerate(group_ids)]
if self.thr_obj == 'j':
cut_ids = [np.argmax(rs['js']) for rs in self.__roc_stats]
self.cuts = [self.rocs[i][2][id] for i, id in enumerate(cut_ids)]
for g, cut in enumerate(self.cuts):
probs[group_ids[g]] = tools.threshold(probs[group_ids[g]],
cut)
self.y_ = probs.astype(np.uint8)
# Calcuating the groupwise classification rates
self.__gr_list = [tools.CLFRates(self.y[i], self.y_[i])
for i in group_ids]
self.group_rates = dict(zip(self.groups, self.__gr_list))
# And then the overall rates
self.overall_rates = tools.CLFRates(self.y, self.y_)
if summary:
self.summary(adj=False)
def adjust(self,
goal='odds',
round=4,
return_optima=False,
summary=True,
binom=False):
"""Adjusts predictions to satisfy a fairness constraint.
Parameters
----------
goal : {'odds', 'opportunity'}, default 'odds'
The constraint to be satisifed. Equalized odds and equal \
opportunity are currently supported.
round : int, default 4
Decimal places for rounding results.
return_optima: bool, default True
Whether to reutn optimal loss and ROC coordinates.
summary : bool, default True
Whether to print post-adjustment false-positive and true-positive \
rates for each group.
binom : bool, default False
Whether to generate adjusted predictions by sampling from a \
binomial distribution.
Returns
-------
(optional) optima : dict
The optimal loss and ROC coordinates after adjustment.
"""
self.goal = goal
# Getting the coefficients for the objective
dr = [(g.nr * self.p[i], g.pr * self.p[i])
for i, g in enumerate(self.__gr_list)]
# Getting the overall error rates and group proportions
s = self.overall_rates.acc
e = 1 - s
# Setting up the coefficients for the objective function
obj_coefs = np.array([[(s - e) * r[0],
(e - s) * r[1]]
for r in dr]).flatten()
obj_bounds = [(0, 1)]
# Generating the pairs for comparison
n_groups = len(self.groups)
group_combos = list(combinations(self.groups, 2))
id_combos = list(combinations(range(n_groups), 2))
# Pair drop to keep things full-rank with 3 or more groups
if n_groups > 2:
n_comp = n_groups - 1
group_combos = group_combos[:n_comp]
id_combos = id_combos[:n_comp]
col_combos = np.array(id_combos) * 2
n_pairs = len(group_combos)
# Making empty matrices to hold the pairwise constraint coefficients
tprs = np.zeros(shape=(n_pairs, 2 * n_groups))
fprs = np.zeros(shape=(n_pairs, 2 * n_groups))
# Filling in the constraint matrices
for i, cols in enumerate(col_combos):
# Fetching the group-specific rates
gc = group_combos[i]
g0 = self.group_rates[gc[0]]
g1 = self.group_rates[gc[1]]
# Filling in the group-specific coefficients
tprs[i, cols[0]] = g0.fnr
tprs[i, cols[0] + 1] = g0.tpr
tprs[i, cols[1]] = -g1.fnr
tprs[i, cols[1] + 1] = -g1.tpr
fprs[i, cols[0]] = g0.tnr
fprs[i, cols[0] + 1] = g0.fpr
fprs[i, cols[1]] = -g1.tnr
fprs[i, cols[1] + 1] = -g1.fpr
# Choosing whether to go with equalized odds or opportunity
if 'odds' in goal:
self.con = np.vstack((tprs, fprs))
elif 'opportunity' in goal:
self.con = tprs
elif 'parity' in goal:
pass
con_b = np.zeros(self.con.shape[0])
# Running the optimization
self.opt = sp.optimize.linprog(c=obj_coefs,
bounds=obj_bounds,
A_eq=self.con,
b_eq=con_b,
method='highs')
self.pya = self.opt.x.reshape(len(self.groups), 2)
# Setting the adjusted predictions
self.y_adj = tools.pred_from_pya(y_=self.y_,
a=self.a,
pya=self.pya,
binom=binom)
# Getting theoretical (no rounding) and actual (with rounding) loss
self.actual_loss = 1 - tools.CLFRates(self.y, self.y_adj).acc
cmin = self.opt.fun
tl = cmin + (e*self.overall_rates.nr) + (s*self.overall_rates.pr)
self.theoretical_loss = tl
# Calculating the theoretical balance point in ROC space
p0, p1 = self.pya[0][0], self.pya[0][1]
group = self.group_rates[self.groups[0]]
fpr = (group.tnr * p0) + (group.fpr * p1)
tpr = (group.fnr * p0) + (group.tpr * p1)
self.roc = (np.round(fpr, round), np.round(tpr, round))
if summary:
self.summary(org=False)
if return_optima:
return {'loss': self.theoretical_loss, 'roc': self.roc}
def predict(self, y_, a, binom=False):
"""Generates bias-adjusted predictions on new data.
Parameters
----------
y_ : ndarry of shape (n_samples,)
A binary- or real-valued array of unadjusted predictions.
a : ndarray of shape (n_samples,)
The protected attributes for the samples in y_.
binom : bool, default False
Whether to generate adjusted predictions by sampling from a \
binomial distribution.
Returns
-------
y~ : ndarray of shape (n_samples,)
The adjusted binary predictions.
"""
# Optional thresholding for continuous predictors
if np.any([0 < x < 1 for x in y_]):
group_ids = [np.where(a == g)[0] for g in self.groups]
y_ = deepcopy(y_)
for g, cut in enumerate(self.cuts):
y_[group_ids[g]] = tools.threshold(y_[group_ids[g]], cut)
# Returning the adjusted predictions
adj = tools.pred_from_pya(y_, a, self.pya, binom)
return adj
def plot(self,
s1=50,
s2=50,
preds=False,
optimum=True,
roc_curves=True,
lp_lines='all',
shade_hull=True,
chance_line=True,
palette='colorblind',
style='white',
xlim=(0, 1),
ylim=(0, 1),
alpha=0.5):
"""Generates a variety of plots for the PredictionBalancer.
Parameters
----------
s1, s2 : int, default 50
The size parameters for the unadjusted (1) and adjusted (2) ROC \
coordinates.
preds : bool, default False
Whether to observed ROC values for the adjusted predictions (as \
opposed to the theoretical optima).
optimum : bool, default True
Whether to plot the theoretical optima for the predictions.
roc_curves : bool, default True
Whether to plot ROC curves for the unadjusted scores, when avail.
lp_lines : {'upper', 'all'}, default 'all'
Whether to plot the convex hulls solved by the linear program.
shade_hull : bool, default True
Whether to fill the convex hulls when the LP lines are shown.
chance_line : bool, default True
Whether to plot the line ((0, 0), (1, 1))
palette : str, default 'colorblind'
Color palette to pass to Seaborn.
style : str, default 'dark'
Style argument passed to sns.set_style()
alpha : float, default 0.5
Alpha parameter for scatterplots.
Returns
-------
A plot showing shapes were specified by the arguments.
"""
# Setting basic plot parameters
plt.xlim(xlim)
plt.ylim(ylim)
sns.set_theme()
sns.set_style(style)
cmap = sns.color_palette(palette, as_cmap=True)
# Plotting the unadjusted ROC coordinates
orig_coords = tools.group_roc_coords(self.y,
self.y_,
self.a)
sns.scatterplot(x=orig_coords.fpr,
y=orig_coords.tpr,
hue=self.groups,
s=s1,
palette='colorblind')
plt.legend(loc='lower right')
# Plotting the adjusted coordinates
if preds:
adj_coords = tools.group_roc_coords(self.y,
self.y_adj,
self.a)
sns.scatterplot(x=adj_coords.fpr,
y=adj_coords.tpr,
hue=self.groups,
palette='colorblind',
marker='x',
legend=False,
s=s2,
alpha=1)
# Optionally adding the ROC curves
if self.rocs is not None and roc_curves:
[plt.plot(r[0], r[1]) for r in self.rocs]
# Optionally adding the chance line
if chance_line:
plt.plot((0, 1), (0, 1),
color='lightgray')
# Adding lines to show the LP geometry
if lp_lines:
# Getting the groupwise coordinates
group_rates = self.group_rates.values()
group_var = np.array([[g]*3 for g in self.groups]).flatten()
# Getting coordinates for the upper portions of the hulls
upper_x = np.array([[0, g.fpr, 1] for g in group_rates]).flatten()
upper_y = np.array([[0, g.tpr, 1] for g in group_rates]).flatten()
upper_df = pd.DataFrame((upper_x, upper_y, group_var)).T
upper_df.columns = ['x', 'y', 'group']
upper_df = upper_df.astype({'x': 'float',
'y': 'float',
'group': 'str'})
# Plotting the line
sns.lineplot(x='x',
y='y',
hue='group',
data=upper_df,
alpha=0.75,
legend=False)
# Optionally adding lower lines to complete the hulls
if lp_lines == 'all':
lower_x = np.array([[0, 1 - g.fpr, 1]
for g in group_rates]).flatten()
lower_y = np.array([[0, 1 - g.tpr, 1]
for g in group_rates]).flatten()
lower_df = pd.DataFrame((lower_x, lower_y, group_var)).T
lower_df.columns = ['x', 'y', 'group']
lower_df = lower_df.astype({'x': 'float',
'y': 'float',
'group': 'str'})
# Plotting the line
sns.lineplot(x='x',
y='y',
hue='group',
data=lower_df,
alpha=0.75,
legend=False)
# Shading the area under the lines
if shade_hull:
for i, group in enumerate(self.groups):
uc = upper_df[upper_df.group == group]
u_null = np.array([0, uc.x.values[1], 1])
if lp_lines == 'upper':
plt.fill_between(x=uc.x,
y1=uc.y,
y2=u_null,
color=cmap[i],
alpha=0.2)
if lp_lines == 'all':
lc = lower_df[lower_df.group == group]
l_null = np.array([0, lc.x.values[1], 1])
plt.fill_between(x=uc.x,
y1=uc.y,
y2=u_null,
color=cmap[i],
alpha=0.2)
plt.fill_between(x=lc.x,
y1=l_null,
y2=lc.y,
color=cmap[i],
alpha=0.2)
# Optionally adding the post-adjustment optimum
if optimum:
if self.roc is None:
print('.adjust() must be called before optimum can be shown.')
pass
elif 'odds' in self.goal:
plt.scatter(self.roc[0],
self.roc[1],
marker='x',
color='black')
elif 'opportunity' in self.goal:
plt.hlines(self.roc[1],
xmin=0,
xmax=1,
color='black',
linestyles='--',
linewidths=0.5)
elif 'parity' in self.goal:
pass
plt.show()
def summary(self, org=True, adj=True):
"""Prints a summary with FPRs and TPRs for each group.
Parameters:
org : bool, default True
Whether to print results for the original predictions.
adj : bool, default True
Whether to print results for the adjusted predictions.
"""
if org:
org_coords = tools.group_roc_coords(self.y, self.y_, self.a)
org_loss = 1 - self.overall_rates.acc
print('\nPre-adjustment group rates are \n')
print(org_coords.to_string(index=False))
print('\nAnd loss is %.4f\n' %org_loss)
if adj:
adj_coords = tools.group_roc_coords(self.y, self.y_adj, self.a)
adj_loss = 1 - tools.CLFRates(self.y, self.y_adj).acc
print('\nPost-adjustment group rates are \n')
print(adj_coords.to_string(index=False))
print('\nAnd loss is %.4f\n' %adj_loss)
class MulticlassBalancer:
def __init__(self,
y,
y_,
a,
data=None,
preds_are_probs=False,
summary=False):
"""Initializes an instance of a PredictionBalancer.
Parameters
----------
y : array-like of shape (n_samples,) or str
The true labels, either as an array or as a string \
specifying a column in data.
y_ : array-like of shape (n_samples,) or str
The predicted labels, either as an array or as a string \
specifying a column in data.
a : array-like of shape (n_samples,) or str
The protected attribute, either as an array, or as a string \
specifying the column in data.
data : pd.DataFrame instance, default None
(Optional) DataFrame from which to pull y, y_, and a.
summary : bool, default True
Whether to print pre-adjustment false-positive and true-positive \
rates for each group.
Attributes
----------
actual_loss : float
Loss on the current set of predictions in y_adj.
con : ndarray
The coefficients of the constraint matrix for the linear program.
goal : {'odds', 'opportunity'}, default 'odds'
The fairness constraint to satisfy. Options are equalized odds \
or equal opportunity. Set during .adjust().
group_rates : dict
The unadjusted tools.CLFRates object for each group.
overall_rates : tools.CLFRates object
The unadjusted CLFRates for the data overall.
p : ndarray of shape (n_groups,)
The proportions for each level of the protected attribute.
pya : ndrray of shape (n_groups, 2)
(P(y~=1 | y_=0), P(y~=1 | y_=1)) for each group after adjustment. \
Set during .adjust().
opt : SciPy.Optimize.OptimizeResult
Optimizer solved by .adjust().
rocs : list of sklearn.metrics.roc_curve results
The roc curves for each group. Set only when y_ contains \
probabilities.
roc : tuple
The theoretical optimum for (fpr, tpr) under equalized odds. Set \
during .adjiust().
theoretical_loss : float
The theoretical optimum for loss given the constraints.
y_adj : ndarray of shape (n_samples,)
Predictions generated using the post-adjustment probabilities in \
pya. Set on .adjust().
"""
# Optional pull from a pd.DataFrame()
if data is not None:
y = data[y].values
y_ = data[y_].values
a = data[a].values
# Setting the targets
self.y = y
self.a = a
self.rocs = None
self.roc = None
self.con = None
self.goal = ''
# Getting the group info
self.p_y = tools.p_vec(y)
self.p_a = tools.p_vec(a)
self.n_classes = self.p_y.shape[0]
self.outcomes = np.unique(y)
# Converting predicted probabilities to class predictions
if preds_are_probs:
self.old_brier_score = tools.brier_score(y, y_)
y_ = np.array([self.outcomes[i]
for i in np.argmax(y_, axis=1)])
self.y_ = y_
# Getting some basic info for each group
self.groups = np.unique(a)
self.n_groups = len(self.groups)
self.group_probs = tools.p_vec(a)
group_ids = [np.where(a == g) for g in self.groups]
self.p_y_a = np.array([tools.p_vec(y[ids])
for ids in group_ids])
# Getting the group-specific P(Y), P(Y- | Y), and constraint matrices
p_vecs = np.array([tools.p_vec(y[ids]) for ids in group_ids])
p_pred_vecs = np.array([tools.p_vec(y_[ids]) for ids in group_ids])
self.p_pred_vecs = self.p_a.reshape(-1, 1) * p_pred_vecs
self.p_vecs = self.p_a.reshape(-1, 1) * p_vecs
self.cp_mats = np.array([tools.cp_mat(y[ids], y_[ids])
for ids in group_ids])
self.cp_mats_t = np.zeros(
(self.n_classes, self.n_classes, self.n_groups)
)
for a in range(self.n_groups):
self.cp_mats_t[:, :, a] = self.cp_mats[a].transpose()
self.cp_mat = tools.cp_mat(y, y_)
old_rocs = [tools.cpmat_to_roc(self.p_vecs[i],
self.cp_mats[i])
for i in range(self.n_groups)]
self.old_rocs = np.array(old_rocs)
if not preds_are_probs:
probs = tools.cat_to_probs(self.y,
self.a,
self.cp_mats)
self.old_brier_score = tools.brier_score(self.y,
probs)
if summary:
self.summary(adj=False)
def __get_constraints(self, p_vec, p_a, cp_mat):
'''Calculates TPR and FPR weights for the constraint matrix'''
# Shortening the vars to keep things clean
p = p_vec
M = cp_mat
# Setting up the matrix of parameter weights
n_classes = M.shape[0]
n_params = n_classes**2
tpr = np.zeros(shape=(n_classes, n_params))
fpr = np.zeros(shape=(n_classes, n_params))
# Filling in the weights
for i in range(n_classes):
# Dropping row to calculate FPR
p_i = np.delete(p, i)
M_i = np.delete(M, i, 0)
start = i * (n_classes)
end = start + n_classes
fpr[i, start:end] = np.dot(p_i, M_i) / p_i.sum()
tpr[i, start:end] = M[i]
# Reshaping the off-diagonal constraints
strict = np.zeros(shape=(n_params, n_params))
#A = np.array(p.T / p_a).T
#B = np.array(M.T * A).T
B = M
for i in range(n_classes):
start = i * n_classes
end = start + n_classes
strict[start:end, start:end] = B
return tpr, fpr, strict
def __pair_constraints(self, constraints):
'''Takes the output of constraint_weights() and returns a matrix
of the pairwise constraints
'''
# Setting up the preliminaries
tprs = np.array([c[0] for c in constraints])
fprs = np.array([c[1] for c in constraints])
strict = np.array([c[2] for c in constraints])
n_params = tprs.shape[2]
n_classes = tprs.shape[1]
n_groups = self.n_groups
if n_groups > 2:
group_combos = list(combinations(range(n_groups), 2))[:-1]
else:
group_combos = [(0, 1)]
n_pairs = len(group_combos)
# Setting up the empty matrices
tpr_cons = np.zeros(shape=(n_pairs,
n_groups,
n_classes,
n_params))
fpr_cons = np.zeros(shape=(n_pairs,
n_groups,
n_classes,
n_params))
strict_cons = np.zeros(shape=(n_pairs,
n_groups,
n_params,
n_params))
# Filling in the constraint comparisons
for i, c in enumerate(group_combos):
# Getting the original diffs for flipping
diffs = self.cp_mats[c[0]] - self.cp_mats[c[1]]
tpr_flip = np.sign(np.diag(diffs)).reshape(-1, 1)
fpr_flip = np.sign(np.sum(fprs[c[0]], 1) - np.sum(fprs[c[1]], 1))
fpr_flip = fpr_flip.reshape(-1, 1)
fpr_flip[np.where(fpr_flip == 0)] = 1
# Filling in the constraints
tpr_cons[i, c[0]] = tpr_flip * tprs[c[0]]
tpr_cons[i, c[1]] = tpr_flip * -1 * tprs[c[1]]
fpr_cons[i, c[0]] = fpr_flip * fprs[c[0]]
fpr_cons[i, c[1]] = fpr_flip * -1 * fprs[c[1]]
strict_cons[i, c[0]] = strict[c[0]]
strict_cons[i, c[1]] = -1 * strict[c[1]]
# Filling in the norm constraints
one_cons = np.zeros(shape=(n_groups * n_classes,
n_groups * n_params))
cols = np.array(list(range(0,
n_groups * n_classes**2,
n_classes)))
cols = cols.reshape(n_groups, n_classes)
i = 0
for c in cols:
for j in range(n_classes):
one_cons[i, c + j] = 1
i += 1
# Reshaping the arrays
tpr_cons = np.concatenate([np.hstack(m) for m in tpr_cons])
fpr_cons = np.concatenate([np.hstack(m) for m in fpr_cons])
strict_cons = np.concatenate([np.hstack(m) for m in strict_cons])
return tpr_cons, fpr_cons, strict_cons, one_cons
def adjust_new(self,
goal='odds',
loss='micro',
round=4,
slack=0,
cv=False,
seed=None,
shuffle=False,
summary=False):
"""Adjusts predictions to satisfy a fairness constraint.
Parameters
----------
goal : {'odds', 'opportunity', 'strict'}, default 'odds'
The constraint to be satisifed. Equalized odds sets the TPR and FPR rates
equal. Opportunity only sets TPR to be equal. Strict sets all off-diagonal
entries of the confusion matrix to be equal, and may not create a feasible
linear program.
loss : {'0-1'} default '0-1'
The loss function to optimize in expectation
round : int, default 4
Decimal places for rounding results.
summary : bool, default True
Whether to print post-adjustment false-positive and true-positive \
rates for each group.
Returns
-------
(optional) optima : dict
The optimal loss and ROC coordinates after adjustment.
"""
# note: self.p_vecs contains the joint group probabilities of y and a
# self.cp_mats contains the probabilities of y^ given y and a
# self.cp_mat contains the probabilities of y^ given y
# the above should be added to the attributes doc string
self.goal = goal
# Deciding whether to get predictions with cross-validation or by
# running the adjustment for the whole dataset
if not cv:
if loss == 'micro':
loss_coefs = self.get_0_1_loss_coefs()
elif loss == 'macro':
loss_coefs = self.get_pya_weighted_01_loss_coefs()
else:
raise ValueError('Loss type %s not recognized' %loss)
# Create a n_constraints by
# n_classes (y~) by n_classes (y^) by n_groups matrix of
# constraints, adding in additional constraints as needed
# to the first dim
if goal == 'odds':
cons_mat = self.get_equal_odds_constraints()
elif goal == 'opportunity':
cons_mat = self.get_equal_opp_constraints()
elif goal == 'strict':
cons_mat = self.get_strict_constraints()
elif goal == 'demographic_parity':
cons_mat = self.get_demographic_parity_constraints()
elif goal == 'positive_predictive_parity':
cons_mat = self.get_pred_parity_constraints(type_='positive')
elif goal == 'strict_predictive_parity':
cons_mat = self.get_pred_parity_constraints(type_='strict')
else:
raise ValueError('Fairness type/goal %s not recognized' %goal)
# Add in constraint for derived probabilities to sum to one
# for every fixed group and (true) class they should be
# normalized, so one constraint for every (group, class) pair
cons_norm = np.zeros(
(self.n_groups * self.n_classes,
self.n_classes, self.n_classes, self.n_groups)
)
for con_idx in range(cons_norm.shape[0]):
c = con_idx % int(self.n_classes)
g = con_idx // int(self.n_classes)
cons_norm[con_idx, c, :, g] = np.ones(self.n_classes)
cons_norm = cons_norm.reshape(cons_norm.shape[0], -1)
# cons_mat = np.concatenate([cons_mat, cons_norm], axis=0)
#print(cons_mat)
# Form the bounds of the constraints:
# For odds/opp/strict these are 0 since they are equalities.
# For the normalization constraints these are 1
# cons_bounds = np.zeros(cons_mat.shape[0])
# cons_bounds = np.ones(cons_mat.shape[0]) * slack
# cons_bounds[-cons_norm.shape[0]:] = 1 # moved to inside slack if/else statement
if slack == 0:
# all constraints are equality constraints only
cons_mat = np.concatenate([cons_mat, cons_norm], axis=0)
cons_bounds = np.ones(cons_mat.shape[0]) * slack
cons_bounds[-cons_norm.shape[0]:] = 1
self.opt = sp.optimize.linprog(c=loss_coefs,
bounds=[0, 1],
A_eq=cons_mat,
b_eq=cons_bounds)
#method='highs')
self.con_bounds = cons_bounds
self.cons = cons_mat
else:
# constraints for normalization are still equality, but
# constraints for equality across groups are relaxed (small inequalities allowed)
cons_norm_bounds = np.ones(cons_norm.shape[0])
# Have to account for abs value of the difference being within slack
# by adding a second set of constraints. AX < slack represents ax - bx < slack
# -AX < slack represents ax - bx > -slack
cons_ineq_bounds = np.ones(2 * cons_mat.shape[0]) * slack
abs_val_cons = np.concatenate([cons_mat, -cons_mat], axis=0)
self.opt = sp.optimize.linprog(c=loss_coefs,
bounds=[0, 1],
A_eq=cons_norm,
b_eq=cons_norm_bounds,
A_ub=abs_val_cons,
b_ub=cons_ineq_bounds)
#method='highs')
# tbh not sure if any of this really makes sense to do sense the
# attributes will vary based on the inputs-does anything actually
# need to access the bounds after running?
self.con_bounds = cons_norm_bounds
self.con_ineq_bounds = cons_ineq_bounds
self.con = abs_val_cons
self.con_norm = cons_norm_bounds
if self.opt.status == 0:
# Reshaping the solution
y_derived = self.opt.x.reshape([self.n_classes,
self.n_classes,
self.n_groups])
self.y_d = y_derived
W = np.einsum('ijk, jlk->ilk',
self.cp_mats_t.transpose((1, 0, 2)),
y_derived)
self.m = np.array([y_derived[:, :, i]
for i in range(self.n_groups)])
# Getting the new cp matrices
self.new_cp_mats = np.array([np.dot(self.cp_mats[i], self.m[i])
for i in range(self.n_groups)])
# Calculating group-specific ROC scores from the new parameters
self.rocs = tools.parmat_to_roc(self.m,
self.p_vecs,
self.cp_mats)
self.loss = 1 - np.sum(self.p_vecs * self.rocs[:, :, 1])
self.macro_loss = 1 - np.mean(self.rocs[:, :, 1])
preds_as_probs = tools.cat_to_probs(self.y,
self.a,
self.new_cp_mats)
self.brier_score = tools.brier_score(self.y,
preds_as_probs)
else:
print('\nBalancing failed: Linear program is infeasible.\n')
self.m = np.nan
self.rocs = np.nan
self.loss = np.nan
self.macro_loss = np.nan
else:
# Getting the predictions with cross validation
preds = tools.cv_predict(self.y,
self.y_,
self.a,
goal=goal,
loss=loss,
shuffle=shuffle,
seed=seed)
# Resetting some class attributes
self.y = preds.y.values
self.y_ = preds.y_.values
self.a = preds.a.values
self.yt = preds.yt.values
# Getting the new CP matrices for Y~
group_ids = [np.where(preds.a == g) for g in self.groups]
self.m = np.array([tools.cp_mat(self.y_[ids],
self.yt[ids])
for ids in group_ids])
self.new_cp_mats = np.array([tools.cp_mat(self.y[ids],
self.yt[ids])
for ids in group_ids])
# Calculating group-specific ROC scores from the new parameters
self.rocs = np.array([tools.cpmat_to_roc(self.p_y_a[i],
self.new_cp_mats[i])
for i in range(self.n_groups)])
self.loss = 1 - np.sum(self.p_vecs * self.rocs[:, :, 1])
self.macro_loss = 1 - np.mean(self.rocs[:, :, 1])
preds_as_probs = tools.cat_to_probs(self.y_,
self.a,
self.m)
self.brier_score = tools.brier_score(self.y,
preds_as_probs)
if summary:
self.summary(org=False)
def get_0_1_loss_coefs(self):
coefs = np.zeros((self.n_classes, self.n_classes, self.n_groups))
for c in range(self.n_classes):
# Form |C| X |A| vector of joint probabilities of y and a
# for summation over the mismatches between derived y and
# true labels
pred_mismatch = np.ones((self.n_classes, 1))
pred_mismatch[c] = 0
p = self.p_vecs.transpose() * pred_mismatch
# matrix product across first two dimensions
coefs[:, c, :] = np.einsum('ijk,jk->ik', self.cp_mats_t, p)
return coefs.flatten()
def get_pya_weighted_01_loss_coefs(self):
return -self.cp_mats_t.flatten()
def get_equal_odds_constraints(self):
# equal opportunity constrains TPR to be equal
tpr_cons = self.get_equal_opp_constraints()
# constrain FPR to be equal
fpr_cons = self.get_fpr_cons()
equal_odds_cons = np.concatenate([tpr_cons, fpr_cons], axis=0)
return equal_odds_cons
def get_fpr_cons(self):