rankaudit 0.1.0__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.
- rankaudit-0.1.0/.gitignore +19 -0
- rankaudit-0.1.0/LICENSE +163 -0
- rankaudit-0.1.0/PKG-INFO +384 -0
- rankaudit-0.1.0/README.md +340 -0
- rankaudit-0.1.0/docs/design.md +176 -0
- rankaudit-0.1.0/pyproject.toml +72 -0
- rankaudit-0.1.0/src/rankaudit/__init__.py +153 -0
- rankaudit-0.1.0/src/rankaudit/attribution/__init__.py +2 -0
- rankaudit-0.1.0/src/rankaudit/attribution/lime_adapter.py +125 -0
- rankaudit-0.1.0/src/rankaudit/attribution/shap_adapter.py +160 -0
- rankaudit-0.1.0/src/rankaudit/bias/__init__.py +1 -0
- rankaudit-0.1.0/src/rankaudit/bias/detector.py +168 -0
- rankaudit-0.1.0/src/rankaudit/core/__init__.py +3 -0
- rankaudit-0.1.0/src/rankaudit/core/pipeline.py +184 -0
- rankaudit-0.1.0/src/rankaudit/core/report.py +150 -0
- rankaudit-0.1.0/src/rankaudit/core/types.py +55 -0
- rankaudit-0.1.0/src/rankaudit/counterfactual/__init__.py +1 -0
- rankaudit-0.1.0/src/rankaudit/counterfactual/engine.py +169 -0
- rankaudit-0.1.0/src/rankaudit/explain/__init__.py +1 -0
- rankaudit-0.1.0/src/rankaudit/explain/rank_explain.py +164 -0
- rankaudit-0.1.0/src/rankaudit/loaders/__init__.py +2 -0
- rankaudit-0.1.0/src/rankaudit/loaders/dataframe.py +69 -0
- rankaudit-0.1.0/src/rankaudit/loaders/trec.py +153 -0
- rankaudit-0.1.0/src/rankaudit/metrics/__init__.py +1 -0
- rankaudit-0.1.0/src/rankaudit/metrics/ndcg.py +51 -0
- rankaudit-0.1.0/tests/__init__.py +0 -0
- rankaudit-0.1.0/tests/test_core.py +293 -0
rankaudit-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
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 made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other transformations
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner.
|
|
50
|
+
|
|
51
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
52
|
+
whom a Contribution has been received by the Licensor and included
|
|
53
|
+
within the Work.
|
|
54
|
+
|
|
55
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
56
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
57
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
58
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
59
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
60
|
+
Work and such Derivative Works in Source or Object form.
|
|
61
|
+
|
|
62
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
63
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
64
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
65
|
+
(except as stated in this section) patent license to make, have made,
|
|
66
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
67
|
+
where such license applies only to those patent claims licensable
|
|
68
|
+
by such Contributor that are necessarily infringed by their
|
|
69
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
70
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
71
|
+
institute patent litigation against any entity (including a cross-claim
|
|
72
|
+
or counterclaim in a lawsuit) alleging that the Work or any
|
|
73
|
+
Contribution incorporated within the Work constitutes patent or
|
|
74
|
+
contributory patent infringement, then any patent licenses granted to
|
|
75
|
+
You under this License for that Work shall terminate as of the date
|
|
76
|
+
such litigation is filed.
|
|
77
|
+
|
|
78
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
79
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
80
|
+
modifications, and in Source or Object form, provided that You
|
|
81
|
+
meet the following conditions:
|
|
82
|
+
|
|
83
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
84
|
+
Works a copy of this License; and
|
|
85
|
+
|
|
86
|
+
(b) You must cause any modified files to carry prominent notices
|
|
87
|
+
stating that You changed the files; and
|
|
88
|
+
|
|
89
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
90
|
+
that You distribute, all copyright, patent, trademark, and
|
|
91
|
+
attribution notices from the Source form of the Work,
|
|
92
|
+
excluding those notices that do not pertain to any part of
|
|
93
|
+
the Derivative Works; and
|
|
94
|
+
|
|
95
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
96
|
+
distribution, You must include a readable copy of the
|
|
97
|
+
attribution notices contained within such NOTICE file, in
|
|
98
|
+
at least one of the following places: within a NOTICE text
|
|
99
|
+
file distributed as part of the Derivative Works; within
|
|
100
|
+
the Source form or documentation, if provided along with the
|
|
101
|
+
Derivative Works; or, within a display generated by the
|
|
102
|
+
Derivative Works, if and wherever such third-party notices
|
|
103
|
+
normally appear. The contents of the NOTICE file are for
|
|
104
|
+
informational purposes only and do not modify the License.
|
|
105
|
+
|
|
106
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
107
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
108
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
109
|
+
this License, without any additional terms or conditions.
|
|
110
|
+
|
|
111
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
112
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
113
|
+
except as required for reasonable and customary use in describing the
|
|
114
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
115
|
+
|
|
116
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
117
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
118
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
119
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
120
|
+
implied, including, without limitation, any warranties or conditions
|
|
121
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
122
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
123
|
+
appropriateness of using or reproducing the Work and assume any
|
|
124
|
+
risks associated with Your exercise of permissions under this License.
|
|
125
|
+
|
|
126
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
127
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
128
|
+
unless required by applicable law (such as deliberate and grossly
|
|
129
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
130
|
+
liable to You for damages, including any direct, indirect, special,
|
|
131
|
+
incidental, or exemplary damages of any character arising as a
|
|
132
|
+
result of this License or out of the use or inability to use the
|
|
133
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
134
|
+
work stoppage, computer failure or malfunction, or all other
|
|
135
|
+
commercial damages or losses), even if such Contributor has been
|
|
136
|
+
advised of the possibility of such damages.
|
|
137
|
+
|
|
138
|
+
9. Accepting Warranty or Liability. While redistributing the Work or
|
|
139
|
+
Derivative Works thereof, You may choose to offer, and charge a fee
|
|
140
|
+
for, acceptance of support, warranty, indemnity, or other liability
|
|
141
|
+
obligations and/or rights consistent with this License. However, in
|
|
142
|
+
accepting such obligations, You may offer such obligations only on
|
|
143
|
+
Your own behalf and on Your sole responsibility, not on behalf of
|
|
144
|
+
any other Contributor, and only if You agree to indemnify, defend,
|
|
145
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
146
|
+
or claims asserted against, such Contributor by reason of your
|
|
147
|
+
accepting any warranty or additional liability.
|
|
148
|
+
|
|
149
|
+
END OF TERMS AND CONDITIONS
|
|
150
|
+
|
|
151
|
+
Copyright 2026 Mandar Narendra Parab
|
|
152
|
+
|
|
153
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
154
|
+
you may not use this file except in compliance with the License.
|
|
155
|
+
You may obtain a copy of the License at
|
|
156
|
+
|
|
157
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
158
|
+
|
|
159
|
+
Unless required by applicable law or agreed to in writing, software
|
|
160
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
161
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
162
|
+
See the License for the specific language governing permissions and
|
|
163
|
+
limitations under the License.
|
rankaudit-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rankaudit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Auditing and explainability library for ranking systems
|
|
5
|
+
Project-URL: Homepage, https://github.com/m-np/rankaudit
|
|
6
|
+
Project-URL: Repository, https://github.com/m-np/rankaudit
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/m-np/rankaudit/issues
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: LLM,audit,counterfactuals,explainability,fairness,information-retrieval,ranking
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Requires-Dist: numpy>=1.23
|
|
24
|
+
Provides-Extra: all
|
|
25
|
+
Requires-Dist: anthropic>=0.25; extra == 'all'
|
|
26
|
+
Requires-Dist: lime>=0.2; extra == 'all'
|
|
27
|
+
Requires-Dist: pandas>=2.0; extra == 'all'
|
|
28
|
+
Requires-Dist: shap>=0.44; extra == 'all'
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: mypy>=1.8; extra == 'dev'
|
|
31
|
+
Requires-Dist: pandas>=2.0; extra == 'dev'
|
|
32
|
+
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
|
|
33
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
34
|
+
Requires-Dist: ruff>=0.4; extra == 'dev'
|
|
35
|
+
Provides-Extra: lime
|
|
36
|
+
Requires-Dist: lime>=0.2; extra == 'lime'
|
|
37
|
+
Provides-Extra: llm
|
|
38
|
+
Requires-Dist: anthropic>=0.25; extra == 'llm'
|
|
39
|
+
Provides-Extra: pandas
|
|
40
|
+
Requires-Dist: pandas>=2.0; extra == 'pandas'
|
|
41
|
+
Provides-Extra: shap
|
|
42
|
+
Requires-Dist: shap>=0.44; extra == 'shap'
|
|
43
|
+
Description-Content-Type: text/markdown
|
|
44
|
+
|
|
45
|
+
# RankAudit
|
|
46
|
+
|
|
47
|
+
**Auditing and explainability toolkit for ranking systems.**
|
|
48
|
+
|
|
49
|
+
[](https://pypi.org/project/rankaudit/)
|
|
50
|
+
[](https://github.com/m-np/rankaudit/blob/main/LICENSE)
|
|
51
|
+
[](https://github.com/m-np/rankaudit/blob/main)
|
|
52
|
+
[](https://pypi.org/project/rankaudit/)
|
|
53
|
+
[](https://huggingface.co/datasets/m-np/arb)
|
|
54
|
+
|
|
55
|
+
> Ranking systems decide what people see and in what order — jobs, loans, news, products. RankAudit makes them inspectable.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## What is RankAudit?
|
|
60
|
+
|
|
61
|
+
Most ranking systems — search engines, recommendation feeds, LLM re-rankers are black boxes. They produce an ordered list with no explanation of *why* document A ranked above B, whether the ranking is fair across demographic groups, or what a lower-ranked item would need to change to move up.
|
|
62
|
+
|
|
63
|
+
RankAudit is a single Python library that closes this gap. It gives engineers and researchers four capabilities:
|
|
64
|
+
|
|
65
|
+
| Capability | What it answers |
|
|
66
|
+
|---|---|
|
|
67
|
+
| **Measure** | How good is the ranking? (NDCG, MAP, MRR, Precision@k) |
|
|
68
|
+
| **Explain** | Why did this document rank here? (ranking-aware SHAP/LIME) |
|
|
69
|
+
| **Interrogate** | What would it take for this document to rank higher? (counterfactuals) |
|
|
70
|
+
| **Detect** | Is the ranking fair across groups? (exposure, parity, position-relevance) |
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Why not existing tools?
|
|
75
|
+
|
|
76
|
+
| Tool | Gap |
|
|
77
|
+
|---|---|
|
|
78
|
+
| SHAP / LIME | Explain a single score in isolation — ignore that ranking is comparative |
|
|
79
|
+
| pytrec_eval / ranx | Compute quality metrics but give no insight into *why* or *where* unfairness lives |
|
|
80
|
+
| Fairlearn / AI Fairness 360 | Designed for classifiers, not ranked lists |
|
|
81
|
+
| **RankAudit** | Ranking-native attribution + counterfactuals + fairness, unified API |
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## Installation
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pip install rankaudit # core only (numpy)
|
|
89
|
+
pip install "rankaudit[shap]" # + SHAP attribution
|
|
90
|
+
pip install "rankaudit[lime]" # + LIME attribution
|
|
91
|
+
pip install "rankaudit[llm]" # + RankExplain (Anthropic / OpenAI)
|
|
92
|
+
pip install "rankaudit[pandas]" # + DataFrame loader
|
|
93
|
+
pip install "rankaudit[all]" # everything
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Quick start
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
import rankaudit as ra
|
|
102
|
+
|
|
103
|
+
report = ra.audit(
|
|
104
|
+
ranker=my_ranker,
|
|
105
|
+
queries=["best laptop under $1000"],
|
|
106
|
+
docs=doc_corpus,
|
|
107
|
+
metrics=["ndcg", "fairness"],
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
print(report.summary())
|
|
111
|
+
print(report.explain(doc_id="doc_42"))
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## Inputs
|
|
117
|
+
|
|
118
|
+
RankAudit accepts four input formats. All normalise to the same internal `QueryDocPair` representation.
|
|
119
|
+
|
|
120
|
+
### Option 1 — Raw queries + doc list (simplest)
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
queries = ["best laptop under $1000", "noise cancelling headphones"]
|
|
124
|
+
|
|
125
|
+
docs = [
|
|
126
|
+
{
|
|
127
|
+
"id": "doc_1",
|
|
128
|
+
"text": "The MacBook Air M3 offers excellent value...",
|
|
129
|
+
"relevance": 3, # graded relevance label (0–3), optional
|
|
130
|
+
"group": "apple", # group label for bias analysis, optional
|
|
131
|
+
"features": { # numeric features your ranker uses
|
|
132
|
+
"bm25": 0.82,
|
|
133
|
+
"semantic_sim": 0.91,
|
|
134
|
+
"recency": 0.74,
|
|
135
|
+
"click_rate": 0.55,
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
...
|
|
139
|
+
]
|
|
140
|
+
|
|
141
|
+
report = ra.audit(ranker=my_ranker, queries=queries, docs=docs)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Option 2 — QueryDocPair objects (full control)
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from rankaudit import QueryDocPair
|
|
148
|
+
|
|
149
|
+
pairs = [
|
|
150
|
+
QueryDocPair(
|
|
151
|
+
query_id="q1",
|
|
152
|
+
query_text="best laptop under $1000",
|
|
153
|
+
doc_id="doc_1",
|
|
154
|
+
doc_text="The MacBook Air M3 offers excellent value...",
|
|
155
|
+
features={"bm25": 0.82, "semantic_sim": 0.91, "recency": 0.74},
|
|
156
|
+
relevance=3.0,
|
|
157
|
+
metadata={"group": "apple"},
|
|
158
|
+
),
|
|
159
|
+
...
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
report = ra.audit(ranker=my_ranker, pairs=pairs)
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Option 3 — pandas DataFrame
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
import pandas as pd
|
|
169
|
+
import rankaudit as ra
|
|
170
|
+
|
|
171
|
+
df = pd.DataFrame({
|
|
172
|
+
"query_id": ["q1", "q1", "q1"],
|
|
173
|
+
"query_text": ["best laptop"] * 3,
|
|
174
|
+
"doc_id": ["doc_1", "doc_2", "doc_3"],
|
|
175
|
+
"doc_text": ["MacBook Air...", "Dell XPS...", "Lenovo ThinkPad..."],
|
|
176
|
+
"relevance": [3.0, 2.0, 1.0],
|
|
177
|
+
"feat_bm25": [0.82, 0.61, 0.55], # feat_ prefix → features dict
|
|
178
|
+
"feat_semantic_sim": [0.91, 0.78, 0.70],
|
|
179
|
+
"group": ["apple", "dell", "lenovo"],
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
pairs = ra.from_dataframe(df)
|
|
183
|
+
report = ra.audit(ranker=my_ranker, pairs=pairs)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Option 4 — TREC / LETOR benchmark files
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
pairs = ra.load_trec(qrel_path="robust04.qrel", run_path="bm25.run")
|
|
190
|
+
pairs = ra.load_letor(path="MQ2007/Fold1/train.txt")
|
|
191
|
+
report = ra.audit(ranker=my_ranker, pairs=pairs)
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Implementing a ranker
|
|
195
|
+
|
|
196
|
+
Any object with a `score` method works — scikit-learn estimators, PyTorch modules, ONNX sessions, and plain callables all qualify.
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
class MyRanker:
|
|
200
|
+
def score(self, pairs: list[QueryDocPair]) -> list[tuple[str, float]]:
|
|
201
|
+
# Return (doc_id, score) for every pair — order doesn't matter
|
|
202
|
+
return [(p.doc_id, sum(p.features.values())) for p in pairs]
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### What is required vs optional on each input
|
|
206
|
+
|
|
207
|
+
| Field | Required? | Used by |
|
|
208
|
+
|---|---|---|
|
|
209
|
+
| `query_id`, `query_text` | Yes | All steps |
|
|
210
|
+
| `doc_id`, `doc_text` | Yes | All steps |
|
|
211
|
+
| `features` | No | Attribution (SHAP/LIME) and counterfactuals only |
|
|
212
|
+
| `relevance` | No | IR metrics (NDCG, MAP, MRR, Precision@k) — all return 0 if omitted |
|
|
213
|
+
| `metadata["group"]` | No | Bias detector — exposure and parity analysis only run when group labels are present |
|
|
214
|
+
|
|
215
|
+
**What you get without optional fields:**
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
# No features, no relevance, no group labels — still works
|
|
219
|
+
pairs = [
|
|
220
|
+
QueryDocPair(query_id="q1", query_text="query", doc_id="d1", doc_text="doc text"),
|
|
221
|
+
...
|
|
222
|
+
]
|
|
223
|
+
|
|
224
|
+
# Skip the steps that need features
|
|
225
|
+
report = ra.audit(
|
|
226
|
+
ranker=my_ranker,
|
|
227
|
+
pairs=pairs,
|
|
228
|
+
attribution=None, # requires features
|
|
229
|
+
counterfactuals=False, # requires features
|
|
230
|
+
)
|
|
231
|
+
# → report.metrics will have NDCG/MAP of 0 (no relevance labels)
|
|
232
|
+
# → report.bias will have position_bias only (no group labels)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
**Full audit requires:**
|
|
236
|
+
- `features` — for attribution and counterfactuals
|
|
237
|
+
- `relevance` — for meaningful NDCG, MAP, MRR, Precision@k values
|
|
238
|
+
- `metadata["group"]` — for exposure and demographic parity bias analysis
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## Outputs
|
|
243
|
+
|
|
244
|
+
`ra.audit(...)` returns an `AuditReport` object with four output surfaces.
|
|
245
|
+
|
|
246
|
+
### report.summary()
|
|
247
|
+
|
|
248
|
+
```
|
|
249
|
+
=== RankAudit Report ===
|
|
250
|
+
Queries audited : 2
|
|
251
|
+
Documents ranked: 20
|
|
252
|
+
Avg NDCG@10 : 0.7841
|
|
253
|
+
Attributions : 20 documents explained
|
|
254
|
+
Counterfactuals : 4 flip examples
|
|
255
|
+
Bias checks : 2 queries analysed
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### report.metrics — ranking quality
|
|
259
|
+
|
|
260
|
+
All four metrics require `relevance` labels on your input pairs. They return 0 if no relevance labels are provided.
|
|
261
|
+
|
|
262
|
+
| Metric | What it measures | `metrics=` value |
|
|
263
|
+
|---|---|---|
|
|
264
|
+
| **NDCG@k** | Quality of the top-k ranking, weighted by position | `"ndcg"` |
|
|
265
|
+
| **Precision@k** | Fraction of relevant docs in the top-k | `"precision"` |
|
|
266
|
+
| **MAP** | Average precision across all relevant docs | `"map"` |
|
|
267
|
+
| **MRR** | How high the first relevant doc appears | `"mrr"` |
|
|
268
|
+
| **Fairness / bias** | Exposure and parity across groups | `"fairness"` |
|
|
269
|
+
|
|
270
|
+
Pass any combination to `ra.audit(metrics=[...])`. Default is `["ndcg", "fairness"]`.
|
|
271
|
+
|
|
272
|
+
```python
|
|
273
|
+
for m in report.metrics:
|
|
274
|
+
print(m.query_id, m.ndcg_at_k, m.map_score, m.mrr_score)
|
|
275
|
+
|
|
276
|
+
# q1 {1: 1.0, 3: 0.92, 5: 0.87, 10: 0.78} 0.81 1.0
|
|
277
|
+
# q2 {1: 0.5, 3: 0.71, 5: 0.74, 10: 0.69} 0.67 0.5
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### report.explain() — feature attribution
|
|
281
|
+
|
|
282
|
+
Ranking-aware attribution showing which features drove a document's position:
|
|
283
|
+
|
|
284
|
+
```python
|
|
285
|
+
print(report.explain(doc_id="doc_2"))
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
```
|
|
289
|
+
Explanation for doc 'doc_2' (query 'q1') — method: shap
|
|
290
|
+
Base score : 0.5312
|
|
291
|
+
Final score : 0.7841
|
|
292
|
+
Top feature contributions:
|
|
293
|
+
semantic_sim +0.1820
|
|
294
|
+
bm25 +0.0934
|
|
295
|
+
recency -0.0381
|
|
296
|
+
click_rate -0.0144
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### report.counterfactuals — rank flip analysis
|
|
300
|
+
|
|
301
|
+
Minimal feature change that would flip the order of two documents:
|
|
302
|
+
|
|
303
|
+
```python
|
|
304
|
+
for cf in report.counterfactuals:
|
|
305
|
+
print(f"Query {cf.query_id}: '{cf.doc_b_id}' (rank {cf.original_rank_b}) "
|
|
306
|
+
f"would overtake '{cf.doc_a_id}' (rank {cf.original_rank_a}) if:")
|
|
307
|
+
for feat, (current, needed) in cf.flipping_changes.items():
|
|
308
|
+
print(f" {feat}: {current:.3f} → {needed:.3f}")
|
|
309
|
+
|
|
310
|
+
# Query q1: 'doc_2' (rank 2) would overtake 'doc_1' (rank 1) if:
|
|
311
|
+
# recency: 0.450 → 0.720
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### report.bias — fairness metrics
|
|
315
|
+
|
|
316
|
+
Exposure and demographic parity across group labels:
|
|
317
|
+
|
|
318
|
+
```python
|
|
319
|
+
for b in report.bias:
|
|
320
|
+
print(b.query_id, b.exposure_bias, b.demographic_parity)
|
|
321
|
+
for note in b.notes:
|
|
322
|
+
print(" [!]", note)
|
|
323
|
+
|
|
324
|
+
# q1 {'apple': 0.4821, 'dell': 0.2103, 'lenovo': 0.1874} ...
|
|
325
|
+
# [!] Exposure ratio between groups is 2.57x — significant disparity detected.
|
|
326
|
+
# [!] Demographic parity gap is 0.50 — one group dominates top-k results.
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### RankExplain — plain-English explanations via LLM
|
|
330
|
+
|
|
331
|
+
```python
|
|
332
|
+
from anthropic import Anthropic
|
|
333
|
+
from rankaudit import RankExplain
|
|
334
|
+
|
|
335
|
+
explainer = RankExplain(client=Anthropic(), model="claude-sonnet-4-6")
|
|
336
|
+
|
|
337
|
+
print(explainer.explain_report(report))
|
|
338
|
+
print(explainer.explain_doc(report, doc_id="doc_2"))
|
|
339
|
+
print(explainer.explain_counterfactuals(report))
|
|
340
|
+
print(explainer.explain_bias(report))
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
```
|
|
344
|
+
The ranking system performs well overall (NDCG@10: 0.78) but shows a notable
|
|
345
|
+
recency penalty — newer documents are discounted by ~30% relative to older ones
|
|
346
|
+
with comparable semantic scores. The 'apple' group receives 2.5x more exposure
|
|
347
|
+
than other groups, which may warrant investigation if group identity should not
|
|
348
|
+
influence ranking position...
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
### Serialisation
|
|
352
|
+
|
|
353
|
+
```python
|
|
354
|
+
report.to_json() # full report as a JSON string
|
|
355
|
+
report.to_dict() # plain Python dict
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
---
|
|
359
|
+
|
|
360
|
+
## Citation
|
|
361
|
+
|
|
362
|
+
If you use RankAudit in your research, please cite:
|
|
363
|
+
|
|
364
|
+
```bibtex
|
|
365
|
+
@software{parab2026rankaudit,
|
|
366
|
+
author = {Parab, Mandar Narendra},
|
|
367
|
+
title = {{RankAudit}: Auditing and Explainability for Ranking Systems},
|
|
368
|
+
year = {2026},
|
|
369
|
+
url = {https://github.com/m-np/rankaudit},
|
|
370
|
+
license = {Apache-2.0}
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
## Contributing
|
|
377
|
+
|
|
378
|
+
Contributions are welcome. Please open an issue before submitting a large PR so we can align on direction. See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
379
|
+
|
|
380
|
+
---
|
|
381
|
+
|
|
382
|
+
## License
|
|
383
|
+
|
|
384
|
+
Copyright 2026 Mandar Narendra Parab. Licensed under the [Apache License 2.0](https://github.com/m-np/rankaudit/blob/main/LICENSE).
|