Sub topic 5
Rank-aware metrics: MRR, MAP, and nDCG
Precision@k and recall@k treat the top k results as an unordered set. A relevant document at rank 1 scores the same as one at rank k. Users and generators both care about order, because the top passages get read first and often get the most weight in the prompt. This chapter assumes you can read Python, know precision and recall, and have the labelled evaluation set from the earlier chapters.
Reciprocal rank and MRR
Reciprocal rank (RR) is 1 divided by the rank of the first relevant document. It is 1.0 at rank 1, 0.5 at rank 2, and 0 if nothing relevant is retrieved. Mean reciprocal rank (MRR) is the average of RR over all questions.
Use MRR when one good passage is enough to answer, as in factoid question answering. It ignores everything after the first hit.
Average precision and MAP
Average precision (AP) rewards a ranking that puts all relevant documents early. At each rank where a relevant document appears, take the precision at that rank. Sum those values and divide by the total number of relevant documents. Relevant documents that are never retrieved add 0 to the sum, so missing them lowers the score. Mean average precision (MAP) is AP averaged over questions.
Use MAP when a question has several relevant passages and the answer needs most of them.
def average_precision(retrieved_ids, relevant_ids):
if not relevant_ids:
return None # undefined without labels
hits = 0
total = 0.0
for rank, d in enumerate(retrieved_ids, start=1):
if d in relevant_ids:
hits += 1
total += hits / rank
return total / len(relevant_ids)
def mean_of(values):
defined = [v for v in values if v is not None]
return sum(defined) / len(defined) if defined else None
retrieved = ["d7", "d2", "d9", "d1"]
relevant = {"d2", "d1", "d5"}
print(average_precision(retrieved, relevant)) # (1/2 + 2/4) / 3 = 0.333...
print(mean_of([average_precision(retrieved, relevant),
average_precision(["d5", "d2"], relevant)]))Skipping questions with no labels, as mean_of does, keeps undefined values from counting as zero.
nDCG
Normalized discounted cumulative gain (nDCG) handles graded labels, such as 0 to 3. Each document contributes its gain divided by log2(rank + 1), so lower ranks count less. The sum is the discounted cumulative gain (DCG). nDCG divides DCG by the DCG of the ideal ordering, which gives a score from 0 to 1.
Using the ndcg_at_k function from the spine, the ranking d7, d2, d9, d1 with grades d2=3, d1=2, d5=1 gives gains [0, 3, 0, 2].
- DCG = 3/log2(3) + 2/log2(5) ≈ 1.893 + 0.861 = 2.754
- Ideal DCG, from gains
[3, 2, 1], = 3 + 1.262 + 0.5 = 4.762 - nDCG ≈ 0.578
The score is low for two reasons. A non-relevant document sits at rank 1, and the grade-1 document d5 was never retrieved.
Choosing a metric
| Metric | Labels needed | Looks at | Best for |
|---|---|---|---|
| MRR | Binary | First relevant hit only | One passage is enough |
| MAP | Binary | All relevant hits and their positions | Several passages needed |
| nDCG@k | Graded | Top k with position discount | Passages differ in usefulness |
Common errors
- Wrong ideal ranking. The ideal DCG must be built from all labelled documents, not only the retrieved ones. Otherwise a retriever that misses the best passage can still score 1.0.
- Mixed cutoffs. Compare nDCG@5 with nDCG@5, never with nDCG@10.
- Undefined questions. Questions with no relevant labels should be excluded and counted, not scored 0.
- Binary labels in nDCG. With 0/1 grades, nDCG still works but loses its main advantage. Consider MAP instead.
Exercise
A question has relevant documents {a, b}. The retriever returns [x, a, y, b]. Compute RR and AP.
Show answer
The first relevant document is at rank 2, so RR = 1/2 = 0.5. The relevant hits are at rank 2 (precision 1/2) and rank 4 (precision 2/4). AP = (0.5 + 0.5) / 2 = 0.5.
Exercise
A retriever scores a high MRR but a low MAP on a dataset where most questions have four relevant passages. What does that suggest?
Show answer
The retriever usually places one relevant passage near the top, which gives the high MRR. It fails to retrieve or rank the other relevant passages, which gives the low MAP. Answers that need several passages are likely to be incomplete.