humancompatible-detect 0.1.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. humancompatible_detect-0.1.4/LICENSE +201 -0
  2. humancompatible_detect-0.1.4/PKG-INFO +253 -0
  3. humancompatible_detect-0.1.4/README.md +195 -0
  4. humancompatible_detect-0.1.4/humancompatible/__init__.py +0 -0
  5. humancompatible_detect-0.1.4/humancompatible/detect/__init__.py +13 -0
  6. humancompatible_detect-0.1.4/humancompatible/detect/binarizer/Binarizer.py +265 -0
  7. humancompatible_detect-0.1.4/humancompatible/detect/binarizer/__init__.py +3 -0
  8. humancompatible_detect-0.1.4/humancompatible/detect/data_handler/DataHandler.py +351 -0
  9. humancompatible_detect-0.1.4/humancompatible/detect/data_handler/__init__.py +3 -0
  10. humancompatible_detect-0.1.4/humancompatible/detect/data_handler/features/Binary.py +107 -0
  11. humancompatible_detect-0.1.4/humancompatible/detect/data_handler/features/Categorical.py +157 -0
  12. humancompatible_detect-0.1.4/humancompatible/detect/data_handler/features/Contiguous.py +107 -0
  13. humancompatible_detect-0.1.4/humancompatible/detect/data_handler/features/Feature.py +105 -0
  14. humancompatible_detect-0.1.4/humancompatible/detect/data_handler/features/Mixed.py +147 -0
  15. humancompatible_detect-0.1.4/humancompatible/detect/data_handler/features/__init__.py +6 -0
  16. humancompatible_detect-0.1.4/humancompatible/detect/data_handler/features/utils.py +88 -0
  17. humancompatible_detect-0.1.4/humancompatible/detect/data_handler/types.py +15 -0
  18. humancompatible_detect-0.1.4/humancompatible/detect/detect_bias.py +280 -0
  19. humancompatible_detect-0.1.4/humancompatible/detect/evaluate_bias.py +286 -0
  20. humancompatible_detect-0.1.4/humancompatible/detect/helpers/__init__.py +5 -0
  21. humancompatible_detect-0.1.4/humancompatible/detect/helpers/prepare.py +105 -0
  22. humancompatible_detect-0.1.4/humancompatible/detect/helpers/utils.py +364 -0
  23. humancompatible_detect-0.1.4/humancompatible/detect/methods/__init__.py +0 -0
  24. humancompatible_detect-0.1.4/humancompatible/detect/methods/l_inf/__init__.py +3 -0
  25. humancompatible_detect-0.1.4/humancompatible/detect/methods/l_inf/l_inf.py +109 -0
  26. humancompatible_detect-0.1.4/humancompatible/detect/methods/l_inf/lp_tools.py +53 -0
  27. humancompatible_detect-0.1.4/humancompatible/detect/methods/msd/__init__.py +4 -0
  28. humancompatible_detect-0.1.4/humancompatible/detect/methods/msd/mapping_msd.py +112 -0
  29. humancompatible_detect-0.1.4/humancompatible/detect/methods/msd/metrics_msd.py +34 -0
  30. humancompatible_detect-0.1.4/humancompatible/detect/methods/msd/msd.py +93 -0
  31. humancompatible_detect-0.1.4/humancompatible/detect/methods/msd/one_rule.py +324 -0
  32. humancompatible_detect-0.1.4/humancompatible_detect.egg-info/PKG-INFO +253 -0
  33. humancompatible_detect-0.1.4/humancompatible_detect.egg-info/SOURCES.txt +38 -0
  34. humancompatible_detect-0.1.4/humancompatible_detect.egg-info/dependency_links.txt +1 -0
  35. humancompatible_detect-0.1.4/humancompatible_detect.egg-info/requires.txt +32 -0
  36. humancompatible_detect-0.1.4/humancompatible_detect.egg-info/top_level.txt +1 -0
  37. humancompatible_detect-0.1.4/pyproject.toml +90 -0
  38. humancompatible_detect-0.1.4/setup.cfg +4 -0
  39. humancompatible_detect-0.1.4/tests/test_detect_bias.py +212 -0
  40. humancompatible_detect-0.1.4/tests/test_evaluate_bias.py +229 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,253 @@
1
+ Metadata-Version: 2.4
2
+ Name: humancompatible-detect
3
+ Version: 0.1.4
4
+ Summary: A Python Toolkit for Detecting Bias in AI Models
5
+ Author: Germán Martínez Matilla, Illia Kryvoviaz, Jakub Mareček, Jiří Němeček
6
+ Maintainer-email: Illia Kryvoviaz <illiakryvoviaz@gmail.com>, Jiří Němeček <nemecj38@fel.cvut.cz>
7
+ License-Expression: Apache-2.0
8
+ Project-URL: Homepage, https://github.com/humancompatible/detect
9
+ Project-URL: Documentation, https://humancompatible-detect.readthedocs.io/en/latest
10
+ Project-URL: Issues, https://github.com/humancompatible/detect/issues
11
+ Keywords: toolkit,fairness,bias,ai,ml,subgroup,intersectional,msd,linf,optimization,milp,pyomo,highs,gurobi
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3 :: Only
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Topic :: Scientific/Engineering
24
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
25
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
26
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
27
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
28
+ Requires-Python: >=3.10
29
+ Description-Content-Type: text/markdown
30
+ License-File: LICENSE
31
+ Requires-Dist: numpy<3.0,>=2.2
32
+ Requires-Dist: pandas<3.0,>=2.2
33
+ Requires-Dist: pyomo<7.0,>=6.9
34
+ Requires-Dist: highspy<2.0,>=1.10
35
+ Requires-Dist: scipy<2.0,>=1.16
36
+ Requires-Dist: tqdm<5.0,>=4.6
37
+ Provides-Extra: gurobi
38
+ Requires-Dist: gurobipy<13.0,>=12.0; extra == "gurobi"
39
+ Provides-Extra: cplex
40
+ Requires-Dist: cplex<23.0,>=22.1.2.0; extra == "cplex"
41
+ Provides-Extra: xpress
42
+ Requires-Dist: xpress<10.0,>=9.8.0; extra == "xpress"
43
+ Provides-Extra: examples
44
+ Requires-Dist: jupyterlab<5.0,>=4.2; extra == "examples"
45
+ Requires-Dist: matplotlib<4.0,>=3.10; extra == "examples"
46
+ Requires-Dist: folktables<0.1,>=0.0.12; extra == "examples"
47
+ Provides-Extra: docs
48
+ Requires-Dist: sphinx<8.0,>=7.0; extra == "docs"
49
+ Requires-Dist: myst-parser<4.0,>=3.0; extra == "docs"
50
+ Requires-Dist: sphinx-rtd-theme<3.0,>=2.0; extra == "docs"
51
+ Provides-Extra: dev
52
+ Requires-Dist: pytest<9.0,>=8.0; extra == "dev"
53
+ Requires-Dist: ruff<0.7,>=0.6; extra == "dev"
54
+ Requires-Dist: black<25.0,>=24.8; extra == "dev"
55
+ Requires-Dist: build<2.0,>=1.2; extra == "dev"
56
+ Requires-Dist: twine<6.0,>=5.0; extra == "dev"
57
+ Dynamic: license-file
58
+
59
+ # humancompatible.detect
60
+
61
+ [![Docs](https://readthedocs.org/projects/humancompatible-detect/badge/?version=latest)](https://humancompatible-detect.readthedocs.io/en/latest)
62
+ [![License: Apache-2.0](https://img.shields.io/badge/License-Apache--2.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
63
+ [![Pypi](https://img.shields.io/pypi/v/humancompatible-detect)](https://pypi.org/project/humancompatible-detect/)
64
+
65
+ humancompatible.detect is an open-source toolkit for detecting bias in AI models and their training data.
66
+
67
+ ## AI Fairness
68
+
69
+ In fairness auditing, one would generally like to know if two distributions are identical.
70
+ These distributions could be a distribution of internal private training data and publicly accessible data from a nationwide census, i.e., a good baseline.
71
+ Or one can compare samples classified positively and negatively, to see if groups are represented equally in each class.
72
+
73
+ In other words, we ask
74
+
75
+ > Is there _some_ combination of protected attributes (race × age × …) for which people are treated noticeably differently?
76
+
77
+ A set of samples belonging to a given combination of protected attributes is called a subgroup.
78
+
79
+ <!-- Formally, let
80
+
81
+ * **X** ∈ ℝ<sup>d</sup> be the feature space,
82
+ * **P** and **Q** two distributions we want to compare (e.g. training vs census, positives vs negatives),
83
+ * **𝒫** ⊂ {1,…,d} the indices of *protected* features (age, sex, race, …).
84
+
85
+ A **sub-group** *S* is all samples whose protected attributes take one fixed value each.
86
+ We must consider every such intersection -- their number is exponential in |𝒫|.
87
+ -->
88
+
89
+ ## Using HumanCompatible.Detect
90
+
91
+ 1. Install the library (in a virtual environment if desired):
92
+ ```bash
93
+ pip install humancompatible-detect
94
+ ```
95
+ 2. Compute the bias ([MSD](#maximum-subgroup-discrepancy-msd) in this case):
96
+
97
+ ```python
98
+ from humancompatible.detect import detect_and_score
99
+
100
+ # toy example
101
+ # (col 1 = Race, col 2 = Age, col 3 = (binary) target)
102
+ rule_idx, msd = detect_and_score(
103
+ csv_path = "./data/01_data.csv",
104
+ target_col = "Target",
105
+ protected_list = ["Race", "Age"],
106
+ method = "MSD",
107
+ )
108
+ ```
109
+
110
+ ### More to explore
111
+
112
+ - [`examples/01_basic_usage.ipynb`](https://github.com/humancompatible/detect/blob/main/examples/01_basic_usage.ipynb) -- a 5-minute notebook reproducing the call above, then translating `rule_idx` back to human-readable conditions.
113
+ - [`examples/02_folktables_within-state.ipynb`](https://github.com/humancompatible/detect/blob/main/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.
114
+ - More notebooks live in [`examples/`](https://github.com/humancompatible/detect/tree/main/examples), new ones being added over time.
115
+
116
+ Feel free to start with the light notebook, then dive into the experiments with different datasets.
117
+
118
+ We also provide [documentation](https://humancompatible-detect.readthedocs.io/en/latest). For more details on installation, see [Installation details](#installation-details).
119
+
120
+ ---
121
+
122
+ ## Methods
123
+
124
+ ### Maximum Subgroup Discrepancy (MSD)
125
+
126
+ MSD is the subgroup maximal difference in probability mass of a given subgroup, comparing the mass given by each distribution.
127
+
128
+ <!-- ```math
129
+
130
+ \text{MSD}(P,Q;\,𝒫)=
131
+ \max_{S\;\text{sub-group on }𝒫}\;
132
+ \bigl|\;P(S)-Q(S)\;\bigr|.
133
+
134
+ ``` -->
135
+
136
+ - Naturally, two distributions are _fair_ iff all subgroups have similar mass.
137
+ - The **arg max** immediately tells you _which_ group is most disadvantaged as an interpretable attribute-value combination.
138
+ - MSD has linear sample complexity, a stark contrast to exponential complexity of other distributional distances (Wasserstein, TV...)
139
+
140
+ ### Subsampled ℓ∞ norm
141
+
142
+ 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.
143
+
144
+ ---
145
+
146
+ ## Installation details
147
+
148
+ ### Requirements
149
+
150
+ All Python dependencies are declared in pyproject.toml (core + optional extras).
151
+
152
+ - **Python ≥ 3.10**
153
+
154
+ - **A MILP solver** (required for MSD).
155
+ > We use [Pyomo](https://pyomo.readthedocs.io/) for modelling. This allows for multiple solvers, see the lists of [solver interfaces](https://pyomo.readthedocs.io/en/stable/reference/topical/solvers/index.html) and [persistent solver interfaces](https://pyomo.readthedocs.io/en/stable/reference/topical/appsi/appsi.html).
156
+
157
+ - Default (recommended): HiGHS -- works out of the box because we install the HiGHS Python bindings (highspy) with the package.
158
+
159
+ - Optional commercial solvers (license required): Gurobi / CPLEX / Xpress
160
+ These require a valid installation + license from the vendor. (Some also have free community license, and pip-installable Python APIs.)
161
+
162
+ - Optional open-source fallback: GLPK requires the glpsol executable on your system PATH.
163
+
164
+ - Other dependencies (installed automatically): numpy, pandas, scipy, pyomo, tqdm, etc.
165
+
166
+ ### (Optional) create a fresh environment
167
+
168
+ ```bash
169
+ python -m venv .venv
170
+ # Activate it
171
+ source .venv/bin/activate # Linux / macOS
172
+ .venv\Scripts\activate.bat # Windows -- cmd.exe
173
+ .venv\Scripts\Activate.ps1 # Windows -- PowerShell
174
+ ```
175
+
176
+ ### Install the package
177
+
178
+ ```bash
179
+ python -m pip install humancompatible-detect
180
+ ```
181
+
182
+ ### Optional extras
183
+
184
+ To install with optional commercial solvers:
185
+
186
+ ```bash
187
+ python -m pip install "humancompatible-detect[gurobi]"
188
+ python -m pip install "humancompatible-detect[cplex]"
189
+ python -m pip install "humancompatible-detect[xpress]"
190
+ ```
191
+
192
+ Or if you want the notebooks + plotting dependencies:
193
+
194
+ ```bash
195
+ python -m pip install "humancompatible-detect[examples]"
196
+ ```
197
+
198
+ And if docs/dev dependencies are desired:
199
+
200
+ ```bash
201
+ python -m pip install "humancompatible-detect[docs]"
202
+ python -m pip install "humancompatible-detect[dev]"
203
+ ```
204
+
205
+ ### Verify it worked
206
+
207
+ ```bash
208
+ python -c "from humancompatible.detect import detect_and_score; print('detect imported OK')"
209
+ ```
210
+
211
+ If the import fails you'll see:
212
+
213
+ ```bash
214
+ ModuleNotFoundError: No module named 'humancompatible'
215
+ ```
216
+
217
+ ---
218
+
219
+ ## References
220
+
221
+ If you use the MSD in your work, please cite the following work:
222
+
223
+ ```bibtex
224
+ @inproceedings{MSD,
225
+ 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},
226
+ title = {Bias Detection via Maximum Subgroup Discrepancy},
227
+ year = {2025},
228
+ isbn = {9798400714542},
229
+ publisher = {Association for Computing Machinery},
230
+ address = {New York, NY, USA},
231
+ url = {https://doi.org/10.1145/3711896.3736857},
232
+ doi = {10.1145/3711896.3736857},
233
+ booktitle = {Proceedings of the 31st ACM SIGKDD Conference on Knowledge Discovery and Data Mining V.2},
234
+ pages = {2174–2185},
235
+ numpages = {12},
236
+ location = {Toronto ON, Canada},
237
+ series = {KDD '25}
238
+ }
239
+ ```
240
+
241
+ If you used the ℓ∞ method, please cite:
242
+
243
+ ```bibtex
244
+ @misc{matilla2025samplecomplexitybiasdetection,
245
+ title={Sample Complexity of Bias Detection with Subsampled Point-to-Subspace Distances},
246
+ author={M. Matilla, Germán and Mareček, Jakub},
247
+ year={2025},
248
+ eprint={2502.02623},
249
+ archivePrefix={arXiv},
250
+ primaryClass={cs.LG},
251
+ url={https://arxiv.org/abs/2502.02623v1},
252
+ }
253
+ ```
@@ -0,0 +1,195 @@
1
+ # humancompatible.detect
2
+
3
+ [![Docs](https://readthedocs.org/projects/humancompatible-detect/badge/?version=latest)](https://humancompatible-detect.readthedocs.io/en/latest)
4
+ [![License: Apache-2.0](https://img.shields.io/badge/License-Apache--2.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
5
+ [![Pypi](https://img.shields.io/pypi/v/humancompatible-detect)](https://pypi.org/project/humancompatible-detect/)
6
+
7
+ humancompatible.detect is an open-source toolkit for detecting bias in AI models and their training data.
8
+
9
+ ## AI Fairness
10
+
11
+ In fairness auditing, one would generally like to know if two distributions are identical.
12
+ These distributions could be a distribution of internal private training data and publicly accessible data from a nationwide census, i.e., a good baseline.
13
+ Or one can compare samples classified positively and negatively, to see if groups are represented equally in each class.
14
+
15
+ In other words, we ask
16
+
17
+ > Is there _some_ combination of protected attributes (race × age × …) for which people are treated noticeably differently?
18
+
19
+ A set of samples belonging to a given combination of protected attributes is called a subgroup.
20
+
21
+ <!-- Formally, let
22
+
23
+ * **X** ∈ ℝ<sup>d</sup> be the feature space,
24
+ * **P** and **Q** two distributions we want to compare (e.g. training vs census, positives vs negatives),
25
+ * **𝒫** ⊂ {1,…,d} the indices of *protected* features (age, sex, race, …).
26
+
27
+ A **sub-group** *S* is all samples whose protected attributes take one fixed value each.
28
+ We must consider every such intersection -- their number is exponential in |𝒫|.
29
+ -->
30
+
31
+ ## Using HumanCompatible.Detect
32
+
33
+ 1. Install the library (in a virtual environment if desired):
34
+ ```bash
35
+ pip install humancompatible-detect
36
+ ```
37
+ 2. Compute the bias ([MSD](#maximum-subgroup-discrepancy-msd) in this case):
38
+
39
+ ```python
40
+ from humancompatible.detect import detect_and_score
41
+
42
+ # toy example
43
+ # (col 1 = Race, col 2 = Age, col 3 = (binary) target)
44
+ rule_idx, msd = detect_and_score(
45
+ csv_path = "./data/01_data.csv",
46
+ target_col = "Target",
47
+ protected_list = ["Race", "Age"],
48
+ method = "MSD",
49
+ )
50
+ ```
51
+
52
+ ### More to explore
53
+
54
+ - [`examples/01_basic_usage.ipynb`](https://github.com/humancompatible/detect/blob/main/examples/01_basic_usage.ipynb) -- a 5-minute notebook reproducing the call above, then translating `rule_idx` back to human-readable conditions.
55
+ - [`examples/02_folktables_within-state.ipynb`](https://github.com/humancompatible/detect/blob/main/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.
56
+ - More notebooks live in [`examples/`](https://github.com/humancompatible/detect/tree/main/examples), new ones being added over time.
57
+
58
+ Feel free to start with the light notebook, then dive into the experiments with different datasets.
59
+
60
+ We also provide [documentation](https://humancompatible-detect.readthedocs.io/en/latest). For more details on installation, see [Installation details](#installation-details).
61
+
62
+ ---
63
+
64
+ ## Methods
65
+
66
+ ### Maximum Subgroup Discrepancy (MSD)
67
+
68
+ MSD is the subgroup maximal difference in probability mass of a given subgroup, comparing the mass given by each distribution.
69
+
70
+ <!-- ```math
71
+
72
+ \text{MSD}(P,Q;\,𝒫)=
73
+ \max_{S\;\text{sub-group on }𝒫}\;
74
+ \bigl|\;P(S)-Q(S)\;\bigr|.
75
+
76
+ ``` -->
77
+
78
+ - Naturally, two distributions are _fair_ iff all subgroups have similar mass.
79
+ - The **arg max** immediately tells you _which_ group is most disadvantaged as an interpretable attribute-value combination.
80
+ - MSD has linear sample complexity, a stark contrast to exponential complexity of other distributional distances (Wasserstein, TV...)
81
+
82
+ ### Subsampled ℓ∞ norm
83
+
84
+ 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.
85
+
86
+ ---
87
+
88
+ ## Installation details
89
+
90
+ ### Requirements
91
+
92
+ All Python dependencies are declared in pyproject.toml (core + optional extras).
93
+
94
+ - **Python ≥ 3.10**
95
+
96
+ - **A MILP solver** (required for MSD).
97
+ > We use [Pyomo](https://pyomo.readthedocs.io/) for modelling. This allows for multiple solvers, see the lists of [solver interfaces](https://pyomo.readthedocs.io/en/stable/reference/topical/solvers/index.html) and [persistent solver interfaces](https://pyomo.readthedocs.io/en/stable/reference/topical/appsi/appsi.html).
98
+
99
+ - Default (recommended): HiGHS -- works out of the box because we install the HiGHS Python bindings (highspy) with the package.
100
+
101
+ - Optional commercial solvers (license required): Gurobi / CPLEX / Xpress
102
+ These require a valid installation + license from the vendor. (Some also have free community license, and pip-installable Python APIs.)
103
+
104
+ - Optional open-source fallback: GLPK requires the glpsol executable on your system PATH.
105
+
106
+ - Other dependencies (installed automatically): numpy, pandas, scipy, pyomo, tqdm, etc.
107
+
108
+ ### (Optional) create a fresh environment
109
+
110
+ ```bash
111
+ python -m venv .venv
112
+ # Activate it
113
+ source .venv/bin/activate # Linux / macOS
114
+ .venv\Scripts\activate.bat # Windows -- cmd.exe
115
+ .venv\Scripts\Activate.ps1 # Windows -- PowerShell
116
+ ```
117
+
118
+ ### Install the package
119
+
120
+ ```bash
121
+ python -m pip install humancompatible-detect
122
+ ```
123
+
124
+ ### Optional extras
125
+
126
+ To install with optional commercial solvers:
127
+
128
+ ```bash
129
+ python -m pip install "humancompatible-detect[gurobi]"
130
+ python -m pip install "humancompatible-detect[cplex]"
131
+ python -m pip install "humancompatible-detect[xpress]"
132
+ ```
133
+
134
+ Or if you want the notebooks + plotting dependencies:
135
+
136
+ ```bash
137
+ python -m pip install "humancompatible-detect[examples]"
138
+ ```
139
+
140
+ And if docs/dev dependencies are desired:
141
+
142
+ ```bash
143
+ python -m pip install "humancompatible-detect[docs]"
144
+ python -m pip install "humancompatible-detect[dev]"
145
+ ```
146
+
147
+ ### Verify it worked
148
+
149
+ ```bash
150
+ python -c "from humancompatible.detect import detect_and_score; print('detect imported OK')"
151
+ ```
152
+
153
+ If the import fails you'll see:
154
+
155
+ ```bash
156
+ ModuleNotFoundError: No module named 'humancompatible'
157
+ ```
158
+
159
+ ---
160
+
161
+ ## References
162
+
163
+ If you use the MSD in your work, please cite the following work:
164
+
165
+ ```bibtex
166
+ @inproceedings{MSD,
167
+ 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},
168
+ title = {Bias Detection via Maximum Subgroup Discrepancy},
169
+ year = {2025},
170
+ isbn = {9798400714542},
171
+ publisher = {Association for Computing Machinery},
172
+ address = {New York, NY, USA},
173
+ url = {https://doi.org/10.1145/3711896.3736857},
174
+ doi = {10.1145/3711896.3736857},
175
+ booktitle = {Proceedings of the 31st ACM SIGKDD Conference on Knowledge Discovery and Data Mining V.2},
176
+ pages = {2174–2185},
177
+ numpages = {12},
178
+ location = {Toronto ON, Canada},
179
+ series = {KDD '25}
180
+ }
181
+ ```
182
+
183
+ If you used the ℓ∞ method, please cite:
184
+
185
+ ```bibtex
186
+ @misc{matilla2025samplecomplexitybiasdetection,
187
+ title={Sample Complexity of Bias Detection with Subsampled Point-to-Subspace Distances},
188
+ author={M. Matilla, Germán and Mareček, Jakub},
189
+ year={2025},
190
+ eprint={2502.02623},
191
+ archivePrefix={arXiv},
192
+ primaryClass={cs.LG},
193
+ url={https://arxiv.org/abs/2502.02623v1},
194
+ }
195
+ ```
@@ -0,0 +1,13 @@
1
+ from .detect_bias import most_biased_subgroup, most_biased_subgroup_csv, most_biased_subgroup_two_samples
2
+ from .evaluate_bias import evaluate_biased_subgroup, evaluate_biased_subgroup_csv, evaluate_biased_subgroup_two_samples
3
+ from .helpers.utils import detect_and_score
4
+
5
+ __all__ = [
6
+ "detect_and_score",
7
+ "most_biased_subgroup",
8
+ "most_biased_subgroup_csv",
9
+ "most_biased_subgroup_two_samples",
10
+ "evaluate_biased_subgroup",
11
+ "evaluate_biased_subgroup_csv",
12
+ "evaluate_biased_subgroup_two_samples",
13
+ ]