Sub topic 2
Rank-Aware Metrics and Answer Faithfulness
Assume you understand chunking, embeddings, vector search, prompting, Python, and basic precision and recall. This chapter focuses on metrics that care about result order and on evaluating the generated answer.
MRR, MAP, and nDCG
Set-based metrics only ask whether relevant documents appear. Rank-aware metrics also ask where they appear.
- MRR (mean reciprocal rank) gives each question a score of
1 / rankfor its first relevant result. A relevant result at rank 1 scores 1.0; one at rank 4 scores 0.25. Average the scores across questions. - MAP (mean average precision) rewards finding several relevant documents early. For each question, calculate precision whenever a relevant result appears, then average those precision values. Average the question scores.
- nDCG (normalized discounted cumulative gain) supports graded relevance, such as scores from 0 to 3. It gives higher weight to highly relevant documents near the top and divides by the ideal ranking.
import math
def reciprocal_rank(results, relevant):
for rank, doc_id in enumerate(results, 1):
if doc_id in relevant:
return 1 / rank
return 0.0
def ndcg_at_k(results, grades, k):
gains = [grades.get(doc_id, 0) for doc_id in results[:k]]
ideal = sorted(grades.values(), reverse=True)[:k]
def dcg(values):
return sum(gain / math.log2(rank + 2)
for rank, gain in enumerate(values))
best = dcg(ideal)
return dcg(gains) / best if best else 0.0
results = ["d7", "d2", "d9", "d1"]
grades = {"d2": 3, "d1": 2, "d5": 1}
print(reciprocal_rank(results, grades)) # 0.5
print(round(ndcg_at_k(results, grades, 4), 3))Faithfulness and Answer Relevance
Faithfulness asks whether every important answer claim is supported by the retrieved context. It measures grounding, not general truth. An answer can be factually correct but unfaithful if the context does not support it.
Answer relevance asks whether the answer addresses the user's question. A response can be faithful yet irrelevant if it accurately summarizes unrelated retrieved passages.
Claim-Level Scoring and LLM Judges
Split an answer into atomic claims, then verify each claim against the context. The score is the fraction supported. Report “undefined” when an answer contains no claims rather than treating it as perfect.
An LLM judge can classify support or relevance when manual labels are expensive. However, judges may prefer fluent answers, share biases with the generator, or accept plausible but unsupported statements. Give the judge only the required inputs, define a strict rubric, and validate its decisions against human labels. Track agreement and inspect disagreements before using it for regression checks.
Exercise
A response contains four claims, and the context supports three. What is its claim-level faithfulness score?
Show answer
0.75, or 75%. This does not show whether the response answered the question; that requires an answer-relevance score.