WRITE · TRACE · UNDERSTAND

Evaluate graded ranking with a normalized ideal order

Return NDCG at positive integer k.

Read lesson: Evaluate graded ranking with a normalized ideal order →

Your experiment

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.

Python runs in your browser. Your draft stays on this device.

What to look for

Inspect judgments, deduplication and the undefined all-zero case. A perfect NDCG on a tiny fixture says nothing about an unmeasured query population.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Evaluate graded ranking with a normalized ideal order works

The idea

NDCG scores a ranked list when relevance comes in grades instead of yes or no. Each result contributes a gain of 2**grade - 1, so a highly relevant item is worth far more than a marginal one. The gain is divided by log2(rank + 1), so the same item is worth less the further down it appears. The sum over the top k is the discounted cumulative gain.

DCG alone cannot be compared across queries, because some queries have more relevant items than others. Dividing by the DCG of the ideal order, all grades sorted from highest to lowest, gives a value between 0 and 1. ndcg removes duplicate IDs before applying k, treats unjudged IDs as grade 0, and returns None when the ideal gain is zero.

How it works

  1. Check that k is a positive integer and that every grade is an integer from 0 to 10.
  2. Remove repeated IDs from ranking, keeping the first occurrence, then keep the first k.
  3. Turn each kept ID into a gain of 2 ** grade - 1, using grade 0 for IDs missing from grades.
  4. Build ideal by sorting the gains of all graded items in descending order and keeping the first k.
  5. Sum each list with the discount gain / log2(i + 2), where i counts from zero.
  6. Return dcg / best, or None when best is zero.

What it costs

Time
O(n + g log g)
Space
O(n + g)

Deduplicating the n ranked IDs is linear, and sorting the g graded items to build the ideal order dominates.

Where it is used

  • LightGBM's lambdarank objective optimises NDCG, and its default label_gain is the same 2**grade - 1.
  • The Yahoo Learning to Rank Challenge scored submissions with NDCG and expected reciprocal rank.
  • scikit-learn's ndcg_score computes NDCG with the grade itself as the gain, so its values differ from this exponential form.

Watch out for

  • Applying the cutoff before removing duplicates, which lets a repeated ID take a position that belongs to the next distinct result.
  • Building the ideal order from the returned items only; it must use every graded item, or missing relevant results go unpunished.
  • Using log2(rank) with rank starting at one, which divides the first result by zero. The discount is log2(rank + 1).