Skip to content

Instantly share code, notes, and snippets.

@michelkana
Last active June 24, 2020 23:16
Show Gist options
  • Save michelkana/428d8937ae1e802c1dd505d57ed68f0d to your computer and use it in GitHub Desktop.
Save michelkana/428d8937ae1e802c1dd505d57ed68f0d to your computer and use it in GitHub Desktop.
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