Last active
June 24, 2020 23:16
-
-
Save michelkana/428d8937ae1e802c1dd505d57ed68f0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def statistical_parity(B, m, fairness_threshold=.01): | |
if len(B) != len(m): | |
raise ValueError('Input arrays do not have same number of entries') | |
indices_pos_class, = np.where(B == 1) | |
indices_neg_class, = np.where(B == 0) | |
outcomes_pos = m[indices_pos_class] | |
outcomes_neg = m[indices_neg_class] | |
if len(outcomes_pos) == 0: | |
return None | |
if len(outcomes_neg) == 0: | |
return None | |
stats_parity = np.abs(len(np.where(outcomes_pos == 1)) / | |
len(outcomes_pos) - | |
len(np.where(outcomes_neg == 1)) / | |
len(outcomes_neg)) | |
if abs(stats_parity) < fairness_threshold: | |
print("The model is FAIR on Black with statistical parity {}".format(round(stats_parity,5))) | |
else: | |
print("The model is NOT FAIR on Black with statistical parity {}".format(round(stats_parity,5))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment