Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: fix MRR and MAP calculations #7841

Merged
merged 7 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions haystack/components/evaluators/document_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,26 @@ def run(

for ground_truth, retrieved in zip(ground_truth_documents, retrieved_documents):
score = 0.0
average_precision = 0.0
relevant_documents = 0
ground_truth_content = []

for ground_document in ground_truth:
if ground_document.content is None:
continue
ground_truth_content.append(ground_document.content)
anakin87 marked this conversation as resolved.
Show resolved Hide resolved

average_precision = 0.0
relevant_documents = 0

for rank, retrieved_document in enumerate(retrieved):
if retrieved_document.content is None:
continue
for rank, retrieved_document in enumerate(retrieved):
if retrieved_document.content is None:
continue

if ground_document.content in retrieved_document.content:
relevant_documents += 1
average_precision += relevant_documents / (rank + 1)
if relevant_documents > 0:
score = average_precision / relevant_documents
if retrieved_document.content in ground_truth_content:
relevant_documents += 1
average_precision += relevant_documents / (rank + 1)
if relevant_documents > 0:
score = average_precision / relevant_documents
individual_scores.append(score)

score = sum(individual_scores) / len(retrieved_documents)
score = sum(individual_scores) / len(ground_truth_documents)

return {"score": score, "individual_scores": individual_scores}
20 changes: 13 additions & 7 deletions haystack/components/evaluators/document_mrr.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,23 @@ def run(

for ground_truth, retrieved in zip(ground_truth_documents, retrieved_documents):
score = 0.0
flag = False
ground_truth_content = []

for ground_document in ground_truth:
if ground_document.content is None:
continue
ground_truth_content.append(ground_document.content)
anakin87 marked this conversation as resolved.
Show resolved Hide resolved

for rank, retrieved_document in enumerate(retrieved):
if retrieved_document.content is None:
continue

if ground_document.content in retrieved_document.content:
score = 1 / (rank + 1)
break
for rank, retrieved_document in enumerate(retrieved):
if flag:
break
if retrieved_document.content is None:
continue
if retrieved_document.content in ground_truth_content:
score = 1 / (rank + 1)
flag = True
break
anakin87 marked this conversation as resolved.
Show resolved Hide resolved
individual_scores.append(score)

score = sum(individual_scores) / len(retrieved_documents)
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/fix-issue-7758-d35b687ca226a707.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Fixed the calculation for MRR and MAP scores.
12 changes: 11 additions & 1 deletion test/components/evaluators/test_document_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ def test_run_with_complex_data():
],
],
)
assert result == {"individual_scores": [1.0, 0.8333333333333333, 1.0, 0.5, 0.0, 1.0], "score": 0.7222222222222222}
assert result == {
"individual_scores": [
1.0,
pytest.approx(0.8333333333333333),
1.0,
pytest.approx(0.5833333333333333),
0.0,
pytest.approx(0.8055555555555555),
],
"score": pytest.approx(0.7037037037037037),
}


def test_run_with_different_lengths():
Expand Down