NDCG: Measuring Ranking Quality With Position Discounting
How NDCG Works Step by Step
Start with Cumulative Gain (CG): sum of relevance scores in your ranked list. If top-5 items have relevance [3, 2, 3, 0, 1], CG = 9. Problem: CG ignores position. [3, 2, 3, 0, 1] and [0, 1, 2, 3, 3] both score 9, but the first ranking is obviously better.
Discounted Cumulative Gain (DCG) fixes this by dividing each relevance by log(position+1). Position 1 divides by log(2)=1, position 2 by log(3)=1.58, position 5 by log(6)=2.58. Higher positions get less discount, so relevant items there contribute more. For [3, 2, 3, 0, 1]: DCG = 3/1 + 2/1.58 + 3/2 + 0/2.32 + 1/2.58 = 3 + 1.26 + 1.5 + 0 + 0.39 = 6.15.
Normalized DCG divides by the ideal DCG (what you would get with perfect ranking). If perfect order is [3, 3, 2, 1, 0], ideal DCG = 3/1 + 3/1.58 + 2/2 + 1/2.32 + 0 = 6.33. NDCG = 6.15/6.33 = 0.97. A perfect ranking scores 1.0.
When to Use NDCG
NDCG shines when you have graded relevance labels and care about the full top-K ranking, not just the first result. Search results, product recommendations, and content feeds typically use NDCG@10 or NDCG@20. It is less useful for navigational queries where users want exactly one result (use MRR instead) or when all relevant items are equally relevant (binary relevance makes the grading pointless).