Expected Reciprocal Rank (ERR)
Background
Expected Reciprocal Rank (ERR) is the cascade-model graded-relevance ranking metric. Unlike NDCG, ERR models user satisfaction probabilistically: each result has a chance of satisfying the user (depending on its grade), and once satisfied, lower positions contribute nothing.
Problem statement
Implement err(grades, g_max=4):
where is the relevance grade at rank . Walk the list top-down, accumulating as you go.
Input
grades— sequence ofintgraded relevance values in , in rank order (rank 1 first).g_max—int >= 1, the maximum grade (default , matching the standard TREC relevance scale).
Output
Returns a Python float in .
Examples
Example 1 — all zero grades
Input: grades=[0, 0, 0, 0]
Output: 0.0
Example 2 — single max-grade item
Input: grades=[4], g_max=4
Output: 15/16 ≈ 0.9375
Explanation: . Contribution = .
Example 3 — hand-computed two-position case
Input: grades=[2, 4], g_max=4
Output: 291/512 ≈ 0.5684
Explanation: . Contribution . Survive = . . Contribution . Sum .
Constraints
- Returns a plain Python
float. g_max >= 1.
Notes
- vs NDCG. NDCG is independent across positions; ERR is dependent — high relevance at the top lowers the influence of everything below.
- Maximum possible value. Even with everywhere, ERR < 1 because at .
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference example — all zero grades
- •Sample — single max-grade item
- •Reference — hand-computed two-position case