fix: cap DocumentNDCGEvaluator score at 1.0 for duplicate retrievals#11959
fix: cap DocumentNDCGEvaluator score at 1.0 for duplicate retrievals#11959Otis0408 wants to merge 1 commit into
Conversation
|
@Otis0408 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
|
Hey @Otis0408, thank you for contributing. I see you currently have 6 different PRs open in Haystack. I recommend pausing this work for a bit, as it may be difficult for the team to keep up with reviews. Thank you again! |
|
Thanks for saying so directly, and sorry for the pile-up. That's on me, and I should have paced it better. I've stopped opening new PRs here and won't add more until the queue clears. If it would help the team, I'm happy to close the two most recent ones (#11958 and #11959) and re-open them later once you've had a chance to work through the rest. Just say the word and I'll do it. Equally happy to leave them and simply wait. Thanks for the reviews so far. |
|
No need to close them. Thank you again! |
anakin87
left a comment
There was a problem hiding this comment.
Thank you!
Looking into this topic, the IDCG computation also needs a fix for duplicates.
evaluator = DocumentNDCGEvaluator()
result = evaluator.run(
ground_truth_documents=[[Document(content="A", score=1.0), Document(content="A", score=1.0)]],
retrieved_documents=[[Document(content="A")]],
)
print(result["score"]) # 0.6131471927654584, expected 1.0Could you take a look, fix and add a test?
DCG summed a relevant document's relevance for every occurrence in the retrieved list while IDCG counts each ground truth document once, so a duplicated relevant document inflated DCG above IDCG and pushed NDCG above the documented 0.0-1.0 range. Credit each ground truth value at most once when computing DCG.
8a19370 to
4eef147
Compare
|
Good catch, thank you. You're right that I updated |
|
Thank you. I notice that now the two functions treat duplicates differently: |
What
DocumentNDCGEvaluatorcould return an NDCG score greater than1.0, violating the0.0to1.0range promised in the class docstring and theindividual_scoresoutput description.Why
calculate_dcgadds a relevance contribution for every retrieved document whose comparison value is present in the ground truth, without deduplicating.calculate_idcg, however, builds the ideal gain from the unique ground truth set. When the retrieved list contains the same relevant document more than once, its relevance is counted multiple times, so DCG exceeds IDCG and the resulting NDCG (dcg / idcg) climbs above1.0.Fix
Credit each ground truth comparison value at most once when computing DCG, by tracking the values already counted. A relevant document retrieved multiple times now contributes a single relevance term (the earliest, highest-discount position), keeping DCG bounded by IDCG and NDCG within
[0.0, 1.0].Before / after
Tests
Added
test_run_with_duplicate_retrieved_document_does_not_exceed_one, which fails on the current code (score1.63) and passes with the fix. The fulltest_document_ndcg.pysuite (29 tests) passes.