diff --git a/README.md b/README.md index b39ecd28..a277148b 100644 --- a/README.md +++ b/README.md @@ -14,21 +14,6 @@ FairLens is an open source Python library for automatically discovering bias and measuring fairness in data. The package can be used to quickly identify bias, and provides multiple metrics to measure fairness across a range of sensitive and legally protected characteristics such as age, race and sex. -## Core Features - -Some of the main features of Fairlens are: - -- **Measuring Bias** - FairLens can be used to measure the extent and significance of biases in datasets using a wide range of statistical distances and metrics. - -- **Sensitive Attribute and Proxy Detection** - Data Scientists may be unaware of protected or sensitive attributes in their data, and potentially hidden correlations between these columns and other non-protected columns in their data. FairLens can quickly identify sensitive columns and flag hidden correlations and the non-sensitive proxies. - -- **Visualization Tools** - FairLens has a range of tools that be used to generate meaningful and descriptive diagrams of different distributions in the dataset before delving further in to quantify them. For instance, FairLens can be used to visualize the distribution of a target with respect to different sensitive demographics, or a correlation heatmap. - -- **Fairness Scorer** - The fairness scorer is a simple tool which data scientists can use to get started with FairLens. It is designed to just take in a dataset and a target variable and to automatically generate a report highlighting hidden biases, correlations, and containing various diagrams. - - -The goal of FairLens is to enable data scientists to gain a deeper understanding of their data, and helps to to ensure fair and ethical use of data in analysis and machine learning tasks. The insights gained from FairLens can be harnessed by the [Bias Mitigation](https://www.synthesized.io/post/synthesized-mitigates-bias-in-data) feature of the [Synthesized](https://synthesized.io) platform, which is able to automagically remove bias using the power of synthetic data. - ## Bias in my data? It's very simple to quickly start understanding any biases that may be present in your data. @@ -81,6 +66,21 @@ See some of our previous blog posts for our take on bias and fairness in ML: - [Fairness and biases in machine learning and their impact on banking and insurance](https://www.synthesized.io/post/fairness-and-biases-in-machine-learning-and-their-impact-on-banking-and-insurance) - [Fairness and algorithmic biases in machine learning and recommendations to enterprise](https://www.synthesized.io/post/fairness-and-algorithmic-biases-in-machine-learning-and-recommendations) +## Core Features + +Some of the main features of Fairlens are: + +- **Measuring Bias** - FairLens can be used to measure the extent and significance of biases in datasets using a wide range of statistical distances and metrics. + +- **Sensitive Attribute and Proxy Detection** - Data Scientists may be unaware of protected or sensitive attributes in their data, and potentially hidden correlations between these columns and other non-protected columns in their data. FairLens can quickly identify sensitive columns and flag hidden correlations and the non-sensitive proxies. + +- **Visualization Tools** - FairLens has a range of tools that be used to generate meaningful and descriptive diagrams of different distributions in the dataset before delving further in to quantify them. For instance, FairLens can be used to visualize the distribution of a target with respect to different sensitive demographics, or a correlation heatmap. + +- **Fairness Scorer** - The fairness scorer is a simple tool which data scientists can use to get started with FairLens. It is designed to just take in a dataset and a target variable and to automatically generate a report highlighting hidden biases, correlations, and containing various diagrams. + + +The goal of FairLens is to enable data scientists to gain a deeper understanding of their data, and helps to to ensure fair and ethical use of data in analysis and machine learning tasks. The insights gained from FairLens can be harnessed by the [Bias Mitigation](https://www.synthesized.io/post/synthesized-mitigates-bias-in-data) feature of the [Synthesized](https://synthesized.io) platform, which is able to automagically remove bias using the power of synthetic data. + ## Installation diff --git a/docs/_static/distance.png b/docs/_static/distance.png new file mode 100644 index 00000000..141ae86e Binary files /dev/null and b/docs/_static/distance.png differ diff --git a/docs/conf.py b/docs/conf.py index 7fbac5d8..007acda8 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -53,6 +53,7 @@ "IPython.sphinxext.ipython_console_highlighting", "IPython.sphinxext.ipython_directive", "sphinx_panels", + "sphinxcontrib.bibtex", ] autosummary_generate = True @@ -63,6 +64,8 @@ panels_add_bootstrap_css = False +bibtex_bibfiles = ["refs.bib"] + # Add any paths that contain templates here, relative to this directory. templates_path = ["_templates"] @@ -110,5 +113,7 @@ "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css", ] +mathjax_path = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" + # Customization html_logo = "_static/FairLens_196x51.png" diff --git a/docs/index.rst b/docs/index.rst index 0a0a296f..675a2c46 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -11,6 +11,21 @@ FairLens is a tool to help people assess the fairness of datasets and models in .. panels:: :card: + intro-card text-center + --- + :fa:`balance-scale,text-black fa-4x,style=fa` + + Fairness and Bias + ^^^^^^^^^^^^^^^^^ + + Learn more about fairness and bias in data science and machine learning, and how we measure it in FairLens. + + +++ + + .. link-button:: user_guide/fairness + :type: ref + :text: Go + :classes: btn-block btn-secondary + --- :fa:`book,text-black fa-4x,style=fa` @@ -44,6 +59,23 @@ FairLens is a tool to help people assess the fairness of datasets and models in :text: Go :classes: btn-block btn-secondary + --- + :fa:`users,text-black fa-4x,style=fa` + + Contributing + ^^^^^^^^^^^^ + + Saw a typo in the documentation? Want to improve + existing functionalities? The contributing guidelines will guide + you through the process of improving FairLens. + + +++ + + .. link-button:: contributing + :type: ref + :text: Go + :classes: btn-block btn-secondary + .. toctree:: :maxdepth: 3 :hidden: @@ -52,39 +84,3 @@ FairLens is a tool to help people assess the fairness of datasets and models in user_guide/index reference/index contributing - - -.. overview panel -.. --- -.. :fa:`balance-scale,text-black fa-4x,style=fa` - -.. Fairness and Bias -.. ^^^^^^^^^^^^^^^^^ - -.. An introduction to fairness and bias in data science. Learn more about how you can assess the fairness of -.. your machine learning pipeline. - -.. +++ - -.. .. link-button:: user_guide/fairness -.. :type: ref -.. :text: Go -.. :classes: btn-block btn-secondary - -.. contribution panel -.. --- -.. :fa:`users,text-black fa-4x,style=fa` - -.. Contributing -.. ^^^^^^^^^^^^ - -.. Saw a typo in the documentation? Want to improve -.. existing functionalities? The contributing guidelines will guide -.. you through the process of improving FairLens. - -.. +++ - -.. .. link-button:: contributing -.. :type: ref -.. :text: Go -.. :classes: btn-block btn-secondary diff --git a/docs/refs.bib b/docs/refs.bib new file mode 100644 index 00000000..3c7b43d8 --- /dev/null +++ b/docs/refs.bib @@ -0,0 +1,22 @@ +@book{fairmlbook, + title={Fairness and Machine Learning}, + author={Solon Barocas and Moritz Hardt and Arvind Narayanan}, + publisher={fairmlbook.org}, + url={http://www.fairmlbook.org}, + year = {2019} +} + +@article{gouic2020projection, + title={Projection to fairness in statistical learning}, + author={Gouic, Thibaut Le and Loubes, Jean-Michel and Rigollet, Philippe}, + journal={arXiv preprint arXiv:2005.11720}, + year={2020} +} + +@misc{compas, + title={How We Analyzed the COMPAS Recidivism Algorithm}, + author={Jeff Larson, Surya Mattu, Lauren Kirchner and Julia Angwin}, + journal={ProPublica}, + url={https://www.propublica.org/article/how-we-analyzed-the-compas-recidivism-algorithm}, + year={2016} +} diff --git a/docs/user_guide/fairness.rst b/docs/user_guide/fairness.rst index ef1a8e14..d78c5c83 100644 --- a/docs/user_guide/fairness.rst +++ b/docs/user_guide/fairness.rst @@ -1,125 +1,65 @@ -Fairness +Overview ======== -This is a general guide to assessing fairness in supervised learning tasks (classification, regression) -using structural datasets. - -Literature Review ------------------ - In most supervised learning problems, a model is trained on a set of features :math:`X`, to predict or estimate a target variable :math:`Y`. The resulting prediction of a trained model is denoted by :math:`R`. Additionally, we -define :math:`A` as a subset of :math:`X`, which corresponds to legally protected attributes such as -ethnicity, gender, etc. - -.. math:: +define :math:`A`, a subset of :math:`X`, which corresponds to legally protected attributes. - \underbrace{\text{Title}\hspace{2mm}\overbrace{\text{Gender}\hspace{2mm} \text{Ethnicity}}^{A}\hspace{2mm}\text{Legal Status}}_{X}\hspace{3mm}\overbrace{\text{Raw Score}}^{Y}\hspace{3mm}\overbrace{\text{Predicted Score}}^{R} +.. raw:: html -.. There are multiple definitions of fairness in literature. +
-There is a large amount of literature on fairness and bias, in the context of data science and -machine learning, in academia. +:math:`\underbrace{\text{Title}\quad \overbrace{\text{Gender}\quad \text{Ethnicity}}^{A}\quad \text{Legal Status}}_{X}\quad \overbrace{\text{Raw Score}}^{Y}\quad \overbrace{\text{Predicted Score}}^{R}` :footcite:p:`compas` -There is a large number of research papers covering bias and fairness in AI and methods of -reducing the problems of their presence in datasets or machine learning models. -Some of them involve data preprocessing, however those solutions might sometimes be problematic -due to hidden correlations present between sensitive groups and columns that seem inoffensive. +.. raw:: html -Algorithmic Fairness -^^^^^^^^^^^^^^^^^^^^ +
-However, the great majority of solutions for ensuring fairness involve algorithmic fairness, which -presumes the existence of a predictor or some type of machine learning model. Even more, it is a -requirement for applying methods that address bias issues through the need of actual predictions, -which are needed to calculate various probabilities, such as true and false positive rates, positive and -negative predictive values and many more. These are then used to enforce metrics such as equalized odds, -statistical or predictive parity. - -Equalized odds -^^^^^^^^^^^^^^ - -Equalized odds is satisfied by a predictor when it produces a true positive rate and a false positive rate -that are equal across the sensitive groups. This implies that the model has the same chance of correctly -classifying a true positive as positive and incorrectly classifying a true positive as negative. +There are multiple definitions of fairness in literature, and while many can be contentious, results are often +considered fair if they are independent of legally protected characteristics such as age, gender, and ethnicity +:footcite:p:`fairmlbook`. .. math:: - P(\hat{y} \mid y = 0, G = male) = P(\hat{y} \mid y = 0, G = female) -Statistical parity -^^^^^^^^^^^^^^^^^^ + P(R \mid A) = P(R) -Alternatively, statistical parity (also know as group fairness or demographic parity) is achieved when members -of the different sub-groups of protected categories (i.e. male and female for the protected category of gender) -are predicted to belong to the positive class at the same rate. +In practice, most legally protected attributes tend to be categorical, therefore for the sake of simplicity +we model protected or sensitive variables as discrete random variables, which gives us the following for independence. .. math:: - P(\hat{y} \mid G = male) = P(\hat{y} \mid G = female) - -It is clear to see that while both of these solutions seem fair, they constrain the model in different ways and -it has actually been shown that it is not mathematically possible to enforce two or more methods on the same model [2], -which raises a dilemma on the true meaning of bias and what type of fairness is the most desirable. - -While any of methods can be employed to produce workable results in practice, they are not exhaustive, as some use cases -might not be focused on producing an ML model as an end result. Data synthesis is a good example of a different purpose, -as the desired result is a new dataset that accurately reflects the intrinsic traits and column distributions of the original data, -with the added benefits of resolving privacy and anonimity issues, as well as possibly extending the dataset to better reflect -underrepresented parts of the data, with the goal of becoming more relevant in decision-making computations. -As such, the aim of Fairlens is to analyze datasets and produce fairness scores in the absence of a machine learning model, -picking up discrepancies between different sensitive demographics with respect to a target column, usually representing -a score or classification. + P(R \mid A = a) = P(R)\quad \forall a \in A -Types of Biases ---------------- - -Some of the predominant causes of unfairness resulting from structural datasets are highlighted in the -works of Kamishima et al. on a regularization approach to fairness in models [1]. In summary, the -following are ways in which biases can exist in structural data. - -Prejudice -^^^^^^^^^ +.. math:: + \text{or} -Prejudice occurs when the target column in a dataset, or any column used in a prediction model, is -dependant on a sensitive column. There are three types of prejudice. +.. math:: -- Direct prejudice occurs when a sensitive column is directly used in a prediction model therefore the - classification results will depend on sensitive features producing direct discrimination. -- Indirect prejudice is defined as the presence of a statistical dependence between a sensitive feature and - the target. This results in a high correlation between the values of the sensitive column and the target. - Even if the target is no longer a function of the sensitive data, it is still clearly influenced by it. -- Latent prejudice is defined as a statistical dependence between a sensitive variable and a non-sensitive one. - For example, if there exists a correlation between a sensitive column and a non-sensitive column - which is then used by the predictor, we can say the determination is discriminatory, as the - dependence is still present, but a layer deeper. + P(R \mid A = a) = P(R \mid A = b)\quad \forall a,b \in A -Underestimation -^^^^^^^^^^^^^^^ +While :footcite:t:`fairmlbook` proposes 2 alternative fairness or non-discrimination criteria, separation and sufficiency, +which are applicable to a range of problems, we have chosen to work with independence because of its generality +:footcite:p:`gouic2020projection`. +Working with this abstract definition of fairness, we quantify the bias of a variable :math:`T` in a group :math:`a`, +as the statistical distance between the the probability distributions of :math:`P(T \mid A = a)` and :math:`P(T)`. -Underestimation appears when a model is not fully converged due to the size limitations of a training dataset. Assuming -the presence of a an algorithm that is able to learn without commiting indirect prejudice, it is going to produce a fair -predictor if the the training examples are infinite. +.. image:: ../_static/distance.png -However, if the size of the data is limited and some groups are underrepresented, the model might unintentionally produce -unfair results based on the local observations of the sample distributions. +Using this definition, we can carry out hypothesis tests to measure the significance of an observed bias in a variable +in a sensitive subgroup. The advantage of using this method is that the target variable can be continuous, categorical +or binary, depending on the metric or test used. -Negative Legacy -^^^^^^^^^^^^^^^ +FairLens consists of a range of metrics and tests for statistical similarity and correlation, along with Monte Carlo +methods for hypothesis testing, which can be used to assess fairness in the aformentioned way. The fairness of +a variable, with respect to sensitive attributes, is assessed by measuring the bias of the variable in each +possible sensitive subgroup and taking the weighted average. -Negative legacy is generated by biased sampling or labelling in a dataset. This can appear in data collected by organizations -that, historically, have been discriminatory towards minorities, without assessing individuals based on objective criteria. This -results in less samples of positive outcomes for some groups, leading to sample selection bias which is considerably difficult to -detect in training data. +In practice, :math:`R` can be substituted with any column in the data. For instance, if :math:`R` is the target column +:math:`Y` in a dataset, then using the above methods will yeild results indicating inherent biases present in the +dataset. This can be useful for mitigating bias in the data itself, which may nip the problem in the bud. +On the other hand, using the predictions as :math:`R` will indicate the algorithmic bias of the model. References ---------- -[1] Mehrabi N, Morstatter F, Saxena N, Lerman K, Galstyan A. A survey on bias and fairness in machine learning. -ACM Computing Surveys (CSUR). 2021 Jul 13;54(6):1-35. - - -[1] Kamishima, T., Akaho, S., and Sakuma, J. Fairness aware learning through regularization approach. -In IEEE 11th International Conference on Data Mining, pp. 643–650, 2011. - -[2] Garg, Pratyush, John Villasenor, and Virginia Foggo. Fairness metrics: A comparative analysis. -In 2020 IEEE International Conference on Big Data (Big Data), pp. 3662-3666. IEEE, 2020. +.. footbibliography:: diff --git a/docs/user_guide/index.rst b/docs/user_guide/index.rst index edf251a6..8c9d459f 100644 --- a/docs/user_guide/index.rst +++ b/docs/user_guide/index.rst @@ -6,6 +6,7 @@ User Guide :glob: :caption: Getting Started + fairness quickstart .. toctree:: diff --git a/setup.cfg b/setup.cfg index 06f7169c..ab9b770b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -60,6 +60,7 @@ doc = m2r2==0.2.7 pydata-sphinx-theme==0.6.3 sphinx-panels==0.6.0 + sphinxcontrib-bibtex==2.3.0 [flake8] max-line-length = 120