Practice in Lab: Evaluate graded ranking with a normalized ideal order →
Evaluate graded ranking with a normalized ideal order
Ranking quality depends on position and on the value of the retrieved item. A binary hit metric cannot distinguish a highly relevant first result from a marginal one. NDCG compares discounted observed gain with the ideal ranking for the same relevance inventory. Duplicating an identifier must not create extra evidence or consume several rank positions. The inventory belongs to a versioned evaluation set; an unjudged document receives zero here, which is a declared convention rather than proof of irrelevance.
import math
grades = [3, 1]
dcg = sum((2**g-1)/math.log2(i+2) for i,g in enumerate(grades))
print(round(dcg, 6))Review the design
Inspect judgments, deduplication and the undefined all-zero case. A perfect NDCG on a tiny fixture says nothing about an unmeasured query population.
Your task
Return NDCG at positive integer k. Deduplicate ranked IDs in first-occurrence order before applying k. Missing IDs have relevance zero. Use gain 2**grade-1 and discount log2(rank+1), with rank starting at one. The ideal order uses all grades sorted descending. Return None when ideal gain is zero. Grades must be integers in 0..10.
The explanation opens here once every check passes.