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
+10
View File
@@ -0,0 +1,10 @@
# Golden dataset: what is in it
passages : 40
questions : 30
exact : 11
multi : 6
paraphrase : 13
judgements : 42 (31 grade 2, 11 grade 1)
passages judged : 31 of 40
never relevant : [p06, p21, p23, p29, p31, p33, p34, p35, p38]
@@ -0,0 +1,11 @@
# One ranked list, every metric, worked by hand
retrieved (best first) : [p03, p01, p07, p02, p09]
relevant with grade : p01=2, p02=1, p08=1
hit@1 = 0.0000
hit@2 = 1.0000
recall@5 = 0.6667 (2 of 3 relevant found)
precision@5 = 0.4000 (2 relevant of 5 returned)
MRR = 0.5000 (first relevant at rank 2)
nDCG@5 = 0.5625 (DCG 2.3235 / ideal 4.1309)
+10
View File
@@ -0,0 +1,10 @@
# Edge cases that change the number
1 duplicates [p01 p01 p01 p02 p03], p01 relevant, precision@5 = 0.20 (naive count would give 0.60)
2 precision cap perfect list, one relevant passage, precision@5 = 0.20
3 short list 2 results, precision@5 = 0.20
4 no relevant recall@5 = NaN, nDCG@5 = NaN
5 MRR blind to the second hit: 0.50 vs 0.50
6 order, same recall@2 = 1.00:
support first nDCG@2 = 0.7967, MRR = 1.00
answer first nDCG@2 = 1.0000, MRR = 1.00
@@ -0,0 +1,20 @@
# Four retrievers, 30 questions, 40 passages
retriever hit@1 hit@5 recall@5 recall@10 MRR nDCG@10
bm25 0.733 0.933 0.867 0.867 0.814 0.798
ngram 0.833 0.933 0.883 0.883 0.875 0.818
hybrid 0.833 0.900 0.850 0.883 0.871 0.818
bm25-lettersonly 0.667 0.867 0.800 0.833 0.751 0.741
MRR by question category
retriever exact paraphrase multi
bm25 1.000 0.609 0.917
ngram 1.000 0.827 0.750
hybrid 1.000 0.769 0.854
bm25-lettersonly 0.828 0.609 0.917
Questions with no relevant passage in the top 5
bm25 [q22, q24]
ngram [q24, q25]
hybrid [q22, q24, q25]
bm25-lettersonly [q02, q03, q22, q24]
+12
View File
@@ -0,0 +1,12 @@
# Paired bootstrap, 10,000 resamples of the 30 questions, seed 42
candidate vs bm25 metric mean diff 95% interval interval excludes 0?
ngram mrr +0.061 [-0.067, +0.183] no
ngram recall@5 +0.017 [-0.067, +0.117] no
hybrid mrr +0.057 [-0.038, +0.147] no
hybrid recall@5 -0.017 [-0.050, +0.000] no
bm25-lettersonly mrr -0.063 [-0.159, +0.000] no
bm25-lettersonly recall@5 -0.067 [-0.167, +0.000] no
one question can move MRR by at most 1/30 = 0.033 (a miss becoming a rank-1 hit)
one question moving from rank 2 to rank 1 moves it by 0.5/30 = 0.017
+14
View File
@@ -0,0 +1,14 @@
# bm25: every question with no relevant passage in the top 5
q22 [paraphrase] "someone pushed a password to git"
returned : [p01, p02, p03, p04, p05]
wanted : {p40=2}
p40 says : Secrets live in the vault, never in source control. A secret found in a repository must be rotated immediately, even if the commit is removed.
first returned says : To reset a forgotten password, open the sign-in page, choose Forgot password and enter your work email. A reset link is emailed and stays valid for 30 minutes.
q24 [paraphrase] "money back after cancelling a yearly subscription"
returned : [p11, p07, p03, p18, p37]
wanted : {p22=2}
p22 says : Refunds for annual plans are prorated by unused full months. Monthly plans are not refunded.
first returned says : To roll back a release, redeploy the previous tagged version from the pipeline. Database migrations are not reverted automatically.
+7
View File
@@ -0,0 +1,7 @@
# Gate run for retriever 'bm25'
tolerance : 0.04
overall MRR : 0.814
overall nDCG@10: 0.798
misses@5 : [q22, q24]
violations : 0
@@ -0,0 +1,16 @@
# The gate against bm25-lettersonly (a tokenizer that drops digits)
FAIL overall hit@1 = 0.667, below baseline 0.733 - 0.04 = 0.693
FAIL exact hit@1 = 0.818, below baseline 1.000 - 0.04 = 0.960
FAIL overall hit@5 = 0.867, below baseline 0.933 - 0.04 = 0.893
FAIL exact hit@5 = 0.818, below baseline 1.000 - 0.04 = 0.960
FAIL overall recall@5 = 0.800, below baseline 0.867 - 0.04 = 0.827
FAIL exact recall@5 = 0.818, below baseline 1.000 - 0.04 = 0.960
FAIL exact recall@10 = 0.909, below baseline 1.000 - 0.04 = 0.960
FAIL overall mrr = 0.751, below baseline 0.814 - 0.04 = 0.774
FAIL exact mrr = 0.828, below baseline 1.000 - 0.04 = 0.960
FAIL overall ndcg@10 = 0.741, below baseline 0.798 - 0.04 = 0.758
FAIL exact ndcg@10 = 0.846, below baseline 1.000 - 0.04 = 0.960
FAIL new miss@5: q02 "E-4012" (relevant not in top 5, got [p01, p02, p03, p04, p05])
FAIL new miss@5: q03 "E-5003" (relevant not in top 5, got [p01, p02, p03, p04, p05])
13 violations
+47
View File
@@ -0,0 +1,47 @@
# mvn test -Dtest=GoldenSetGateTest -Drageval.retriever=bm25-lettersonly
# Gate run for retriever 'bm25-lettersonly'
tolerance : 0.04
overall MRR : 0.751
overall nDCG@10: 0.741
misses@5 : [q02, q03, q22, q24]
violations : 13
org.opentest4j.AssertionFailedError:
retrieval regression against baseline.properties:
- overall hit@1 = 0.667, below baseline 0.733 - 0.04 = 0.693
- exact hit@1 = 0.818, below baseline 1.000 - 0.04 = 0.960
- overall hit@5 = 0.867, below baseline 0.933 - 0.04 = 0.893
- exact hit@5 = 0.818, below baseline 1.000 - 0.04 = 0.960
- overall recall@5 = 0.800, below baseline 0.867 - 0.04 = 0.827
- exact recall@5 = 0.818, below baseline 1.000 - 0.04 = 0.960
- exact recall@10 = 0.909, below baseline 1.000 - 0.04 = 0.960
- overall mrr = 0.751, below baseline 0.814 - 0.04 = 0.774
- exact mrr = 0.828, below baseline 1.000 - 0.04 = 0.960
- overall ndcg@10 = 0.741, below baseline 0.798 - 0.04 = 0.758
- exact ndcg@10 = 0.846, below baseline 1.000 - 0.04 = 0.960
- new miss@5: q02 "E-4012" (relevant not in top 5, got [p01, p02, p03, p04, p05])
- new miss@5: q03 "E-5003" (relevant not in top 5, got [p01, p02, p03, p04, p05]) ==> expected: <true> but was: <false>
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] GoldenSetGateTest.retrievalHasNotRegressed:31 retrieval regression against baseline.properties:
- overall hit@1 = 0.667, below baseline 0.733 - 0.04 = 0.693
- exact hit@1 = 0.818, below baseline 1.000 - 0.04 = 0.960
- overall hit@5 = 0.867, below baseline 0.933 - 0.04 = 0.893
- exact hit@5 = 0.818, below baseline 1.000 - 0.04 = 0.960
- overall recall@5 = 0.800, below baseline 0.867 - 0.04 = 0.827
- exact recall@5 = 0.818, below baseline 1.000 - 0.04 = 0.960
- exact recall@10 = 0.909, below baseline 1.000 - 0.04 = 0.960
- overall mrr = 0.751, below baseline 0.814 - 0.04 = 0.774
- exact mrr = 0.828, below baseline 1.000 - 0.04 = 0.960
- overall ndcg@10 = 0.741, below baseline 0.798 - 0.04 = 0.758
- exact ndcg@10 = 0.846, below baseline 1.000 - 0.04 = 0.960
- new miss@5: q02 "E-4012" (relevant not in top 5, got [p01, p02, p03, p04, p05])
- new miss@5: q03 "E-5003" (relevant not in top 5, got [p01, p02, p03, p04, p05]) ==> expected: <true> but was: <false>
[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] BUILD FAILURE
exit status of mvn: non-zero (see BUILD FAILURE above)