def test_theil_boundaries(self): np.random.seed(1) # test bounds metric = BinaryFairnessMetrics.TheilIndex() my_dict = {} for _ in range(1000): random = np.random.choice([0, 1], 10) if str(random) in my_dict: continue else: my_dict[str(random)] = (random[:5], random[5:]) with self.assertWarns( RuntimeWarning): # division by 0 in certain instances. # enumerate through all the combinations # test theil as well results_thiel = [] vals = [] for y_true, y_pred in my_dict.values(): vals.append([y_true, y_pred]) results_thiel.append(metric.get_score(y_true, y_pred)) assert min(results_thiel) == 0 assert max(results_thiel) == np.log(5)
def test_theil_normal_invalid(self): # Metric metric = BinaryFairnessMetrics.TheilIndex() y_true = [0, 1, 1, 0, 1, 1, 1, 0, 1, -1] y_pred = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] with self.assertRaises(ValueError): metric.get_score(y_true, y_pred)
def test_theil_normal_list(self): # Metric metric = BinaryFairnessMetrics.TheilIndex() y_true = [0, 1, 1, 0, 1, 1, 1, 0, 1, 0] y_pred = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] score = metric.get_score(y_true, y_pred) assert isinstance(score, float) assert np.isclose(score, 0.413, atol=0.01)
def test_theil_normal_df(self): my_df = pd.DataFrame.from_dict({ 'y_true': [0, 1, 1, 0, 1, 1, 1, 0, 1, 0], 'y_pred': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] }) # Metric metric = BinaryFairnessMetrics.TheilIndex() # Score score = metric.get_score(my_df['y_true'], my_df['y_pred']) assert isinstance(score, float) assert np.isclose(score, 0.413, atol=0.01)