Lesson 26.4 · 4 of 7 · 6 min read · Reviewed 24 Nov 2026
Ranking metrics: MRR and nDCG
By the end, you can
- Compute reciprocal rank, MRR and nDCG by hand.
- Choose a ranking metric that matches how your RAG system uses results.
Why order matters
Recall@5 (26.1) asks one yes or no question: is a relevant chunkChunking: Splitting documents into smaller passages before embedding and indexing them. somewhere in the top 5? It gives the same score whether that chunk sits at rank 1 or rank 5. In practice the position often matters:
- You pass only a few chunks to the model, so a rerankerReranking: Re-scoring an initial set of retrieved results with a more accurate model to put the best ones first.’s job is to push the best one up.
- Models tend to use early passages more reliably than ones buried in a long context.
- A citation UI shows the top result first, and users trust it.
Ranking metrics measure that order.
Reciprocal rank and MRR
For one question, the reciprocal rank (RR) is 1 divided by the rank of the first relevant chunk. If nothing relevant comes back, RR is 0. Mean reciprocal rank (MRR) is the average RR over your test set.
| Question | First relevant at | RR |
|---|---|---|
| q1 | 1 | 1.00 |
| q2 | 3 | 0.33 |
| q3 | not found | 0.00 |
| q4 | 2 | 0.50 |
| MRR | 0.46 |
MRR suits questions with one right passage, such as “what is the refund window?”. It ignores everything after the first hit.
nDCG: graded relevance with a discount
Some questions have several useful chunks, and some chunks are more useful than others. nDCG (normalised discounted cumulative gain) handles both.
- Give each retrieved chunk a grade, for example 2 = answers the question, 1 = partly helps, 0 = not relevant.
- DCG@k: add up each grade divided by log2(rank + 1). Rank 1 divides by 1, rank 3 by 2, so lower ranks count for less.
- IDCG@k: the DCG of the ideal order, with the best grades first.
- nDCG@k = DCG@k / IDCG@k, a number from 0 to 1.
Worked example, top 5 grades in retrieved order: 0, 2, 1, 0, 1.
DCG = 0/1 + 2/1.585 + 1/2 + 0/2.322 + 1/2.585 = 0 + 1.262 + 0.500 + 0 + 0.387 = 2.149
ideal order 2, 1, 1, 0, 0
IDCG = 2/1 + 1/1.585 + 1/2 = 2 + 0.631 + 0.500 = 3.131
nDCG@5 = 2.149 / 3.131 = 0.69
The same five chunks in the ideal order would score 1.0. Recall@5 would score both orders the same. Some libraries use a gain of 2^grade minus 1 instead of the grade itself, which rewards the top grade more strongly; say which you use when you report.
Which one to report
| Situation | Metric |
|---|---|
| Does the model see a relevant chunk at all? | Recall@k at the k you pass |
| One right passage per question | MRR |
| Several passages with different usefulness | nDCG@k |
| Comparing rerankers | nDCG@k or MRR, with recall@k beside it |
Common mistakes
- Averaging ranks instead of reciprocals. A mean rank of 2.5 is not an MRR.
- Comparing nDCG across different k. nDCG@3 and nDCG@10 are different numbers; fix k.
- Binary grades by accident. If every grade is 0 or 1, nDCG still works but loses the “partly helps” signal you may care about.
- Reporting a ranking metric alone. A reranker can raise MRR on found questions while recall stays poor. Show both.
Key points
- Recall@k ignores order inside the top k; ranking metrics reward putting the best chunk first.
- MRR averages 1 / rank of the first relevant chunk, so it cares only about the first hit.
- nDCG uses graded relevance and a log discount, then divides by the best possible score, giving 0 to 1.
- Use ranking metrics when order matters: reranking, small k, or a model that leans on the first passages.
Compute MRR and nDCG by hand
Implement reciprocal rank, MRR and nDCG@k and see how reordering the same chunks changes them.
You will have: A script with mrr and ndcg functions that reproduces the lesson's worked example.
Steps
- Write reciprocal_rank: 1 / rank of the first relevant chunk, or 0.
- Average it over four questions to get MRR.
- Write dcg(grades, k) with the log2(rank + 1) discount, and ndcg as DCG over the ideal DCG.
- Score the lesson's ranking 0, 2, 1, 0, 1 and the reranked order 2, 1, 1, 0, 0.
- Check that recall@5 is the same for both orders while nDCG differs.
Done when
- MRR prints as 0.46 for the four questions.
- nDCG@5 prints as 0.69 for the retrieved order and 1.00 for the ideal order.
- Recall@5 is a hit for both orders.
Open in the Playground No API key needed.
Starter code (Python)
import math
def reciprocal_rank(grades):
# TODO: 1 / (position of the first grade > 0, counting from 1), or 0 if none
return 0.0
def dcg(grades, k):
# TODO: sum of grade / log2(rank + 1) over the top k
return 0.0
def ndcg(grades, k):
ideal = sorted(grades, reverse=True)
best = dcg(ideal, k)
return dcg(grades, k) / best if best else 0.0
questions = [[1, 0, 0], [0, 0, 1], [0, 0, 0], [0, 1, 0]]
print("MRR =", sum(reciprocal_rank(q) for q in questions) / len(questions))
for name, g in [("retrieved", [0, 2, 1, 0, 1]), ("reranked", [2, 1, 1, 0, 0])]:
print(name, "nDCG@5 =", ndcg(g, 5))
Show a solution
import math
def reciprocal_rank(grades):
for i, g in enumerate(grades):
if g > 0:
return 1 / (i + 1)
return 0.0
def dcg(grades, k):
return sum(g / math.log2(i + 2) for i, g in enumerate(grades[:k]))
def ndcg(grades, k):
ideal = sorted(grades, reverse=True)
best = dcg(ideal, k)
return dcg(grades, k) / best if best else 0.0
questions = [[1, 0, 0], [0, 0, 1], [0, 0, 0], [0, 1, 0]]
print(f"MRR = {sum(reciprocal_rank(q) for q in questions) / len(questions):.2f}")
for name, g in [("retrieved", [0, 2, 1, 0, 1]), ("reranked", [2, 1, 1, 0, 0])]:
hit = any(x > 0 for x in g[:5])
print(f"{name:9} DCG@5 = {dcg(g, 5):.3f} nDCG@5 = {ndcg(g, 5):.2f} recall@5 = {'hit' if hit else 'miss'}")