Metrics & Evaluation

Precision@k, Recall@k & NDCG

Evaluating top K recommendation and search results using Precision at K, Recall at K, and Normalized Discounted Cumulative Gain.

🟡 intermediate5 min readmetrics
Ranking Metrics evaluate ordered lists generated by search engines and recommendation systems. Precision at K measures the proportion of relevant items in top K slots, while Recall at K measures the fraction of all relevant items captured. Normalized Discounted Cumulative Gain (NDCG) measures multi-level graded relevance quality, applying logarithmic position discounting to penalize relevant items placed further down the list.

Position Matters in Ranking Systems

In classification, getting a positive prediction correct scores the same regardless of order.

In search and recommendation systems, position order is everything:

Ranking evaluation metrics measure the quality of Top-K recommended items.

┌──────────────────────────┬──────────────────────────┬──────────────────────────┐
│ 1. PRECISION@K           │ 2. RECALL@K              │ 3. NDCG@K                │
├──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Proportion of top-K      │ Fraction of ALL true     │ Graded relevance metric  │
│ slots containing binary  │ positive items retrieved │ with logarithmic position│
│ relevant items.          │ inside top-K slots.      │ discounting. Gold Standard!│
└──────────────────────────┴──────────────────────────┴──────────────────────────┘

1. Precision@K and Recall@K

Suppose a user liked 5 movies globally ($5$ Total Relevant Items). We recommend a top-5 list ($K = 5$) containing 3 liked movies:

$$\text{Precision@5} = \frac{\text{Relevant Items in Top 5}}{5} = \frac{3}{5} = 0.60$$

$$\text{Recall@5} = \frac{\text{Relevant Items in Top 5}}{\text{Total Relevant Items Globally}} = \frac{3}{5} = 0.60$$

2. Normalized Discounted Cumulative Gain (NDCG@K)

NDCG solves the position problem and supports Multi-Level Graded Relevance (for example $0 = \text{Irrelevant}, 1 = \text{Fair}, 2 = \text{Good}, 3 = \text{Perfect}$).

Step 1: Discounted Cumulative Gain (DCG@K)

$$\text{DCG}@K = \sum_{i=1}^K \frac{2^{r_i} - 1}{\log_2(i + 1)}$$

Logarithmic discounting $\frac{1}{\log_2(i+1)}$ heavily penalizes relevant items placed lower down the list.

Step 2: Ideal DCG@K (IDCG@K)

Re-sort the top-K items in perfect descending relevance order and compute DCG@K.

Step 3: Normalization (NDCG@K)

$$\text{NDCG}@K = \frac{\text{DCG}@K}{\text{IDCG}@K}$$

Normalizing against ideal sorting outputs a standardized score between $0.0$ and $1.0$.

Metric Comparison Table

MetricMulti-Level Graded Scores?Position Sensitive?Primary Use Case
Precision@KNo (Binary)NoSimple feed evaluation
Recall@KNo (Binary)NoCandidate retrieval stage
NDCG@KYes (Graded)Yes (Log Discounting)E-Commerce & Search Ranking

Say this out loud

Ranking metrics evaluate ordered recommendation lists. Precision at K measures the proportion of relevant items inside top K slots. Recall at K measures the fraction of total relevant items captured. NDCG at K is the industry gold standard metric for multi level graded relevance, discounting item relevance logarithmically based on rank position and normalizing against ideal sorting.

Followups to expect

  1. What is Mean Reciprocal Rank (MRR)? Evaluates reciprocal rank $1 / \text{rank}$ of the first relevant item, suitable for navigational search where users seek a single target result.
  2. How do you choose K in offline evaluation? Match K to actual user interface screen capacity: $K = 5$ or $K = 10$ for mobile feed cards, $K = 20$ for web search engine pages.

Check yourself

Question 1 of 3

Why do standard classification metrics fail when evaluating search engines and top K recommendation lists?

More in Metrics & Evaluation

See all →
Precision, Recall & F14 minWhy Accuracy Lies4 minROC-AUC vs PR-AUC4 min