NDCG (Normalized Discounted Cumulative Gain)

Search Engineering intermediate Building with AI 11 min read

Overview

Normalized Discounted Cumulative Gain (NDCG) is the gold-standard metric for evaluating ranked retrieval systems. Unlike binary metrics such as MRR that only care about the first relevant result, NDCG evaluates the entire ranking order and supports graded relevance—meaning a “highly relevant” document is worth more than a “somewhat relevant” one. This makes NDCG the most comprehensive single metric for judging ranking quality.

NDCG was introduced by Kalervo Järvelin and Jaana Kekäläinen in 2002 and has since become the standard evaluation metric used by search engines, recommendation systems, and AI retrieval pipelines. The key insight behind NDCG is twofold: (1) documents appearing higher in the ranking should contribute more to the score (the “discount” part), and (2) the metric should be normalized against a theoretically perfect ranking so scores are comparable across queries of varying difficulty.

For AI engineers building RAG systems or search infrastructure, NDCG provides the most complete picture of retrieval quality. A system might have good MRR (first result is often relevant) but poor NDCG (the overall ranking order is bad), which means users who scroll past the first result are getting a degraded experience.

How It Works

Step 1: Cumulative Gain (CG)

The simplest building block is Cumulative Gain, which is just the sum of all relevance scores in the result list without any regard for position:

Cumulative Gain

CG_p = Σ rel_i for positions i = 1 to p, where rel_i is the relevance score of the document at position i.

CG has a critical flaw: it doesn’t penalize bad ordering. A list with the most relevant document at position 10 has the same CG as one with it at position 1. This is where discounting comes in.

Step 2: Discounted Cumulative Gain (DCG)

DCG applies a logarithmic discount that reduces the contribution of results appearing lower in the ranking. Documents at the top of the list contribute their full relevance, while documents further down are “discounted” progressively:

DCG Formula

DCG_p = rel_1 + Σ (rel_i / log<sub>2</sub>(i + 1)) for positions i = 2 to p. An alternative formulation used in industry: DCG_p = Σ ((2^rel_i - 1) / log<sub>2</sub>(i + 1)) for i = 1 to p.

The logarithmic discount means that moving a relevant document from position 10 to position 1 gives a much larger score boost than moving it from position 100 to position 91. This matches human behavior—users pay far more attention to top results.

Step 3: Ideal DCG (IDCG)

IDCG is the maximum possible DCG achievable for a given query. You compute it by sorting all the relevance scores in descending order (best documents first) and calculating the DCG of this “ideal” ranking:

Ideal DCG

IDCG_p = DCG computed on the ideal ranking where all documents are sorted by relevance in descending order. This represents the theoretical best the system could achieve.

Step 4: Normalized DCG (NDCG)

Finally, NDCG normalizes DCG by dividing by IDCG, producing a score between 0 and 1:

NDCG Formula

NDCG_p = DCG_p / IDCG_p. A score of 1.0 means the ranking is perfect. A score of 0.0 means no relevant documents were returned.

Worked Example

Suppose a search query returns 5 documents with these relevance scores (on a 0-3 scale):

Returned ranking:    [3, 2, 3, 0, 1]  (relevance scores at positions 1-5)
Ideal ranking:       [3, 3, 2, 1, 0]  (sorted descending)

DCG Calculation (standard formula):
  Position 1: 3 / log2(2) = 3 / 1.000 = 3.000
  Position 2: 2 / log2(3) = 2 / 1.585 = 1.262
  Position 3: 3 / log2(4) = 3 / 2.000 = 1.500
  Position 4: 0 / log2(5) = 0 / 2.322 = 0.000
  Position 5: 1 / log2(6) = 1 / 2.585 = 0.387
  DCG = 3.000 + 1.262 + 1.500 + 0.000 + 0.387 = 6.149

IDCG Calculation (ideal ordering [3, 3, 2, 1, 0]):
  Position 1: 3 / log2(2) = 3.000
  Position 2: 3 / log2(3) = 1.893
  Position 3: 2 / log2(4) = 1.000
  Position 4: 1 / log2(5) = 0.431
  Position 5: 0 / log2(6) = 0.000
  IDCG = 3.000 + 1.893 + 1.000 + 0.431 + 0.000 = 6.324

NDCG = DCG / IDCG = 6.149 / 6.324 = 0.972

An NDCG of 0.972 indicates that the ranking is very close to ideal—the only imperfection is that the relevance-3 document at position 3 should have been at position 2 (swapped with the relevance-2 document).

NDCG@K — Cutoff Variant

Like MRR, NDCG is almost always evaluated with a cutoff K. NDCG@5 evaluates only the top 5 results, NDCG@10 the top 10. This is important because in production, users rarely look past the first page of results. Common choices are NDCG@5 for search and NDCG@10 or NDCG@20 for recommendation systems.

Python Implementation

import math

def dcg(relevances: list[int], k: int) -> float:
    """Compute DCG@K."""
    score = 0.0
    for i, rel in enumerate(relevances[:k]):
        score += rel / math.log2(i + 2)  # i+2 because log2(1)=0
    return score

def ndcg(relevances: list[int], k: int) -> float:
    """Compute NDCG@K."""
    actual_dcg = dcg(relevances, k)
    ideal_relevances = sorted(relevances, reverse=True)
    ideal_dcg = dcg(ideal_relevances, k)
    if ideal_dcg == 0:
        return 0.0
    return actual_dcg / ideal_dcg

# Example
scores = [3, 2, 3, 0, 1]
print(f"NDCG@5: {ndcg(scores, k=5):.3f}")  # Output: NDCG@5: 0.972

# scikit-learn also provides this:
# from sklearn.metrics import ndcg_score
# ndcg_score([ideal], [actual], k=5)

Graded vs Binary Relevance

One of NDCG’s major strengths is support for graded relevance. Instead of just “relevant” or “not relevant” (binary), you can assign grades like 0 (irrelevant), 1 (marginally relevant), 2 (relevant), and 3 (highly relevant). This reflects reality more accurately—a document that perfectly answers the question is much more valuable than one that tangentially mentions the topic. Many evaluation datasets use 4-point or 5-point relevance scales for NDCG evaluation.

Practical Applications

Search Engine Evaluation

NDCG is the primary offline metric for major search engines. Google, Bing, and internal enterprise search systems all use NDCG to measure ranking quality. Teams maintain labeled datasets with relevance judgments from human annotators (typically on a 5-point scale from “Perfect” to “Bad”) and compute NDCG@10 as the key quality indicator. A change that improves NDCG@10 by even 0.5% on a benchmark set is considered significant.

RAG Pipeline Quality

In RAG systems, NDCG evaluates whether the retriever returns the most useful chunks in the optimal order. Since the generator model processes chunks sequentially (or gives more weight to earlier chunks due to positional attention patterns), the order matters. A RAG system with high NDCG means the most relevant information is packed into the earliest positions where the generator will use it most effectively.

Recommendation Systems

Netflix, Spotify, Amazon, and other recommendation platforms use NDCG to evaluate how well their algorithms rank recommended items. For example, if a user would love Movie A, like Movie B, and be indifferent to Movie C, an ideal ranking puts them in that order. NDCG captures how close the actual ranking is to this ideal.

When to Use NDCG vs Other Metrics

Key Takeaways

Further Reading