[fix] Average MAP over tied scores, like MRR and NDCG #4416
All Vouch review confidence checks passed
Residual risk 0.25 = Change Risk (0.47) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.47 · 486 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.370
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #4416"
general
weight: 0.35
1AbdullahButt
Follow-up to #4412.
Problem
#4412 made MRR average over all orderings of tied scores, and NDCG already did that through sklearn. MAP still uses
sklearn.metrics.average_precision_score, so with ties the three metrics disagree. For a single relevant document, average precision is by definition its reciprocal rank, but:The
one-positivecase intest_reranking_evaluator_mrr(one positive tied with four negatives) shows the same thing at the evaluator level:mrr@10is 0.457 andmapis 0.2.Cause
With ties, sklearn credits every relevant document in a tied group with the precision at the end of that group. That isn't the AP of any actual ordering, and it isn't always pessimistic either: in
test_complementary_retrieversthe fused ranking isn1followed by the tied positivesp1,p2, so the AP is exactly (1/2 + 2/3) / 2 = 7/12 whichever way the tie breaks, while sklearn reports 2/3.Fix
_average_precisiontoutil/metrics.py, next to_reciprocal_rank. It computes the expected AP over all orderings of tied scores in closed form, walking the tied groups from best to worst. In a group ofndocuments withmpositives, each position is relevant with probabilitym / n, and given that,(m - 1) / (n - 1)of the positions before it within the group are relevant on average. Without ties it equalsaverage_precision_score.RerankingEvaluator(both the batched and individual paths, soSparseRerankingEvaluatorandMultiVectorRerankingEvaluatorfollow),CrossEncoderRerankingEvaluatorandReciprocalRankFusionEvaluator. The CrossEncoder and RRF scaling for unretrieved positives is unchanged.Like #4412, this changes reported MAP values whenever scores tie. Scores without ties are unchanged.
Tests
New, in
tests/util/test_metrics.py:_average_precisionmatches a brute-force average over every ordering on six tie patterns._reciprocal_rankwithkcovering the whole ranking.average_precision_score.test_reranking_evaluator_mrrnow also assertsmap == mrrfor its single-positive cases that fit withinat_k. It fails onmainfor bothuse_batched_encodingvalues.Four existing assertions pinned sklearn's value for tied scores. Each new value was checked by hand:
| Test | Before | After | Reason |
|---|---|---|---|
|
test_missing_positives_with_tied_scores(CrossEncoder) | 0.25 | 0.375 | (1 + 1/2) / 2, scaled by the 1 of 2 positives retrieved ||
test_tied_scores(RRF) | 0.5 / 0.25 | 0.75 / 0.375 | same as above, without / with the missing positive ||
test_rrf_ties_average_metrics| 0.5 | 0.75 | single positive, so equal to the 0.75 MRR the same test asserts ||
test_complementary_retrievers| 2/3 | 7/12 | positives at ranks 2 and 3 in either order |Validation
pytest tests/util/test_metrics.py tests/sentence_transformer/evaluation tests/cross_encoder/evaluation tests/sparse_encoder tests/multi_vector_encoder/evaluation -q -n 6: 765 passed, 15 skipped._average_precisionagainst brute force on 2,000 random lists of up to 7 documents with heavy ties: max absolute difference 2.2e-16.pre-commit runon the changed files: ruff check, ruff format and typos passed.Versions: sentence-transformers 6.4.0.dev0 (main @ 815cafd2), transformers 5.22.1, torch 2.15.0+cpu, Python 3.12.2, Windows 11.
AI assistance
I used Claude Code to find the bug, write the fix and the regression tests, and draft this description.