tonic_validate icon indicating copy to clipboard operation
tonic_validate copied to clipboard

feat: add NDCG metric for context ranking evaluation

Open hashwnath opened this issue 6 months ago • 0 comments

Summary

Closes #230

  • Adds NDCGMetric class that evaluates the ranking quality of retrieved contexts using Normalized Discounted Cumulative Gain (NDCG)
  • Uses the existing context_relevancy_call LLM helper to determine binary relevance of each context item, then computes NDCG based on position ordering
  • Follows the same class pattern as RetrievalPrecisionMetric (same requirements, prompt, and calculate_metric structure)

How it works

NDCG measures whether relevant contexts appear earlier in the retrieved list:

  • DCG = sum(rel_i / log2(i + 1)) for each position i (1-indexed)
  • IDCG = DCG of the ideal ranking (all relevant items sorted first)
  • NDCG = DCG / IDCG, ranging from 0.0 to 1.0

A score of 1.0 means relevant contexts are optimally ranked. A lower score means relevant items appear later than ideal.

Edge cases handled

  • Empty context list raises ValueError
  • All irrelevant contexts returns 0.0 (IDCG is 0)
  • Single context item works correctly

Test plan

  • [x] Unit test for compute_ndcg static method with known mathematical values (perfect ranking, worst ranking, mixed)
  • [x] Integration tests via ValidateScorer following existing parametrized test patterns (all relevant, all irrelevant, single item cases)
  • [x] test_ndcg_compute_ndcg passes locally

Generated with Claude Code

hashwnath avatar Jun 01 '26 02:06 hashwnath