Skip to content

Commit

Permalink
Fixed computation of coefficient of variation in classification_metri…
Browse files Browse the repository at this point in the history
…cs (#288)

c_v = sqrt(2*GE(alpha=2))

Signed-off-by: Eric Augustine <eaugustine011@gmail.com>
Co-authored-by: Samuel Hoffman <hoffman.sc@gmail.com>
  • Loading branch information
plankington and hoffmansc authored Jan 21, 2022
1 parent 1641cac commit 48acf62
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions aif360/aif360-r/tests/testthat/test-classification.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ test_that("running dataset test", {
expect_equal(cm$average_abs_odds_difference(), 0.4250, tolerance=0.000603)
expect_equal(cm$average_odds_difference(), -0.07545, tolerance=5.32e-05)
expect_equal(class(cm$binary_confusion_matrix()), "list")
expect_equal(cm$between_all_groups_coefficient_of_variation(), 0.08035, tolerance=3.65e-05)
expect_equal(cm$between_all_groups_coefficient_of_variation(), 0.0568, tolerance=3.65e-05)
expect_equal(cm$between_all_groups_generalized_entropy_index(alpha=2), 0.00161, tolerance=3.49e-06)
expect_equal(cm$between_all_groups_theil_index(), 0.0016, tolerance=8.24e-06)
expect_equal(cm$coefficient_of_variation(), 0.8040, tolerance=0.00105)
expect_equal(cm$coefficient_of_variation(), 0.5685, tolerance=0.00105)
expect_equal(cm$disparate_impact(), 1.629, tolerance=0.00063)
expect_equal(cm$equal_opportunity_difference(), -0.5)
expect_equal(cm$error_rate(), 0.45)
Expand Down
8 changes: 4 additions & 4 deletions aif360/explainers/metric_json_explainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def between_all_groups_coefficient_of_variation(self):
response = OrderedDict((
("metric", "Between All Groups Coefficient Of Variation"),
("message", outcome),
("description", "Computed as twice the square root of pairwise entropy between every pair of privileged and underprivileged groups."),
("description", "Computed as the square root of twice the pairwise entropy between every pair of privileged and underprivileged groups with alpha = 2."),
("ideal", "The ideal value of this metric is 0.") #2.0"
))
return json.dumps(response)
Expand Down Expand Up @@ -103,7 +103,7 @@ def between_group_coefficient_of_variation(self):
response = OrderedDict((
("metric", "Between Group Coefficient Of Variation"),
("message", outcome),
("description", "Computed as twice the square root of pairwise entropy between a given pair of privileged and underprivileged groups."),
("description", "Computed as the square root of twice the pairwise entropy between a given pair of privileged and underprivileged groups with alpha = 2."),
("ideal", "The ideal value of this metric is 0.0") #2.0"
))
return json.dumps(response)
Expand All @@ -123,7 +123,7 @@ def between_group_theil_index(self):
response = OrderedDict((
("metric", "Between Group Theil Index"),
("message", outcome),
("description", "Computed as the pairwise entropy between a given pair of privileged and underprivileged groups."),
("description", "Computed as the pairwise entropy between a given pair of privileged and underprivileged groups with alpha = 1."),
("ideal", "The ideal value of this metric is 0.0") #1.0"
))
return json.dumps(response)
Expand All @@ -133,7 +133,7 @@ def coefficient_of_variation(self):
response = OrderedDict((
("metric", "Coefficient Of Variation"),
("message", outcome),
("description", "Computed as twice the generalized entropy index."),
("description", "Computed as the square root of twice the generalized entropy index with alpha = 2."),
("ideal", "The ideal value of this metric is 0.0") #2.0"
))
return json.dumps(response)
Expand Down
16 changes: 8 additions & 8 deletions aif360/metrics/classification_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,10 @@ def theil_index(self):
return self.generalized_entropy_index(alpha=1)

def coefficient_of_variation(self):
r"""The coefficient of variation is two times the square root of the
r"""The coefficient of variation is the square root of two times the
:meth:`generalized_entropy_index` with :math:`\alpha = 2`.
"""
return 2 * np.sqrt(self.generalized_entropy_index(alpha=2))
return np.sqrt(2*self.generalized_entropy_index(alpha=2))

def between_group_theil_index(self):
r"""The between-group Theil index is the
Expand All @@ -789,11 +789,11 @@ def between_group_theil_index(self):
return self.between_group_generalized_entropy_index(alpha=1)

def between_group_coefficient_of_variation(self):
r"""The between-group coefficient of variation is two times the square
root of the :meth:`between_group_generalized_entropy_index` with
r"""The between-group coefficient of variation is the square
root of two times the :meth:`between_group_generalized_entropy_index` with
:math:`\alpha = 2`.
"""
return 2*np.sqrt(self.between_group_generalized_entropy_index(alpha=2))
return np.sqrt(2*self.between_group_generalized_entropy_index(alpha=2))

def between_all_groups_theil_index(self):
r"""The between-group Theil index is the
Expand All @@ -803,11 +803,11 @@ def between_all_groups_theil_index(self):
return self.between_all_groups_generalized_entropy_index(alpha=1)

def between_all_groups_coefficient_of_variation(self):
r"""The between-group coefficient of variation is two times the square
root of the :meth:`between_all_groups_generalized_entropy_index` with
r"""The between-group coefficient of variation is the square
root of two times the :meth:`between_all_groups_generalized_entropy_index` with
:math:`\alpha = 2`.
"""
return 2 * np.sqrt(self.between_all_groups_generalized_entropy_index(
return np.sqrt(2*self.between_all_groups_generalized_entropy_index(
alpha=2))

def differential_fairness_bias_amplification(self, concentration=1.0):
Expand Down
4 changes: 2 additions & 2 deletions aif360/sklearn/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def theil_index(b):
return generalized_entropy_index(b, alpha=1)

def coefficient_of_variation(b):
r"""The coefficient of variation is two times the square root of the
r"""The coefficient of variation is the square root of two times the
:func:`generalized_entropy_index` with :math:`\alpha = 2`.
Args:
Expand All @@ -601,7 +601,7 @@ def coefficient_of_variation(b):
See also:
:func:`generalized_entropy_index`
"""
return 2 * np.sqrt(generalized_entropy_index(b, alpha=2))
return np.sqrt(2 * generalized_entropy_index(b, alpha=2))


# TODO: use sample_weight?
Expand Down

0 comments on commit 48acf62

Please sign in to comment.