WRITE · TRACE · UNDERSTAND

Evaluate retrieval with relevance and rank

Implement retrieval_metrics(ranked,relevant,k) returning dict recall,mrr.

Read lesson: Evaluate retrieval with relevance and rank →

Your experiment

Implement retrieval_metrics(ranked,relevant,k) returning dict recall,mrr. Deduplicate ranked preserving order, then take k. mrr is reciprocal first relevant rank or 0. No relevant ids gives None values. Require k>=1 integer.

Python runs in your browser. Your draft stays on this device.

What to look for

Retrieval metrics evaluate the judged identifiers and ordering. They do not establish answer faithfulness, citation correctness or resistance to prompt injection; those require separate evaluation cases.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Evaluate retrieval with relevance and rank works

The idea

Two numbers describe how well a retriever did on one query. Recall at k is the share of the relevant documents that appear in the top k results, which tells you whether the evidence got in at all. Reciprocal rank is 1 divided by the position of the first relevant result, which tells you how far a reader or a model must look before finding something useful. Averaged over many queries it is called MRR, the key name this Lab uses.

Both are reported because they can disagree. A list can hold every relevant document near the bottom, or one relevant document at the top and nothing else. When no relevant ids are known, retrieval_metrics returns None for both, since 0 would report a failure that was never measured.

How it works

  1. Require k to be an integer of at least 1.
  2. Turn relevant into a set and return None for both metrics when it is empty.
  3. Remove repeated ids from ranked, keeping first positions, and only then keep the first k as top.
  4. Compute recall as the number of relevant ids in top divided by the number of relevant ids.
  5. For mrr, find the first relevant id in top and return 1 over its one-based position, or 0.0 if there is none.

What it costs

Time
O(n)
Space
O(n)

Removing duplicates reads the whole ranked list of n ids once, and set lookups make each relevance test constant time.

Where it is used

  • The MS MARCO passage ranking leaderboard scores systems by MRR@10.
  • The BEIR benchmark scores retrieval models with the rank-aware nDCG@10 and also reports recall at a cutoff.
  • The pytrec_eval and ranx Python libraries compute recall and reciprocal rank from ranked runs and relevance judgements.

Watch out for

  • Cutting to k before removing duplicates, so a repeated id takes a slot and pushes a relevant result out of the top k.
  • Dividing recall by k or by the number of results returned: that is precision. Recall divides by the number of relevant documents.
  • Returning 0 when no relevant ids are known, which mixes unlabelled queries into an average as if they were failures.