tonic_validate
tonic_validate copied to clipboard
feat: add NDCG metric for context ranking evaluation
Summary
Closes #230
- Adds
NDCGMetricclass that evaluates the ranking quality of retrieved contexts using Normalized Discounted Cumulative Gain (NDCG) - Uses the existing
context_relevancy_callLLM 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, andcalculate_metricstructure)
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_ndcgstatic method with known mathematical values (perfect ranking, worst ranking, mixed) - [x] Integration tests via
ValidateScorerfollowing existing parametrized test patterns (all relevant, all irrelevant, single item cases) - [x]
test_ndcg_compute_ndcgpasses locally
Generated with Claude Code