A Python Toolkit for Detecting Bias in AI Models
Project description
humancompatible.detect
humancompatible.detect is an open-source toolkit for detecting bias in AI models and their training data.
AI Fairness
In fairness auditing, one would generally like to know if two distributions are identical. These distributions could be a distribution of internal private training data and publicly accessible data from a nationwide census, i.e., a good baseline. Or one can compare samples classified positively and negatively, to see if groups are represented equally in each class.
In other words, we ask
Is there some combination of protected attributes (race × age × …) for which people are treated noticeably differently?
A set of samples belonging to a given combination of protected attributes is called a subgroup.
Using HumanCompatible.Detect
-
Install the library (in a virtual environment if desired):
pip install humancompatible-detect
-
Compute the bias (MSD in this case):
from humancompatible.detect import detect_and_score # toy example # (col 1 = Race, col 2 = Age, col 3 = (binary) target) rule_idx, msd = detect_and_score( csv_path = "./data/01_data.csv", target_col = "Target", protected_list = ["Race", "Age"], method = "MSD", )
More to explore
examples/01_basic_usage.ipynb-- a 5-minute notebook reproducing the call above, then translatingrule_idxback to human-readable conditions.examples/02_folktables_within-state.ipynb-- a realistic Folktables/ACS Income example that runs MSD within a single state, reports the most affected subgroup, and interprets the signed gap.- More notebooks live in
examples/, new ones being added over time.
Feel free to start with the light notebook, then dive into the experiments with different datasets.
We also provide documentation. For more details on installation, see Installation details.
Methods
Maximum Subgroup Discrepancy (MSD)
MSD is the subgroup maximal difference in probability mass of a given subgroup, comparing the mass given by each distribution.
- Naturally, two distributions are fair iff all subgroups have similar mass.
- The arg max immediately tells you which group is most disadvantaged as an interpretable attribute-value combination.
- MSD has linear sample complexity, a stark contrast to exponential complexity of other distributional distances (Wasserstein, TV...)
Subsampled ℓ∞ norm
This method checks in a very efficient way whether the bias in any subgroup exceeds a given threshold. That is, it tells us to which extent, a particular subgroup obtains the positive outcome more or less frequently than the general trend in the dataset. Here, the fact that we can perform a subsampling with guaranties is key. It is the method of choice in cases in which one wants to be sure that a given dataset is compliant with a predefined acceptable bias level for all its subgroups.
Installation details
Requirements
All Python dependencies are declared in pyproject.toml (core + optional extras).
-
Python ≥ 3.10
-
A MILP solver (required for MSD).
We use Pyomo for modelling. This allows for multiple solvers, see the lists of solver interfaces and persistent solver interfaces.
-
Default (recommended): HiGHS -- works out of the box because we install the HiGHS Python bindings (highspy) with the package.
-
Optional commercial solvers (license required): Gurobi / CPLEX / Xpress These require a valid installation + license from the vendor. (Some also have free community license, and pip-installable Python APIs.)
-
Optional open-source fallback: GLPK requires the glpsol executable on your system PATH.
-
-
Other dependencies (installed automatically): numpy, pandas, scipy, pyomo, tqdm, etc.
(Optional) create a fresh environment
python -m venv .venv
# Activate it
source .venv/bin/activate # Linux / macOS
.venv\Scripts\activate.bat # Windows -- cmd.exe
.venv\Scripts\Activate.ps1 # Windows -- PowerShell
Install the package
python -m pip install humancompatible-detect
Optional extras
To install with optional commercial solvers:
python -m pip install "humancompatible-detect[gurobi]"
python -m pip install "humancompatible-detect[cplex]"
python -m pip install "humancompatible-detect[xpress]"
Or if you want the notebooks + plotting dependencies:
python -m pip install "humancompatible-detect[examples]"
And if docs/dev dependencies are desired:
python -m pip install "humancompatible-detect[docs]"
python -m pip install "humancompatible-detect[dev]"
Verify it worked
python -c "from humancompatible.detect import detect_and_score; print('detect imported OK')"
If the import fails you'll see:
ModuleNotFoundError: No module named 'humancompatible'
References
If you use the MSD in your work, please cite the following work:
@inproceedings{MSD,
author = {N\v{e}me\v{c}ek, Ji\v{r}\'{\i} and Kozdoba, Mark and Kryvoviaz, Illia and Pevn\'{y}, Tom\'{a}\v{s} and Mare\v{c}ek, Jakub},
title = {Bias Detection via Maximum Subgroup Discrepancy},
year = {2025},
isbn = {9798400714542},
publisher = {Association for Computing Machinery},
address = {New York, NY, USA},
url = {https://doi.org/10.1145/3711896.3736857},
doi = {10.1145/3711896.3736857},
booktitle = {Proceedings of the 31st ACM SIGKDD Conference on Knowledge Discovery and Data Mining V.2},
pages = {2174–2185},
numpages = {12},
location = {Toronto ON, Canada},
series = {KDD '25}
}
If you used the ℓ∞ method, please cite:
@misc{matilla2025samplecomplexitybiasdetection,
title={Sample Complexity of Bias Detection with Subsampled Point-to-Subspace Distances},
author={M. Matilla, Germ\'{a}n and Mare\v{c}ek, Jakub},
year={2025},
eprint={2502.02623},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/2502.02623v1},
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file humancompatible_detect-0.1.6.tar.gz.
File metadata
- Download URL: humancompatible_detect-0.1.6.tar.gz
- Upload date:
- Size: 44.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4bdcfe69b0257fac1bc6494667f68e6560088604e439d4c1b8080c4cf508c62d
|
|
| MD5 |
fe0ebb9f86f07cf7934452d926344008
|
|
| BLAKE2b-256 |
bd3db74db34c82170dfd8b0dfb96b0940895bb5cd53932f192d6e063970ff26a
|
File details
Details for the file humancompatible_detect-0.1.6-py3-none-any.whl.
File metadata
- Download URL: humancompatible_detect-0.1.6-py3-none-any.whl
- Upload date:
- Size: 50.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0362e0adfa97eec062cfcb5a066061efd898ae468e9466920148d9385f3dd2a
|
|
| MD5 |
7f73d2b527715b9b0450dbe783053af7
|
|
| BLAKE2b-256 |
4b6ee791693dd222431c1625c46a304f8b14829b683187b2efdca3b6a40bd5d2
|