Skip to content

Instantly share code, notes, and snippets.

@michelkana
Last active June 24, 2020 23:16
Show Gist options
  • Save michelkana/2fa15dd33708c26409460e840a75bdb8 to your computer and use it in GitHub Desktop.
Save michelkana/2fa15dd33708c26409460e840a75bdb8 to your computer and use it in GitHub Desktop.
import numpy as np
def disparate_impact(B, m, fairness_threshold=.8):
if len(B) != len(m):
raise ValueError('Input arrays do not have same number of entries')
# "positive class" are those where predictions = Charge Off
# "majority class" are those where protected class status = 1
indices_pos_class, = np.where(B == 1)
outcomes_pos = m[indices_pos_class]
if len(np.where(outcomes_pos == 1)) == 0:
return None, None
value_discrim = len(np.where(outcomes_pos == 0)) / len(
np.where(outcomes_pos == 1))
if value_discrim <= fairness_threshold:
is_discriminatory = True
print("The model is DISCRIMINATING on Black with level {}".format(round(1-value_discrim,5)))
else:
print("The model is NOT DISCRIMINATING on Black with level {}".format(round(1-value_discrim,5)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment