Fast postprocessing of any score-based predictor to meet fairness criteria.
The error-parity
package can achieve strict or relaxed fairness constraint fulfillment,
which can be useful to compare ML models at equal fairness levels.
Install package from PyPI:
pip install error-parity
Or, for development, you can clone the repo and install from local sources:
git clone https://github.com/socialfoundations/error-parity.git
pip install ./error-parity
See detailed example notebooks under the examples folder.
from error_parity import RelaxedThresholdOptimizer
# Given any trained model that outputs real-valued scores
fair_clf = RelaxedThresholdOptimizer(
predictor=lambda X: model.predict_proba(X)[:, -1], # for sklearn API
# predictor=model, # use this for a callable model
constraint="equalized_odds",
tolerance=0.05, # fairness constraint tolerance
)
# Fit the fairness adjustment on some data
# This will find the optimal _fair classifier_
fair_clf.fit(X=X, y=y, group=group)
# Now you can use `fair_clf` as any other classifier
# You have to provide group information to compute fair predictions
y_pred_test = fair_clf(X=X_test, group=group_test)
Given a callable score-based predictor (i.e., y_pred = predictor(X)
), and some (X, Y, S)
data to fit, RelaxedThresholdOptimizer
will:
- Compute group-specific ROC curves and their convex hulls;
- Compute the
r
-relaxed optimal solution for the chosen fairness criterion (using cvxpy); - Find the set of group-specific binary classifiers that match the optimal solution found.
- each group-specific classifier is made up of (possibly randomized) group-specific thresholds over the given predictor;
- if a group's ROC point is in the interior of its ROC curve, partial randomization of its predictions may be necessary.
We welcome community contributions for cvxpy implementations of other fairness constraints.
Currently implemented fairness constraints:
- equality of odds (Hardt et al., 2016);
- i.e., equal group-specific TPR and FPR;
- use
constraint="equalized_odds"
;
- equal opportunity;
- i.e., equal group-specific TPR;
- use
constraint="true_positive_rate_parity"
;
- predictive equality;
- i.e., equal group-specific FPR;
- use
constraint="false_positive_rate_parity"
;
Road-map:
- demographic parity;
- i.e., equal group-specific predicted prevalence;
This repository contains code and supplementary materials for the following preprint:
André F. Cruz and Moritz Hardt. "Unprocessing Seven Years of Algorithmic Fairness." arXiv preprint, 2023.