Classification Metrics#

[1]:
import numpy as np

from langfair.metrics.classification import ClassificationMetrics

1. Introduction#

Large language models (LLMs) used in classification use cases should be assessed for group fairness (if applicable). Similar to traditional person-level classification challenges in machine learning, these use cases present the risk of allocational harms. LangFair offers the following classification fairness metrics from the LLM fairness literature:

2. Assessment#

[2]:
# Simulate dataset for this example. In practice, users should replace this data with predicted classes generated by the LLM,
# corresponding ground truth values, and corresponding protected attribute group data.
sample_size = 10000
groups = np.random.binomial(n=1, p=0.5, size=sample_size)
y_pred = np.random.binomial(n=1, p=0.3, size=sample_size)
y_true = np.random.binomial(n=1, p=0.3, size=sample_size)

Classification Metrics#


ClassificationMetrics() - Pairwise classification fairness metrics (class)

Class parameters:

  • metric_type - ({‘all’, ‘assistive’, ‘punitive’, ‘representation’}, default=’all’) Specifies which metrics to use.

Methods:

  1. evaluate - Returns min, max, range, and standard deviation of metrics across protected attribute groups.

    Method Parameters:

    • groups - (array-like) Group indicators. Must contain exactly two unique values.

    • y_pred - (array-like) Binary model predictions. Positive and negative predictions must be 1 and 0, respectively.

    • y_true - (array-like) Binary labels (ground truth values). Positive and negative labels must be 1 and 0, respectively.

    • ratio - (boolean) Indicates whether to compute the metric as a difference or a ratio

    Returns:

    • Dictionary containing fairness metric values (Dictionary).

Generate an instance of class ClassificationMetrics using default metric_type='all', which includes “assistive”, “punitive”, and “representation” metrics.

[3]:
cm = ClassificationMetrics(metric_type='all')
[4]:
# Metrics expressed as ratios (target value of 1)
cm.evaluate(groups=groups, y_pred=y_pred, y_true=y_true, ratio=True)
[4]:
{'FalseNegativeRateParity': 0.9683960547735326,
 'FalseOmissionRateParity': 0.9682772917805723,
 'FalsePositiveRateParity': 0.9832027144990514,
 'FalseDiscoveryRateParity': 0.9750294817188464,
 'PredictedPrevalenceRateParity': 1.010318056277584}
[5]:
# Metrics expressed as differences (target value of 0)
cm.evaluate(groups=groups, y_pred=y_pred, y_true=y_true, ratio=False)
[5]:
{'FalseNegativeRateParity': 0.022435421009698087,
 'FalseOmissionRateParity': 0.009568167034658404,
 'FalsePositiveRateParity': 0.005089653684952955,
 'FalseDiscoveryRateParity': 0.01776013575685509,
 'PredictedPrevalenceRateParity': 0.0030867911196673647}