Add rag-eval module: retrieval metrics, golden dataset and a JUnit regression gate

hit@k, recall@k, precision@k, MRR and nDCG@k in plain Java with their edge
cases pinned by tests, a 30-question graded golden dataset over a 40-passage
runbook, a paired bootstrap, and a gate (baseline + tolerance + no new misses)
shown failing on a tokenizer that drops digits. BM25, a hashed-trigram vector
(not a neural embedding) and RRF; no model, no network. Java 25, JUnit 6.1.3.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b7F47n8shUHpexxavn
This commit is contained in:
Ankur
2026-11-25 13:20:18 +05:30
co-authored by Claude Sonnet 5.5
parent 0bb301d361
commit 98ed878675
43 changed files with 1385 additions and 0 deletions
@@ -0,0 +1,82 @@
package com.ankurm.rageval;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Rank-based retrieval metrics. Every method takes the ranked list of passage ids a retriever returned
* (best first) and the ground truth, and is a pure function with no dependencies.
*
* <p>Conventions, all pinned by {@code MetricsTest}:
* <ul>
* <li>Rank is 1-based; {@code k} cuts the list to its first k entries.</li>
* <li>Duplicates are removed first (first occurrence wins), so two chunks of one document cannot
* count twice.</li>
* <li>A list shorter than k is not padded: precision still divides by k.</li>
* <li>With no relevant passages, recall and nDCG are undefined and return {@code NaN}.</li>
* </ul>
*/
public final class Metrics {
private Metrics() {}
/** 1 if any relevant passage is in the top k, else 0. */
public static double hitAtK(List<String> ranked, Set<String> relevant, int k) {
return top(ranked, k).stream().anyMatch(relevant::contains) ? 1 : 0;
}
/** Distinct relevant passages found in the top k, divided by all relevant passages. */
public static double recallAtK(List<String> ranked, Set<String> relevant, int k) {
if (relevant.isEmpty()) return Double.NaN;
long found = top(ranked, k).stream().filter(relevant::contains).count();
return (double) found / relevant.size();
}
/** Relevant passages in the top k, divided by k (not by the list length). */
public static double precisionAtK(List<String> ranked, Set<String> relevant, int k) {
long found = top(ranked, k).stream().filter(relevant::contains).count();
return (double) found / k;
}
/** 1 / rank of the first relevant passage, or 0 if none is in the list. */
public static double reciprocalRank(List<String> ranked, Set<String> relevant) {
List<String> list = distinct(ranked);
for (int i = 0; i < list.size(); i++) {
if (relevant.contains(list.get(i))) return 1.0 / (i + 1);
}
return 0;
}
/**
* nDCG@k with exponential gain: DCG = sum (2^grade - 1) / log2(rank + 1), divided by the DCG of the
* best possible ordering of the graded passages.
*/
public static double ndcgAtK(List<String> ranked, Map<String, Integer> grades, int k) {
if (grades.isEmpty()) return Double.NaN;
double dcg = 0;
List<String> list = top(ranked, k);
for (int i = 0; i < list.size(); i++) {
dcg += gain(grades.getOrDefault(list.get(i), 0), i + 1);
}
List<Integer> ideal = grades.values().stream().sorted(java.util.Comparator.reverseOrder()).toList();
double idcg = 0;
for (int i = 0; i < Math.min(k, ideal.size()); i++) idcg += gain(ideal.get(i), i + 1);
return dcg / idcg;
}
private static double gain(int grade, int rank) {
return (Math.pow(2, grade) - 1) / (Math.log(rank + 1) / Math.log(2));
}
static List<String> distinct(List<String> ranked) {
return new ArrayList<>(new LinkedHashSet<>(ranked));
}
static List<String> top(List<String> ranked, int k) {
List<String> list = distinct(ranked);
return list.subList(0, Math.min(k, list.size()));
}
}