The fairdo.metrics package provides a collection of metrics [1] to measure fairness/discrimination in datasets.
The metrics are divided into following subpackages:
fairdo.metrics.group: This subpackage provides metrics to measure group fairness.
fairdo.metrics.individual: This subpackage provides metrics to measure individual fairness.
fairdo.metrics.dependence: This subpackage provides metrics to measure the (in)dependency between two variables.
fairdo.metrics.penalty: This subpackage contains specialized penalty functions to penalize fairness metrics to guarantee certain constraints such as group coverage [2].
This module contains functions to calculate the dependency, correlation, association or any other relationship
between two variables.
In the fairness context, the dependency between the target variable and the protected attribute(s) is of interest.
Let \(y\) be the target variable and \(z\) be the protected attribute(s), then some kind of relationship between
these two variables is calculated using a function \(f\): \(f(y, z)\).
Calculates the dependency between y with each z[:,i] using the specified dependency_function.
Aggregates the dependency scores using the agg function.
Let \(f\) be the dependency function, \(y\) be the target variable,
and \(z\) be the protected attributes, then the dependency score is calculated as (pythonic notation):
y (np.array, shape (n_samples,)) – Flattened binary array of shape (n_samples,), can be a prediction or the truth label.
z (np.array, shape (n_samples, n_protected_attributes)) – Array of shape (n_samples, n_protected_attributes), represents the protected attributes.
dependency_function (callable, optional) – Function to compute the dependency between y and each protected attribute.
Default is normalized_mutual_info_score.
agg (callable, optional) – Aggregation function to combine the dependency scores. Default is np.max.
positive_label (int, optional) – Label considered as positive. Default is 1.
**kwargs – Additional keyword arguments. These are not currently used.
Calculate the dual total correlation [5] for more than two variables.
Given a set of \(m\) categorical variables \(X = (X_1, X_2, \ldots, X_m)\), it is given by:
where \(H(X_1, X_2, \ldots, X_m)\) is the joint entropy and
\(H(X_i | X_1, X_2, \ldots, X_{i-1}, X_{i+1}, \ldots, X_m)\)
is the conditional entropy of \(X_i\) given all other variables.
Parameters:
*arrays (np.array) – Arrays of shape (n_samples,) containing the labels.
Returns:
The dual total correlation of the categorical variables.
Calculate the mutual information [12] between two arrays.
The protected attribute z can be binary or non-binary.
Mutual information is a measure of the mutual dependence between two variables.
It quantifies the “amount of information” (in units such as bits) obtained
about one random variable, by observing the other random variable.
Higher values indicate a higher dependency between the two variables. It is defined as:
where \(p(Y, Z)\) is the joint probability distribution of \(Y\) and \(Z\), and \(p(Y)\) and
\(p(Z)\) are the respective marginal probability distributions.
Parameters:
y (np.array (n_samples,)) – Flattened array, can be a prediction or the truth label. Discrete values.
z (np.array (n_samples,)) – Flattened array of the same shape as y. Discrete values.
bins (int, optional) – Number of bins for discretization. Default is 2.
**kwargs – Additional keyword arguments. These are not currently used.
Calculate the normalized mutual information between two arrays.
The protected attribute z can be binary or non-binary.
Normalized mutual information is a normalization of the Mutual Information (MI) score
to scale the results between 0 (no mutual information, independent variables) and 1
(perfect correlation). The function handles any warning by ignoring them.
The formula is given by:
Compute the normalized mutual information [11] for multiple non-binary protected attributes.
This function calculates the normalized mutual information between y and each
protected attribute in z, and then aggregates these scores using the specified agg function.
Let \(y\) be the target variable and \(z\) be the protected attributes, then the normalized mutual
information score for multiple protected attributes is calculated as (pythonic notation):
Calculate the O-information [6] of multiple categorical variables.
The O-information is the difference between the total correlation and the dual total correlation:
where \(\text{cov}(Y, Z)\) is the covariance between \(Y\) and \(Z\),
and \(\sigma_Y\) and \(\sigma_Z\) are the respective standard deviations.
Parameters:
y (np.array) – Flattened array, can be a prediction or the truth label.
z (np.array) – Flattened array of the same shape as y.
**kwargs – Additional keyword arguments. These are not currently used.
Returns:
The Pearson correlation coefficient between y and z.
Return type:
float
Notes
The Pearson correlation coefficient measures the linear relationship between two variables.
The calculation of the Pearson correlation coefficient is not affected by scaling,
and it ranges from -1 to 1. A value of 1 implies a perfect positive correlation,
while a value of -1 implies a perfect negative correlation.
Calculate the absolute value of the Pearson correlation coefficient between two arrays.
The protected attribute z can be binary or non-binary.
It is given by:
The Randomized Dependence Coefficient by
David Lopez-Paz, Philipp Hennig, Bernhard Schoelkopf [7].
According to the paper, the coefficient should be relatively insensitive to
the settings of the f, k, and s parameters.
Parameters:
y (np.array (n_samples,) or (n_samples, n_variables)) –
z (np.array (n_samples,) or (n_samples, n_variables)) –
f (callable) – function to use for random projection
k (int) – number of random projections to use
s (numeric) – scale parameter
n (int) – number of times to compute the RDC and
return the median (for stability)
Returns:
RDC between y and z.
Return type:
float
Notes
Implementation by Gary Doran and taken from: garydoranjr/rdc
Calculate the total correlation (multi-information) of multiple categorical variables [13][4].
Given a set of \(m\) categorical variables \(X = (X_1, X_2, \ldots, X_m)\), the total correlation is:
Calculate the Disparate Impact ratio.
The protected attribute z must be binary.
This function computes the ratio of probabilities of positive outcomes for
the unprivileged group to the privileged group. A value of 1 indicates
fairness, while a value < 1 indicates discrimination towards the
unprivileged group. A value of > 1 would indicate discrimination towards
the privileged group.
Parameters:
y (np.array) – Flattened binary array, can be the prediction or the truth label.
z (np.array) – Flattened binary array of shape y, represents the protected attribute.
positive_label (int, optional) – Label considered as positive. Default is 1.
privileged_group (int, optional) – Label considered as privileged. Default is 1.
Calculate the difference in objective Disparate Impact ratio.
The protected attribute z must be binary.
This function computes the difference between 1 and the Disparate Impact
ratio. A value of 0 indicates fairness. A positive value indicates
discrimination towards the unprivileged group. A negative value indicates
discrimination towards the privileged group.
Parameters:
y (np.array) – Flattened binary array, can be the prediction or the truth label.
z (np.array) – Flattened binary array of shape y, represents the protected attribute.
positive_label (int, optional) – Label considered as positive. Default is 1.
privileged_group (int, optional) – Label considered as privileged. Default is 1.
Returns:
The difference in objective Disparate Impact ratio.
Calculate the objective Disparate Impact ratio.
The protected attribute z must be binary.
This function computes the absolute difference between 1 and the Disparate
Impact ratio. It can be used as an objective function to minimize
discrimination towards the unprivileged group (and the privileged group).
Lower values indicate less discrimination.
Parameters:
y (np.array) – Flattened binary array, can be the prediction or the truth label.
z (np.array) – Flattened binary array of shape y, represents the protected attribute.
positive_label (int, optional) – Label considered as positive. Default is 1.
privileged_group (int, optional) – Label considered as privileged. Default is 1.
Compute the difference in Equality of Opportunity [1] between
the privileged group and the unprivileged group.
Equality of Opportunity [1] is a fairness metric
that measures the difference in true positive rates between the privileged and unprivileged groups.
This function returns a float representing that difference.
A value of 0 indicates perfect fairness, positive values indicate bias
against the unprivileged group, while negative values indicate
bias against the privileged group.
y_true (numpy.array) – The true binary labels as a flattened array.
y_pred (numpy.array) – The predicted binary labels from the model.
Should be of the same shape as y_true.
z (numpy.array) – The protected attribute as a binary array.
This array indicates the group (privileged or unprivileged) for each instance in the data.
Should be of the same shape as y_true.
positive_label (int, optional (default=1)) – The label considered as positive in the dataset.
privileged_group (int, optional (default=1)) – The label that denotes the privileged group.
If 0, the function will treat the unprivileged group as the privileged group.
Returns:
The difference in Equality of Opportunity between the privileged and unprivileged groups.
Calculate the absolute value of the statistical parity difference between all groups inside a protected attribute.
The protected attribute z can be binary or non-binary.
Returned value is aggregated with agg_group.
Parameters:
y (np.array) – Flattened binary array, can be the prediction or the truth label.
z (np.array) – Flattened array of shape y, represents the protected attribute.
Can represent non-binary protected attribute.
agg_group (callable, optional) – Aggregation function for the group. Default is np.sum.
positive_label (int, optional) – Label considered as positive. Default is 1.
privileged_group (int, optional) – Label considered as privileged. Default is 1.
Returns:
The absolute value of the statistical parity difference.
Calculate the absolute difference in statistical parity for multiple non-binary protected attributes.
Intersections from all protected attributes are considered.
Protected attributes z[i] can be binary or non-binary.
Parameters:
y (np.array) – Flattened binary array of shape (n_samples,), can be the prediction or the truth label.
z (np.array) – Array of shape (n_samples, n_protected_attributes) representing the protected attribute.
agg_group (callable, optional) – Aggregation function for the group. Default is np.sum.
Calculate the maximum of statistical parity absolute differences between all groups in a protected attribute.
The protected attribute z can be binary or non-binary.
Parameters:
y (np.array) – Flattened binary array, can be the prediction or the truth label.
z (np.array) – Flattened array of shape y, represents the protected attribute.
Can represent non-binary protected attribute.
positive_label (int, optional) – Label considered as positive. Default is 1.
privileged_group (int, optional) – Label considered as privileged. Default is 1.
Returns:
Average of the absolute value of the statistical parity differences between all groups.
Calculate the sum of statistical parity absolute differences between all groups and return the average score.
The protected attribute z can be binary or non-binary.
Parameters:
y (np.array) – Flattened binary array, can be the prediction or the truth label.
z (np.array) – Flattened array of shape y, represents the protected attribute.
Can represent non-binary protected attribute.
positive_label (int, optional) – Label considered as positive. Default is 1.
privileged_group (int, optional) – Label considered as privileged. Default is 1.
Returns:
Average of the absolute value of the statistical parity differences between all groups.
Calculate the absolute difference in statistical parity for multiple non-binary protected attributes.
Protected attributes z[i] can be binary or non-binary.
Parameters:
y (np.array) – Flattened binary array of shape (n_samples,), can be the prediction or the truth label.
z (np.array) – Array of shape (n_samples, n_protected_attributes) representing the protected attribute.
agg_attribute (callable, optional) – Aggregation function for the attribute. Default is np.sum.
agg_group (callable, optional) – Aggregation function for the group. Default is np.sum.
positive_label (int, optional) – Label considered as positive. Default is 1.
Calculate the maximum of statistical parity absolute differences between all groups in a protected attribute.
The protected attribute z can be binary or non-binary.
Parameters:
y (np.array) – Flattened binary array, can be the prediction or the truth label.
z (np.array) – Flattened array of shape y, represents the protected attribute.
Can represent non-binary protected attribute.
positive_label (int, optional) – Label considered as positive. Default is 1.
privileged_group (int, optional) – Label considered as privileged. Default is 1.
Returns:
Average of the absolute value of the statistical parity differences between all groups.
This module provides penalty functions specifically designed for fairness optimization with constraints.
The constraint in this context is that the number of data points after pre-processing should match a specified value.
A practical penalty function is relative_shortfall_penalty, which is designed to handle situations
where the number of data points is less than this specified value, and in such cases,
penalties are applied to the solutions.
Calculate the penalty for missing groups in a protected attribute.
The number of groups n_groups is used to calculate the penalty.
If agg_group is ‘max’, the penalty is 1 if any group is missing, otherwise 0.
If agg_group is ‘sum’, the penalty is the sum of the penalties for each group.
agg_attribute is used to aggregate the penalties for each protected attribute.
Parameters:
z (np.array) –
Array of shape (n_samples, n_protected_attributes) representing multiple protected attributes.
or
(n_samples,) represents one protected attribute.
Each protected attribute can consists of >2 groups.
n_groups (np.array or int) – Number of groups for each protected attribute.
agg_group (str, optional) – Aggregation function for the group. Default is ‘sum’.
agg_attribute (str, optional) – Aggregation function for the attribute. Default is ‘max’.
eps (float, optional) – Small value to add to the penalty. Default is 0.1.
Acts as an upper bound for the maximum discrimination possible
that is not a supremum. This is to ensure that missing a group
is always worse than having a group with a large discrimination.