Sub topic 4
Precision@k, recall@k, and hit rate
This chapter assumes you can read Python, know precision and recall from classification, and have an evaluation set where each question lists the documents that answer it (see the previous chapter). Here you score a retriever by looking at the set of top-k results and ignoring their order.
What the metrics measure
A retriever returns a ranked list of document ids. Take the first k of them, the top-k set. Three questions can be asked about that set:
- Precision@k: what share of the k results are relevant?
- Recall@k: what share of all relevant documents appear in the top k?
- Hit rate@k: does the top k contain at least one relevant document? This is 1 or 0 per question.
Precision@k tells you how much noise you pass to the generator. Recall@k tells you whether the evidence the generator needs is present at all. Hit rate is a lenient version of recall, and it suits questions that need only one good passage.
Computing them
def precision_at_k(retrieved_ids, relevant_ids, k):
top_k = retrieved_ids[:k]
if not top_k:
return 0.0
hits = sum(1 for d in top_k if d in relevant_ids)
return hits / len(top_k)
def recall_at_k(retrieved_ids, relevant_ids, k):
if not relevant_ids:
return None # undefined: the question has no labelled relevant documents
hits = sum(1 for d in retrieved_ids[:k] if d in relevant_ids)
return hits / len(relevant_ids)
def hit_at_k(retrieved_ids, relevant_ids, k):
return 1.0 if any(d in relevant_ids for d in retrieved_ids[:k]) else 0.0
retrieved = ["d7", "d2", "d9", "d1"]
relevant = {"d2", "d1", "d5"}
print(precision_at_k(retrieved, relevant, 3)) # 1/3
print(recall_at_k(retrieved, relevant, 3)) # 1/3
print(hit_at_k(retrieved, relevant, 3)) # 1.0In this example the top 3 are d7, d2 and d9. Only d2 is relevant, so precision@3 is 1/3. Three documents are relevant (d2, d1, d5) and one was found, so recall@3 is 1/3. The hit rate is 1.0 because at least one relevant document appears.
Note that recall_at_k returns None when a question has no labelled relevant documents. Recall is undefined there. Drop those questions from the recall average instead of scoring them 0 or 1.
Averaging over the evaluation set
Compute each metric per question, then take the mean over questions. The mean of hit@k is the hit rate. Do not pool all documents into one big count, because questions with many relevant documents would dominate.
How k changes the numbers
The two metrics move in opposite directions as k grows.
| Change | Precision@k | Recall@k | Hit rate@k |
|---|---|---|---|
| Increase k | Usually falls | Never falls | Never falls |
| Decrease k | Usually rises | Never rises | Never rises |
Pick k to match what your pipeline sends to the generator. If the prompt holds 5 passages, report metrics at k=5. Metrics at other k values do not describe what the generator saw.
Common errors
- Precision has a ceiling. If a question has only 2 relevant documents and k is 5, precision@5 cannot exceed 0.4, even for a perfect retriever. Compare precision across systems at the same k, and do not read a low value as a failure on its own.
- Incomplete labels punish good retrievers. If a retrieved document is relevant but unlabelled, it counts as a miss. Review a sample of "misses" by hand.
- Order is ignored. A relevant document at rank 1 and one at rank k score the same. The next chapter covers rank-aware metrics.
Exercise
A question has relevant documents {a, b, c, d}. The retriever returns [a, x, b, y, z] for k=5. What are precision@5, recall@5, and hit@5?
Show answer
Two of the five results (a and b) are relevant, so precision@5 is 2/5 = 0.4. Two of the four relevant documents were found, so recall@5 is 2/4 = 0.5. The top 5 contains at least one relevant document, so hit@5 is 1.