Package: aequitas
Aequitas: Bias Auditing & Fair ML Toolkitaequitasis an open-source bias auditing and Fair ML toolkit for data scientists, machine learning researchers, and policymakers. The objective of this package is to provide an easy-to-use and transparent tool for auditing predictors, as well as experimenting with Fair ML methods in binary classification settings.📥 Installationpip install aequitasorpip install git+https://github.com/dssg/aequitas.git🔍 Quickstart on Bias AuditingTo perform a bias audit, you need a pandasDataFramewith the following format:labelscoresens_attr_1sens_attr_2...sens_attr_N000AFY101CFN211BTN...N10ETYwherelabelis the target variable for your prediction task andscoreis the model output.
Only one sensitive attribute is required; all must be inCategoricalformat.fromaequitasimportAuditaudit=Audit(df)To obtain a summary of the bias audit, run:# Select the fairness metric of interest for your datasetaudit.summary_plot(["tpr","fpr","pprev"])We can also observe a single metric and sensitive attribute:audit.disparity_plot(attribute="sens_attr_2",metrics=["fpr"])🧪 Quickstart on Fair ML ExperimentingTo perform an experiment, a dataset is required. It must have a label column, a sensitive attribute column, and features.fromaequitas.flowimportDefaultExperimentexperiment=DefaultExperiment(dataset,label="label",s="sensitive_attribute")experiment.run()Several aspects of an experiment (e.g., algorithms, number of runs, dataset splitting) can be configured individually.🧠 Quickstart on Method TrainingAssuming anaequitas.flow.Dataset, it is possible to train methods and use their functionality depending on the type of algorithm (pre-, in-, or post-processing).For pre-processing methods:fromaequitas.flow.methods.preprocessingimportPrevalenceSamplingsampler=PrevalenceSampling()sampler.fit(dataset.train.X,dataset.train.y,dataset.train.s)X_sample,y_sample,s_sample=sampler.transform(dataset.train.X,dataset.train.y,dataset.train.s)for in-processing methods:fromaequitas.flow.methods.inprocessingimportFairGBMmodel=FairGBM()model.fit(X_sample,y_sample,s_sample)scores_val=model.predict_proba(dataset.validation.X,dataset.validation.y,dataset.validation.s)scores_test=model.predict_proba(dataset.test.X,dataset.test.y,dataset.test.s)for post-processing methods:fromaequitas.flow.methods.postprocessingimportBalancedGroupThresholdthreshold=BalancedGroupThreshold("top_pct",0.1,"fpr")threshold.fit(dataset.validation.X,scores_val,dataset.validation.y,dataset.validation.s)corrected_scores=threshold.transform(dataset.test.X,scores_test,dataset.test.s)With this sequence, we would sample a dataset, train a FairGBM model, and then adjust the scores to have equal FPR per group (achieving Predictive Equality).📜 Features of the ToolkitMetrics: Audits based on confusion matrix-based metrics with flexibility to select the more important ones depending on use-case.Plotting options: The major outcomes of bias auditing and experimenting offer also plots adequate to different user objectives.Fair ML methods: Interface and implementation of several Fair ML methods, including pre-, in-, and post-processing methods.Datasets: Two "families" of datasets included, namedBankAccountFraudandFolkTables.Extensibility: Adapted to receive user-implemented methods, with intuitive interfaces and method signatures.Reproducibility: Option to save artifacts of Experiments, from the transformed data to the fitted models and predictions.Modularity: Fair ML Methods and default datasets can be used individually or integrated in anExperiment.Hyperparameter optimization: Out of the box integration and abstraction ofOptuna's hyperparameter optimization capabilities for experimentation.Fair ML MethodsWe support a range of methods designed to address bias and discrimination in different stages of the ML pipeline.TypeMethodDescriptionPre-processingData RepairerTransforms the data distribution so that a given feature distribution is marginally independent of the sensitive attribute, s.Label FlippingFlips the labels of a fraction of the training data according to the Fair Ordering-Based Noise Correction method.Prevalence SamplingGenerates a training sample with controllable balanced prevalence for the groups in dataset, either by undersampling or oversampling.UnawarenessRemoves features that are highly correlated with the sensitive attribute.MassagingFlips selected labels to reduce prevalence disparity between groups.In-processingFairGBMNovel method where a boosting trees algorithm (LightGBM) is subject to pre-defined fairness constraints.Fairlearn ClassifierModels from the Fairlearn reductions package. Possible parameterization for ExponentiatedGradient and GridSearch methods.Post-processingGroup ThresholdAdjusts the threshold per group to obtain a certain fairness criterion (e.g., all groups with 10% FPR)Balanced Group ThresholdAdjusts the threshold per group to obtain a certain fairness criterion, while satisfying a global constraint (e.g., Demographic Parity with a global FPR of 10%)Fairness Metricsaequitasprovides the value of confusion matrix metrics for each possible value of the sensitive attribute columns To calculate fairness metrics. The cells of the confusion metrics are:CellSymbolDescriptionFalse Positive$FP_g$The number of entities of the group with $\hat{Y}=1$ and $Y=0$False Negative$FN_g$The number of entities of the group with $\hat{Y}=0$ and $Y=1$True Positive$TP_g$The number of entities of the group with $\hat{Y}=1$ and $Y=1$True Negative$TN_g$The number of entities of the group with $\hat{Y}=0$ and $Y=0$From these, we calculate several metrics:MetricFormulaDescriptionAccuracy$Acc_g = \cfrac{TP_g + TN_g}{|g|}$The fraction of correctly predicted entities withing the group.True Positive Rate$TPR_g = \cfrac{TP_g}{TP_g + FN_g}$The fraction of true positives within the label positive entities of a group.True Negative Rate$TNR_g = \cfrac{TN_g}{TN_g + FP_g}$The fraction of true negatives within the label negative entities of a group.False Negative Rate$FNR_g = \cfrac{FN_g}{TP_g + FN_g}$The fraction of false negatives within the label positive entities of a group.False Positive Rate$FPR_g = \cfrac{FP_g}{TN_g + FP_g}$The fraction of false positives within the label negative entities of a group.Precision$Precision_g = \cfrac{TP_g}{TP_g + FP_g}$The fraction of true positives within the predicted positive entities of a group.Negative Predictive Value$NPV_g = \cfrac{TN_g}{TN_g + FN_g}$The fraction of true negatives within the predicted negative entities of a group.False Discovery Rate$FDR_g = \cfrac{FP_g}{TP_g + FP_g}$The fraction of false positives within the predicted positive entities of a group.False Omission Rate$FOR_g = \cfrac{FN_g}{TN_g + FN_g}$The fraction of false negatives within the predicted negative entities of a group.Predicted Positive$PP_g = TP_g + FP_g$The number of entities within a group where the decision is positive, i.e., $\hat{Y}=1$.Total Predictive Positive$K = \sum PP_{g(a_i)}$The total number of entities predicted positive across groups defined by $A$Predicted Negative$PN_g = TN_g + FN_g$The number of entities within a group where the decision is negative, i.e., $\hat{Y}=0$Predicted Prevalence$Pprev_g=\cfrac{PP_g}{|g|}=P(\hat{Y}=1 | A=a_i)$The fraction of entities within a group which were predicted as positive.Predicted Positive Rate$PPR_g = \cfrac{PP_g}{K} = P(A=A_i | \hat{Y}=1)$The fraction of the entities predicted as positive that belong to a certain group.These are implemented in theGroupclass. With theBiasclass, several fairness metrics can be derived by different combinations of ratios of these metrics.📔Example NotebooksNotebookDescriptionAudit a Model's PredictionsCheck how to do an in-depth bias audit with the COMPAS example notebook.Correct a Model's PredictionsCreate a dataframe to audit a specific model, and correct the predictions with group-specific thresholds in the Model correction notebook.Train a Model with Fairness ConsiderationsExperiment with your own dataset or methods and check the results of a Fair ML experiment.Further documentationYou can find the toolkit documentationhere.For more examples of the python library and a deep dive into concepts of fairness in ML, see ourTutorialpresented on KDD and AAAI. Visit also theAequitas project website.Citing AequitasIf you use Aequitas in a scientific publication, we would appreciate citations to the following paper:Pedro Saleiro, Benedict Kuester, Abby Stevens, Ari Anisfeld, Loren Hinkson, Jesse London, Rayid Ghani, Aequitas: A Bias and Fairness Audit Toolkit,  arXiv preprint arXiv:1811.05577 (2018). (PDF)@article{2018aequitas,title={Aequitas: A Bias and Fairness Audit Toolkit},author={Saleiro, Pedro and Kuester, Benedict and Stevens, Abby and Anisfeld, Ari and Hinkson, Loren and London, Jesse and Ghani, Rayid},journal={arXiv preprint arXiv:1811.05577},year={2018}}Back to top