From 3a39c83b0e2ac047ffcce41047c7908bfa543022 Mon Sep 17 00:00:00 2001 From: plankington Date: Fri, 21 Jan 2022 10:14:00 -0600 Subject: [PATCH] Fixed computation of coefficient of variation in classification_metrics (#288) c_v = sqrt(2*GE(alpha=2)) Signed-off-by: Eric Augustine Co-authored-by: Samuel Hoffman --- .../tests/testthat/test-classification.R | 4 ++-- aif360/explainers/metric_json_explainer.py | 8 ++++---- aif360/metrics/classification_metric.py | 16 ++++++++-------- aif360/sklearn/metrics/metrics.py | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/aif360/aif360-r/tests/testthat/test-classification.R b/aif360/aif360-r/tests/testthat/test-classification.R index dc1d7838..afec4348 100644 --- a/aif360/aif360-r/tests/testthat/test-classification.R +++ b/aif360/aif360-r/tests/testthat/test-classification.R @@ -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) diff --git a/aif360/explainers/metric_json_explainer.py b/aif360/explainers/metric_json_explainer.py index fa58579d..aae8225b 100644 --- a/aif360/explainers/metric_json_explainer.py +++ b/aif360/explainers/metric_json_explainer.py @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/aif360/metrics/classification_metric.py b/aif360/metrics/classification_metric.py index 0e881b44..6cb9799b 100644 --- a/aif360/metrics/classification_metric.py +++ b/aif360/metrics/classification_metric.py @@ -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 @@ -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 @@ -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): diff --git a/aif360/sklearn/metrics/metrics.py b/aif360/sklearn/metrics/metrics.py index 4b0c3ab3..a34003ad 100644 --- a/aif360/sklearn/metrics/metrics.py +++ b/aif360/sklearn/metrics/metrics.py @@ -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: @@ -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?