← Back to Pull requests

[fix] Average MAP over tied scores, like MRR and NDCG #4416

Open
unknown opened this pull request with 486 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 19, 2026 17:29 UTC
Author

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:

from sklearn.metrics import average_precision_score
from sentence_transformers.util.metrics import _reciprocal_rank

labels, scores = [1, 0, 0, 0], [1.0, 1.0, 1.0, 1.0]
average_precision_score(labels, scores)    # 0.25
_reciprocal_rank(labels, scores, k=10)     # 0.5208...

The one-positive case in test_reranking_evaluator_mrr (one positive tied with four negatives) shows the same thing at the evaluator level: mrr@10 is 0.457 and map is 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_retrievers the fused ranking is n1 followed by the tied positives p1, p2, so the AP is exactly (1/2 + 2/3) / 2 = 7/12 whichever way the tie breaks, while sklearn reports 2/3.

Fix

  • Add _average_precision to util/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 of n documents with m positives, each position is relevant with probability m / n, and given that, (m - 1) / (n - 1) of the positions before it within the group are relevant on average. Without ties it equals average_precision_score.

  • Use it in RerankingEvaluator (both the batched and individual paths, so SparseRerankingEvaluator and MultiVectorRerankingEvaluator follow), CrossEncoderRerankingEvaluator and ReciprocalRankFusionEvaluator. 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_precision matches a brute-force average over every ordering on six tie patterns.

  • For a single positive it equals _reciprocal_rank with k covering the whole ranking.

  • Without ties it matches average_precision_score.

  • No positives gives 0.0.
  • test_reranking_evaluator_mrr now also asserts map == mrr for its single-positive cases that fit within at_k. It fails on main for both use_batched_encoding values.

    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.

  • A local fuzz run (not committed) compared _average_precision against brute force on 2,000 random lists of up to 7 documents with heavy ties: max absolute difference 2.2e-16.

  • pre-commit run on 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.

    Diff: 486 lines (+364 / -122) across 6 files Reviewer: 1AbdullahButt Review duration: 12337m
    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
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.3% (MEDIUM) · Higher = larger blast radius
    47.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.2% · Safe to merge
    25.2%