# FLAI : Fairness Learning in Artificial Intelligence
[](https://github.com/rugonzs/FLAI/actions/workflows/python-publish.yml)
[](https://github.com/rugonzs/FLAI/actions/workflows/docs.yml)
[](https://pypi.org/project/flai-causal/)
[](https://pepy.tech/project/flai-causal)
[](https://pepy.tech/project/flai-causal)
[](https://pepy.tech/project/flai-causal)
Python library developed by Rubén González during his phD. research. His mission? To mitigate bias and discrimination through the application of causal algorithms. Use the citation at the bottom to reference this work.
[Demo](https://www.rubengonzalez.ai/demo)
[Documentation](https://rugonzs.github.io/FLAI/)
## Overview

**FLAI** is a Python library designed with three key functionalities: detect **discrimination**, building a **causal algorithm**, and **mitigating biases** within it.
1. **Fairness Metric: Bias Detection**, This library introduces a novel metric to measure Fairness, which consists of a two-dimensional vector: Equality and Equity. This metric allows proper quantification of both justice dimensions.
2. **Causal Algorithm Creation:** This library facilitates the development of a reliable causal algorithm, setting the stage for impartial data analysis.
3. **Bias Mitigation:** Fairness is pursued in two significant areas - **In-Training** and **Pre-Training**.
### In-Training Mitigation
The library includes features that allow the user to adjust the causal algorithm in two essential ways:
- **Graph Relationship Modification:** Relationships within the graph can be modified to establish a more balanced structure.
- **Probability Table Modification:** The probability table can be adjusted to prevent propagation or amplification of existing biases.
### Pre-Training Mitigation
With the mitigated causal algorithm, a bias-free dataset can be generated. This dataset can be used for the training of other algorithms, enabling the bias mitigation process to extend to the initial stages of new model development.
## Installation
**FLAI** can be easily installed using pip, Python's package installer. Open your terminal or command prompt and type the following command:
```bash
pip install flai-causal
```
python <= 3.9
```bash
pip install flai-causal==2.0.0
```
## Features Fairnes Metric
### Single sensible feature
Measure equality and equity.
```python
from FLAI import data
from FLAI import causal_graph
import pandas as pd
df = pd.read_parquet('../Data/case_1.parquet')
flai_dataset = data.Data(df, transform=False)
df_f,datos_f = flai_dataset.fairness_eqa_eqi(features = ['education'],
target_column = 'proba',
column_filter = ['sensible'],
plot = True)
```
Group is the underprivileged group, in this case sensible = 0. The reference is the privileged group, in this case sensible = 1
| Group | reference | Equity | Equality | Fairness |
|--------------------|--------------------|---------|----------|--------------|
| sensible 0 | sensible 1 | -004 | 0.08 | 0.09 |

### Multiple Sensible Feature
```python
df_f,datos_f = flai_dataset.fairness_eqa_eqi(features = ['education','age'],
target_column = 'proba',
column_filter = ['race','sex'],
plot = True)
```
| Group | reference | Equity | Equality | Fairness |
|--------------------|--------------------|---------|----------|--------------|
| race_0 sex_0 | race_1 sex_1 | -0.06 | 0.18 | 0.19 |
| race_0 sex_1 | race_1 sex_1 | -0.04 | 0.06 | 0.07 |
| race_1 sex_0 | race_1 sex_1 | -0.07 | 0.15 | 0.17 |

## Features Mitigation
### Causal Creation
```python
from FLAI import data
from FLAI import causal_graph
import pandas as pd
df = pd.read_pickle('../Data/adult.pickle')
flai_dataset = data.Data(df, transform=True)
flai_graph = causal_graph.CausalGraph(flai_dataset, target = 'label')
flai_graph.plot(directed = True)
```

### Causal Mitigation
#### Relations Mitigation
```python
flai_graph.mitigate_edge_relation(sensible_feature=['sex','age'])
```

#### Table Probabilities Mitigation
```python
flai_graph.mitigate_calculation_cpd(sensible_feature = ['age','sex'])
```
#### Inference
Assess the impact of sensitive features before mitigation. Sex, Age and Label 0 is the unfavorable value.
```python
flai_graph.inference(variables=['sex','label'], evidence={})
flai_graph.inference(variables=['age','label'], evidence={})
```
| sex | label | p |
|-------|-------|--------|
| 0 | 0 | 0.1047 |
| **0** | **1** | **0.2053** |
| 1 | 0 | 0.1925 |
| **1** | **1** | **0.4975** |
| age | label | p |
|-----|-------|--------|
| 0 | 0 | 0.0641 |
| **0** | **1** | **0.1259** |
| 1 | 0 | 0.2331 |
| **1** | **1** | **0.5769** |
Assess the impact of sensitive features after mitigation. Changes in sex or age not affect the output.
```python
mitigated_graph.inference(variables=['sex','label'], evidence={})
mitigated_graph.inference(variables=['age','label'], evidence={})
```
| sex | label | p |
|-----|-------|--------|
| 0 | 0 | 0.1498 |
| **0** | **1** | **0.3502** |
| 1 | 0 | 0.1498 |
| **1** | **1** | **0.3502** |
| age | label | p |
|-----|-------|--------|
| 0 | 0 | 0.1498 |
| **0** | **1** | **0.3502** |
| 1 | 0 | 0.1498 |
| **1** | **1** | **0.3502** |
### Fair Data
```python
fair_data = flai_graph.generate_dataset(n_samples = 1000, methodtype = 'bayes')
```

#### Train Algorithm With Fair Data.
```python
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
mitigated_X = fair_data.data[['age', 'sex', 'credit_history','savings','employment' ]]
mitigated_y = fair_data.data[['label']]
mitigated_X_train, mitigated_X_test, mitigated_y_train, mitigated_y_test = train_test_split(mitigated_X,
mitigated_y, test_size=0.7, random_state=54)
model_mitigated = XGBClassifier()
model_mitigated.fit(mitigated_X_train, mitigated_y_train)
metrics = mitigated_dataset.fairness_metrics(target_column='label', predicted_column = 'Predicted',
columns_fair = {'sex' : {'privileged' : 1, 'unprivileged' : 0},
'age' : {'privileged' : 1, 'unprivileged' : 0}})
```
##### Metrics Performance
| | ACC | TPR | FPR | FNR | PPP |
|-----------------|--------|---------|---------|---------|---------|
| model | 0.7034 | 0.97995 | 0.94494 | 0.02005 | 0.96948 |
| sex_privileged | 0.7024 | 0.97902 | 0.94363 | 0.02098 | 0.96841 |
| sex_unprivileged| 0.7044 | 0.98087 | 0.94626 | 0.01913 | 0.97055 |
| age_privileged | 0.7042 | 0.97881 | 0.94118 | 0.02119 | 0.96758 |
| age_unprivileged| 0.7026 | 0.98109 | 0.94872 | 0.01891 | 0.97139 |
##### Metrics Fairness
| | EOD | DI | SPD | OD |
|-----------------|---------|---------|---------|---------|
| sex_fair_metrics| 0.00185 | 1.00221 | 0.00214 | 0.00448 |
| age_fair_metrics| 0.00228 | 1.00394 | 0.00382 | 0.00981 |
#### Equity, Equality and Fairness Curve
```python
df_f,datos_f = fair_data.fairness_eqa_eqi(features = ['education','age'],
target_column = 'proba',
column_filter = ['race','sex'],
plot = True)
```
| Group | reference | Equity | Equality | Fairness |
|--------------------|--------------------|---------|----------|--------------|
| race_0 sex_0 | race_1 sex_1 | 0.0 | 0.0 | 0.0 |
| race_0 sex_1 | race_1 sex_1 | 0.0 | 0.0 | 0.0 |
| race_1 sex_0 | race_1 sex_1 | 0.0 | 0.0 | 0.0 |

##### Shap Results
```python
import shap
explainer_original = shap.Explainer(model_original)
explainer_mitigated = shap.Explainer(model_mitigated)
shap_values_orignal = explainer_original(original_dataset.data[
['age', 'sex', 'credit_history','savings','employment']])
shap_values_mitigated = explainer_mitigated(original_dataset.data[
['age', 'sex', 'credit_history','savings','employment']])
shap.plots.beeswarm(shap_values_orignal)
shap.plots.bar(shap_values_orignal)
shap.plots.beeswarm(shap_values_mitigated)
shap.plots.bar(shap_values_mitigated)
```


### References
* https://erdogant.github.io/bnlearn/
* http://pgmpy.org
## Citation
[Fairness Metric Paper Soon....]()
[Mitigation Paper](https://www.sciencedirect.com/science/article/pii/S0167739X24000694)
```
@article{GONZALEZSENDINO2024384,
title = {Mitigating bias in artificial intelligence: Fair data generation via causal models for transparent and explainable decision-making},
journal = {Future Generation Computer Systems},
volume = {155},
pages = {384-401},
year = {2024},
issn = {0167-739X},
doi = {https://doi.org/10.1016/j.future.2024.02.023},
url = {https://www.sciencedirect.com/science/article/pii/S0167739X24000694},
author = {Rubén González-Sendino and Emilio Serrano and Javier Bajo},
keywords = {Causal model, Bias mitigation, Fairness, Responsible artificial intelligence, Bayes},
abstract = {In the evolving field of Artificial Intelligence, concerns have arisen about the opacity of certain models and their potential biases. This study aims to improve fairness and explainability in AI decision making. Existing bias mitigation strategies are classified as pre-training, training, and post-training approaches. This paper proposes a novel technique to create a mitigated bias dataset. This is achieved using a mitigated causal model that adjusts cause-and-effect relationships and probabilities within a Bayesian network. Contributions of this work include (1) the introduction of a novel mitigation training algorithm for causal model; (2) a pioneering pretraining methodology for producing a fair dataset for Artificial Intelligence model training; (3) the diligent maintenance of sensitive features in the dataset, ensuring that these vital attributes are not overlooked during analysis and model training; (4) the enhancement of explainability and transparency around biases; and finally (5) the development of an interactive demonstration that vividly displays experimental results and provides the code for facilitating replication of the work.}
}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/rugonzs/FLAI",
"name": "FLAI-CAUSAL",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Rub\u00e9n Gonz\u00e1lez Sendino",
"author_email": "rubo.g@icloud.com",
"download_url": "https://files.pythonhosted.org/packages/13/b1/bc9c648df28a9680e4aea5037c6710e9e0273aa63b0186e544ad77e01ed1/flai_causal-3.0.4.tar.gz",
"platform": null,
"description": "\n\n# FLAI : Fairness Learning in Artificial Intelligence\n\n[](https://github.com/rugonzs/FLAI/actions/workflows/python-publish.yml)\n\n[](https://github.com/rugonzs/FLAI/actions/workflows/docs.yml)\n\n[](https://pypi.org/project/flai-causal/)\n\n[](https://pepy.tech/project/flai-causal)\n\n[](https://pepy.tech/project/flai-causal)\n\n[](https://pepy.tech/project/flai-causal)\n\nPython library developed by Rub\u00e9n Gonz\u00e1lez during his phD. research. His mission? To mitigate bias and discrimination through the application of causal algorithms. Use the citation at the bottom to reference this work.\n\n[Demo](https://www.rubengonzalez.ai/demo)\n\n[Documentation](https://rugonzs.github.io/FLAI/)\n\n## Overview\n\n\n\n**FLAI** is a Python library designed with three key functionalities: detect **discrimination**, building a **causal algorithm**, and **mitigating biases** within it.\n\n1. **Fairness Metric: Bias Detection**, This library introduces a novel metric to measure Fairness, which consists of a two-dimensional vector: Equality and Equity. This metric allows proper quantification of both justice dimensions.\n\n2. **Causal Algorithm Creation:** This library facilitates the development of a reliable causal algorithm, setting the stage for impartial data analysis.\n\n3. **Bias Mitigation:** Fairness is pursued in two significant areas - **In-Training** and **Pre-Training**.\n\n ### In-Training Mitigation\n\n The library includes features that allow the user to adjust the causal algorithm in two essential ways:\n\n - **Graph Relationship Modification:** Relationships within the graph can be modified to establish a more balanced structure.\n - **Probability Table Modification:** The probability table can be adjusted to prevent propagation or amplification of existing biases.\n\n ### Pre-Training Mitigation\n\n With the mitigated causal algorithm, a bias-free dataset can be generated. This dataset can be used for the training of other algorithms, enabling the bias mitigation process to extend to the initial stages of new model development.\n\n\n\n\n## Installation\n\n**FLAI** can be easily installed using pip, Python's package installer. Open your terminal or command prompt and type the following command:\n\n```bash\npip install flai-causal\n```\n\npython <= 3.9\n```bash\npip install flai-causal==2.0.0\n```\n\n## Features Fairnes Metric\n\n### Single sensible feature\n\nMeasure equality and equity.\n\n```python\nfrom FLAI import data\nfrom FLAI import causal_graph\nimport pandas as pd\n\ndf = pd.read_parquet('../Data/case_1.parquet')\nflai_dataset = data.Data(df, transform=False)\n\ndf_f,datos_f = flai_dataset.fairness_eqa_eqi(features = ['education'], \n target_column = 'proba', \n column_filter = ['sensible'],\n plot = True)\n```\n\nGroup is the underprivileged group, in this case sensible = 0. The reference is the privileged group, in this case sensible = 1\n\n| Group | reference | Equity | Equality | Fairness |\n|--------------------|--------------------|---------|----------|--------------|\n| sensible 0 | sensible 1 | -004 | 0.08 | 0.09 |\n\n\n\n\n\n### Multiple Sensible Feature\n\n\n```python\ndf_f,datos_f = flai_dataset.fairness_eqa_eqi(features = ['education','age'], \n target_column = 'proba', \n column_filter = ['race','sex'],\n plot = True)\n```\n\n| Group | reference | Equity | Equality | Fairness |\n|--------------------|--------------------|---------|----------|--------------|\n| race_0 sex_0 | race_1 sex_1 | -0.06 | 0.18 | 0.19 |\n| race_0 sex_1 | race_1 sex_1 | -0.04 | 0.06 | 0.07 |\n| race_1 sex_0 | race_1 sex_1 | -0.07 | 0.15 | 0.17 |\n\n\n\n\n\n## Features Mitigation\n\n### Causal Creation\n\n```python\nfrom FLAI import data\nfrom FLAI import causal_graph\nimport pandas as pd\n\ndf = pd.read_pickle('../Data/adult.pickle')\nflai_dataset = data.Data(df, transform=True)\nflai_graph = causal_graph.CausalGraph(flai_dataset, target = 'label')\nflai_graph.plot(directed = True)\n```\n\n\n\n### Causal Mitigation\n\n#### Relations Mitigation\n\n```python\n\nflai_graph.mitigate_edge_relation(sensible_feature=['sex','age'])\n```\n\n\n#### Table Probabilities Mitigation\n\n```python\n\nflai_graph.mitigate_calculation_cpd(sensible_feature = ['age','sex'])\n\n```\n\n#### Inference\n\nAssess the impact of sensitive features before mitigation. Sex, Age and Label 0 is the unfavorable value.\n\n```python\nflai_graph.inference(variables=['sex','label'], evidence={})\nflai_graph.inference(variables=['age','label'], evidence={})\n\n```\n\n| sex | label | p |\n|-------|-------|--------|\n| 0 | 0 | 0.1047 |\n| **0** | **1** | **0.2053** |\n| 1 | 0 | 0.1925 |\n| **1** | **1** | **0.4975** |\n\n| age | label | p |\n|-----|-------|--------|\n| 0 | 0 | 0.0641 |\n| **0** | **1** | **0.1259** |\n| 1 | 0 | 0.2331 |\n| **1** | **1** | **0.5769** |\n\nAssess the impact of sensitive features after mitigation. Changes in sex or age not affect the output.\n\n```python\nmitigated_graph.inference(variables=['sex','label'], evidence={})\nmitigated_graph.inference(variables=['age','label'], evidence={})\n\n```\n\n| sex | label | p |\n|-----|-------|--------|\n| 0 | 0 | 0.1498 |\n| **0** | **1** | **0.3502** |\n| 1 | 0 | 0.1498 |\n| **1** | **1** | **0.3502** |\n\n\n| age | label | p |\n|-----|-------|--------|\n| 0 | 0 | 0.1498 |\n| **0** | **1** | **0.3502** |\n| 1 | 0 | 0.1498 |\n| **1** | **1** | **0.3502** |\n\n\n### Fair Data\n\n```python\nfair_data = flai_graph.generate_dataset(n_samples = 1000, methodtype = 'bayes')\n```\n\n\n#### Train Algorithm With Fair Data.\n\n```python\nfrom xgboost import XGBClassifier\nfrom sklearn.model_selection import train_test_split\n\nmitigated_X = fair_data.data[['age', 'sex', 'credit_history','savings','employment' ]]\nmitigated_y = fair_data.data[['label']]\nmitigated_X_train, mitigated_X_test, mitigated_y_train, mitigated_y_test = train_test_split(mitigated_X,\n mitigated_y, test_size=0.7, random_state=54)\nmodel_mitigated = XGBClassifier()\nmodel_mitigated.fit(mitigated_X_train, mitigated_y_train)\nmetrics = mitigated_dataset.fairness_metrics(target_column='label', predicted_column = 'Predicted',\n columns_fair = {'sex' : {'privileged' : 1, 'unprivileged' : 0},\n 'age' : {'privileged' : 1, 'unprivileged' : 0}})\n```\n\n##### Metrics Performance\n\n| | ACC | TPR | FPR | FNR | PPP |\n|-----------------|--------|---------|---------|---------|---------|\n| model | 0.7034 | 0.97995 | 0.94494 | 0.02005 | 0.96948 |\n| sex_privileged | 0.7024 | 0.97902 | 0.94363 | 0.02098 | 0.96841 |\n| sex_unprivileged| 0.7044 | 0.98087 | 0.94626 | 0.01913 | 0.97055 |\n| age_privileged | 0.7042 | 0.97881 | 0.94118 | 0.02119 | 0.96758 |\n| age_unprivileged| 0.7026 | 0.98109 | 0.94872 | 0.01891 | 0.97139 |\n\n##### Metrics Fairness\n\n| | EOD | DI | SPD | OD |\n|-----------------|---------|---------|---------|---------|\n| sex_fair_metrics| 0.00185 | 1.00221 | 0.00214 | 0.00448 |\n| age_fair_metrics| 0.00228 | 1.00394 | 0.00382 | 0.00981 |\n\n#### Equity, Equality and Fairness Curve\n\n```python\ndf_f,datos_f = fair_data.fairness_eqa_eqi(features = ['education','age'], \n target_column = 'proba', \n column_filter = ['race','sex'],\n plot = True)\n```\n\n| Group | reference | Equity | Equality | Fairness |\n|--------------------|--------------------|---------|----------|--------------|\n| race_0 sex_0 | race_1 sex_1 | 0.0 | 0.0 | 0.0 |\n| race_0 sex_1 | race_1 sex_1 | 0.0 | 0.0 | 0.0 |\n| race_1 sex_0 | race_1 sex_1 | 0.0 | 0.0 | 0.0 |\n\n\n\n##### Shap Results\n```python\nimport shap\n\nexplainer_original = shap.Explainer(model_original)\nexplainer_mitigated = shap.Explainer(model_mitigated)\nshap_values_orignal = explainer_original(original_dataset.data[\n['age', 'sex', 'credit_history','savings','employment']])\nshap_values_mitigated = explainer_mitigated(original_dataset.data[\n ['age', 'sex', 'credit_history','savings','employment']])\nshap.plots.beeswarm(shap_values_orignal)\nshap.plots.bar(shap_values_orignal) \n\nshap.plots.beeswarm(shap_values_mitigated)\nshap.plots.bar(shap_values_mitigated)\n\n```\n\n\n\n\n\n### References\n* https://erdogant.github.io/bnlearn/\n* http://pgmpy.org\n## Citation\n[Fairness Metric Paper Soon....]()\n\n[Mitigation Paper](https://www.sciencedirect.com/science/article/pii/S0167739X24000694)\n```\n@article{GONZALEZSENDINO2024384,\ntitle = {Mitigating bias in artificial intelligence: Fair data generation via causal models for transparent and explainable decision-making},\njournal = {Future Generation Computer Systems},\nvolume = {155},\npages = {384-401},\nyear = {2024},\nissn = {0167-739X},\ndoi = {https://doi.org/10.1016/j.future.2024.02.023},\nurl = {https://www.sciencedirect.com/science/article/pii/S0167739X24000694},\nauthor = {Rub\u00e9n Gonz\u00e1lez-Sendino and Emilio Serrano and Javier Bajo},\nkeywords = {Causal model, Bias mitigation, Fairness, Responsible artificial intelligence, Bayes},\nabstract = {In the evolving field of Artificial Intelligence, concerns have arisen about the opacity of certain models and their potential biases. This study aims to improve fairness and explainability in AI decision making. Existing bias mitigation strategies are classified as pre-training, training, and post-training approaches. This paper proposes a novel technique to create a mitigated bias dataset. This is achieved using a mitigated causal model that adjusts cause-and-effect relationships and probabilities within a Bayesian network. Contributions of this work include (1) the introduction of a novel mitigation training algorithm for causal model; (2) a pioneering pretraining methodology for producing a fair dataset for Artificial Intelligence model training; (3) the diligent maintenance of sensitive features in the dataset, ensuring that these vital attributes are not overlooked during analysis and model training; (4) the enhancement of explainability and transparency around biases; and finally (5) the development of an interactive demonstration that vividly displays experimental results and provides the code for facilitating replication of the work.}\n}\n```\n",
"bugtrack_url": null,
"license": "Apache-2.0 license",
"summary": "Library to creat causal model and mitigate the bias.",
"version": "3.0.4",
"project_urls": {
"Documentation": "https://rugonzs.github.io/FLAI/",
"Homepage": "https://github.com/rugonzs/FLAI",
"Source Code": "https://github.com/rugonzs/FLAI"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ebf122b54a9daabb2ce720e21506eef1e6476c35e7186c978a376b6fbd6cc135",
"md5": "da65abbb403600ddf04e539bf4e999bc",
"sha256": "af176d541005449508f5535d2ed894cc1e1141d44776548cfc03bc8ab71d2a19"
},
"downloads": -1,
"filename": "FLAI_CAUSAL-3.0.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "da65abbb403600ddf04e539bf4e999bc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 15503,
"upload_time": "2024-11-24T18:30:03",
"upload_time_iso_8601": "2024-11-24T18:30:03.909286Z",
"url": "https://files.pythonhosted.org/packages/eb/f1/22b54a9daabb2ce720e21506eef1e6476c35e7186c978a376b6fbd6cc135/FLAI_CAUSAL-3.0.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "13b1bc9c648df28a9680e4aea5037c6710e9e0273aa63b0186e544ad77e01ed1",
"md5": "0228896d91f9a247d62073f4abd0f7ee",
"sha256": "faca16c11fecfba3649338a171505fa73555e72631a96dc32bcdfe885f01f196"
},
"downloads": -1,
"filename": "flai_causal-3.0.4.tar.gz",
"has_sig": false,
"md5_digest": "0228896d91f9a247d62073f4abd0f7ee",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 18721,
"upload_time": "2024-11-24T18:30:05",
"upload_time_iso_8601": "2024-11-24T18:30:05.554634Z",
"url": "https://files.pythonhosted.org/packages/13/b1/bc9c648df28a9680e4aea5037c6710e9e0273aa63b0186e544ad77e01ed1/flai_causal-3.0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-24 18:30:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "rugonzs",
"github_project": "FLAI",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "flai-causal"
}