Retrieval evaluation
Same precision, different ranking: a worked nDCG example
Compare two fictional retrieval runs with identical Precision@3 and Recall@3 but different binary nDCG@3.
By RAG Compare editorial · Published
Original worked examples are hypothetical, not measured product results. Sources checked 2026-11-09.
Count the right documents, then inspect their order
Two retrievers can find the same number of relevant documents yet present them in a different order. Precision and recall describe the set of results inside a cutoff. nDCG adds a preference for relevant results appearing earlier. Those questions are related, but they are not interchangeable.
This walkthrough uses a tiny fictional corpus and binary relevance labels. It is a handworked example, not a benchmark result or evidence that one retrieval method is generally better. RAG Compare accepts ranked document IDs and supplied judgments; it does not retrieve documents or decide which ones are relevant.
Set up a fully judged example
Suppose the query is “How can I reset a device without deleting saved settings?” In our invented five-document corpus, guide-a and guide-b answer that question. Guide-c, guide-d and guide-e do not. Mark guide-a and guide-b as 1, and the other three as 0. The complete relevant set contains two documents.
Run A returns guide-c, guide-a, guide-b. Run B returns guide-a, guide-c, guide-b. Set k to 3 and declare the relevance set complete only because this fictional example defines the whole corpus. Both runs return three judged documents and include the same two relevant items.
The distinction is at rank 1: Run B puts an answer-bearing document first. At rank 3 both runs have recovered the same relevant set. A metric that only counts hits inside the top three cannot express the earlier placement.
Work out precision and recall
Precision@3 divides the relevant hits in the first three positions by three. Both runs have two hits, so each scores 2/3, approximately 0.6667. Recall@3 divides those hits by the two relevant documents in the complete relevant set. Both score 2/2, or 1.
RAG Compare uses k as the precision denominator, even when a run returns fewer than k documents. Empty ranking positions do not become hits. This is an explicit tool convention to keep in mind when reproducing the calculation in another evaluator.
Neither result says that the generated answer is correct. A generation step could ignore both relevant documents or misread them. Retrieval evaluation isolates one part of a larger system; answer quality still requires its own checks.
Technical references: NIST TREC common evaluation measures · Microsoft RAG evaluators
Calculate binary nDCG by hand
In this workbench, a relevant item has gain 1 and a nonrelevant item gain 0. Divide each gain by log2(rank + 1), then add the discounted gains. The discount is 1 at rank 1, approximately 0.63093 at rank 2, and 0.5 at rank 3.
Run A has gains 0, 1, 1, so its DCG@3 is 0 + 0.63093 + 0.5 = 1.13093. Run B has gains 1, 0, 1, so its DCG@3 is 1 + 0 + 0.5 = 1.5.
The ideal ranking puts the two relevant documents first, followed by a nonrelevant document. Ideal DCG@3 is therefore 1 + 0.63093 = 1.63093. Divide each run’s DCG by that ideal: Run A’s nDCG@3 is approximately 0.6934 and Run B’s is approximately 0.9197. The increase reflects earlier placement, not extra relevant documents found.
Technical references: scikit-learn nDCG documentation
A different cutoff asks a different question
At k = 1, Run A has precision 0 and recall 0, while Run B has precision 1 and recall 0.5. At k = 2, both runs have precision 0.5 and recall 0.5, although Run B still places the relevant document earlier. At k = 3, the set-based metrics tie again at their original values.
Use the cutoff chart to see how this pattern develops, not to select whichever k makes a preferred run look best. Choose the operational cutoff using the application’s needs, then evaluate the choice on data that was not used to tune it. A visual improvement on one invented query is not statistical evidence.
Use the comparison to decide what to inspect
If your application shows one result immediately, rank 1 may deserve particular attention. If it sends three chunks into a generation step, all three are available, but ordering and the actual content can still matter. Connect the metric to the real workflow rather than treating a larger number as a complete product decision.
Before comparing your own runs, verify that the corpus, query set, document IDs, cutoff and judgment policy match. Keep unknown judgments unknown. Record the exact evaluation setup with the scores so that a future comparison can be repeated rather than guessed.
Sources and scope
Provider rules and supported features can change. These sources support the technical background; the worked examples and decision checklists are authored for this article.
- NIST TREC common evaluation measures
Definitions of precision and recall; opened 9 November 2026. The fixed-k convention is the app’s explicit policy.
- scikit-learn nDCG documentation
Discounted gain normalised by ideal gain; opened 9 November 2026. This article uses the app’s binary specialization.
- Microsoft RAG evaluators
Separates retrieval/process evaluation from groundedness/system evaluation; opened 9 November 2026.