1.  [](/)
2.  [Glossary](/glossary)
3.  NDCG (Normalized Discounted Cumulative Gain)

[Back to Glossary](/glossary)

# What is NDCG (Normalized Discounted Cumulative Gain)

## NDCG (Normalized Discounted Cumulative Gain) - Ranking quality with graded relevance, scored from 0 to 1

NDCG (normalized discounted cumulative gain) scores a ranked list by adding up the relevance of each result, discounting results further down the list, and dividing by the score of the ideal ordering. A perfect ranking scores 1. NDCG@10 looks only at the top 10 results. It is the standard metric when results carry graded relevance, such as exact, close and related.

### How It Works

Give each result a relevance grade, for example 3 for an exact match, 2 for a close one, 1 for related and 0 for irrelevant. DCG@K adds each grade divided by log2(position + 1), so rank 1 counts in full, rank 2 counts 0.63 and rank 4 counts 0.43. IDCG@K is the same sum for the best possible order of those results, and NDCG@K = DCG@K / IDCG@K.

### Worked Example

*   A query returns four results with grades 3, 2, 0, 1
*   DCG@4 = 3/1 + 2/1.585 + 0/2 + 1/2.322 = 4.693
*   The ideal order is 3, 2, 1, 0, so IDCG@4 = 3 + 1.262 + 0.5 + 0 = 4.762
*   NDCG@4 = 4.693 / 4.762 = 0.985
*   Putting the best result third instead (grades 0, 2, 3, 1) gives DCG@4 = 3.193 and NDCG@4 = 0.670

### Technical Details

*   Formula used here: DCG@K = Σ rel\_i / log2(i + 1) for i = 1 to K. Some libraries use the exponential gain (2^rel\_i - 1) instead, which rewards top grades more, so check which one a score was computed with before comparing
*   Range 0 to 1, averaged over all queries in the evaluation set
*   With binary relevance (every grade 0 or 1), NDCG still rewards putting relevant results higher
*   A query with no relevant results has IDCG = 0 and is usually scored 0 or left out

### Measuring NDCG in Mixpeek

Mixpeek retriever evaluations compute NDCG at each cutoff you set in \`k\_values\` (for example 1, 5, 10 and 20). Add \`relevance\_scores\` to a ground truth query, such as \`{"doc\_a1": 5, "doc\_a2": 3}\`, and NDCG uses those grades; without them it uses binary relevance. The example evaluation in the docs reports NDCG@5 of 0.81. See [retriever evaluations](https://mixpeek.com/docs/retrieval/evaluations).

### Common Pitfalls

*   Comparing NDCG across different K values or different gain formulas
*   Grades that annotators apply inconsistently, which add noise larger than the change being measured
*   Judging only the results the current system returned, so a new system's unjudged results count as irrelevant
*   Reporting a single average that hides queries where the ranking got worse

[One-click templatesStart from a templateEach template deploys a working search app on your own files.Browse templates](/templates)

[Put it to work: search your own files](https://studio.mixpeek.com/namespaces/create?mode=managed&utm_source=homepage&utm_medium=glossary_onramp&utm_campaign=mvs_onramp)

### Related Terms

[ACID](/glossary/acid)[API](/glossary/api)[Blob Storage](/glossary/blob-storage)[CLIP](/glossary/clip)[Embedding](/glossary/embedding)