Özer Cem KELAHMETKELAHMET
AI Simulations · Retrieval (RAG)

nDCG@k, precision@k and reciprocal rank: scoring a ranked retrieval list step by step

Before you tune a RAG retriever you need one number that says whether the ranking got better. Here twenty passages for one question, judged 0 to 3, are scored with nDCG@k, precision@k, recall@k and reciprocal rank exactly as trec_eval and scikit-learn compute them.

Download Markdown

Interactive simulation. It needs JavaScript; the text below explains what it shows.

What you are looking at

A retriever has returned twenty passages for one question, in rank order. Each passage carries a relevance grade from a human judge or an LLM judge: 0 irrelevant, 1 related, 2 highly relevant, 3 perfectly relevant. This four-level scale is the kind of multi-graded judgement used in the TREC Deep Learning track, which was built precisely so that rankings can be scored with nDCG.

The dots on the chart are the grades at each rank. The dashed curve is the most a rank could possibly earn (the top grade times the position discount), and the second curve is the discount itself. The fitted points show what each rank actually earned; beyond the cut-off k they drop to zero. The bar chart shows the same discounted gains side by side, so you can see at a glance which positions carry the score.

The table holds the inputs (rank and grade) and the live columns: the discount, the gain, the discounted gain, whether the passage counts as relevant, and the grade the ideal ranking would put at that position. The results panel shows DCG@k, the ideal DCG@k, nDCG@k, precision@k, recall@k and the reciprocal rank.

The algorithm

nDCG answers one question: how close is this ordering to the best ordering of the same passages? It is built in four moves.

  1. Gain. Each grade becomes a gain. trec_eval's ndcg_cut and scikit-learn's ndcg_score use the grade itself as the gain. Many learning-to-rank papers use 2^grade − 1 instead, which makes a perfect passage worth 7 against 3 for a highly relevant one.
  2. Discount. The gain at rank i is divided by log2(i + 1). Rank 1 keeps all of it, rank 3 keeps half, rank 10 keeps 29%. The reader is assumed to stop reading as they go down.
  3. DCG@k is the sum of discounted gains over the top k.
  4. Normalise. Sort the same judged passages by grade, compute their DCG@k, and divide. The result is between 0 and 1, so queries with many and few relevant passages can be averaged.

Precision@k, recall@k and reciprocal rank need a yes-or-no label, so a grade bar decides what counts as relevant. Precision@k is the share of the top k that is relevant; recall@k is the share of all relevant passages that made it into the top k. Reciprocal rank is 1 divided by the rank of the first relevant passage, and MRR is its mean over all test questions. RR only cares about the first good answer, which suits a chatbot that reads one passage; nDCG cares about the whole ordering, which suits RAG that stuffs the top k into the prompt.

Worked example

With the defaults (k = 10, relevant means grade 2 or more, linear gain) the top ten grades are 1, 0, 3, 2, 0, 3, 1, 2, 0, 0. The discounted gains are 1.000, 0, 1.500, 0.861, 0, 1.069, 0.333, 0.631, 0, 0, so DCG@10 = 5.394. The ideal ordering puts the three grade-3 passages first, then the four grade-2 ones, then three of the grade-1 ones: IDCG@10 = 10.312. nDCG@10 = 5.394 / 10.312 = 52.3%: the right passages were found, but too low. Four relevant passages (ranks 3, 4, 6 and 8) are in the top ten, so precision@10 = 40% and recall@10 = 4 / 7 = 57.1%. The first relevant passage is at rank 3, so RR = 0.333.

Things to try

  • Switch the gain to exponential. nDCG@10 falls from 52.3% to 47.0%, because the ideal list now earns 7 for each perfect passage and this run buried one of them at rank 11.
  • Edit the table so the first passage has grade 3 and the third has grade 1. nDCG@10 jumps to 62.0% and RR to 1.0, though the same ten passages were retrieved: ranking metrics reward order, not just membership.
  • Set k to 2. No relevant passage is in the top two, so RR@2 is 0 and nDCG@2 is 20.4%. Short cut-offs punish a slow start hard.
  • Lower the relevance bar to grade 1. The first passage now counts, RR becomes 1.0 and precision@10 rises to 60%. nDCG does not change, because it uses the grades directly.
  • Raise k to 20. nDCG@20 is 73.0% and recall reaches 100%, but precision falls to 35%: a long context finds everything and dilutes it.

How it works in real systems

RAG teams usually report recall@k for the retriever (is the needed chunk in what reaches the model?) and nDCG@10 or MRR@10 for the ranking that a reranker produces. The MS MARCO passage ranking task is scored with MRR, because its labels are sparse: usually one passage per query is marked relevant and most passages were never seen by a judge. The TREC Deep Learning track pooled the top results of many systems and had NIST assessors grade them on several levels, so that nDCG could be measured.

Two details change the numbers in practice. First, the ideal DCG in trec_eval is computed from every judged relevant document in the qrels, not only those you retrieved, so missing a relevant passage lowers nDCG even if your list looks well ordered. This simulation assumes all judged passages are in the list. Second, unjudged passages are treated as irrelevant, which is why LLM judges are now used to fill the gaps. Compute these metrics before and after you add hybrid search or a cross-encoder reranker, and pair them with answer-level checks such as RAGAS faithfulness and context recall. For the keyword half of retrieval see BM25 scoring.

Sources

Last updated Nov 24, 2026
Type to search.
    ↑↓ navigate↵ open