call(back)
Algorithms & Data StructuresmediumSign in to star problems

NDCG@k

ndcgAtK(rankedRels, k, ideal) scores a ranking. rankedRels holds graded relevances in the order your system returned the items. Using 0-based positions,

DCG@k = sum over the first k positions i of  rel_i / log2(i + 2)
NDCG@k = DCG@k of the ranking / DCG@k of the ideal ordering

The ideal ordering sorts relevances from highest to lowest. When ideal (the relevances of every relevant item) is given, the ideal DCG comes from it; when it is null, from the ranked list itself. Return 0 when the ideal DCG is 0. Results are compared after rounding to six decimals.

ndcgAtK([3, 2, 3, 0, 1, 2], 6, null)  ->  0.960808
ndcgAtK([1, 0, 0], 3, [3, 2, 1])      ->  0.210002   // the best items were never returned

Hints

Hint 1

Write dcg(rels) once: the sum of rel / log2(i + 2) over 0-based positions. Both the numerator and the denominator use it.

Hint 2

The ideal list is the sorted-descending relevances — of ideal when given, so items the system never returned still count against it.

Asked at