== Listing 4.1: the synthetic corpus ==
vocabulary 2793 words; 960 entities (720 queried, 240 distractors); 2880 documents
document length: mean 53.9, min 36, max 72 tokens
example query : E0a attr5a t0_27 t0_41
relevant doc  : E0a attr0a val7 t0_28 t0_29 c0 | t0_29 t0_15 t0_23 t0_41 c1 c6 | E0a attr5b val26 t0_55 t0_41 c8 ...
relevant documents containing the query's entity word: 0.504
queries with at least one such document: 0.875
queried entities with training queries (seen): 432 of 720; training queries: 4320

== Listing 4.2: sparse retrieval ==
mean IDF by word type: entity word 7.40, attribute word 0.69, value word 2.11, topic word 4.18, common word 2.87
saturating term-frequency factor tf (k1+1) / (tf + k1) at average length:
  k1=0.5: tf=1: 1.00  tf=2: 1.20  tf=5: 1.36  tf=10: 1.43  tf=50: 1.49   limit 1.5
  k1=1.2: tf=1: 1.00  tf=2: 1.38  tf=5: 1.77  tf=10: 1.96  tf=50: 2.15   limit 2.2
  k1=3.0: tf=1: 1.00  tf=2: 1.60  tf=5: 2.50  tf=10: 3.08  tf=50: 3.77   limit 4.0
dev nDCG@10 over (k1, b):
        b=0.0    b=0.5    b=0.75   b=1.0  
  k1=0.5 0.5846  0.5886  0.5874  0.5879
  k1=1.2 0.5962  0.5976  0.5965  0.5969
  k1=2.0 0.5960  0.5972  0.5975  0.5969
  k1=5.0 0.5887  0.5931  0.5936  0.5936
best on dev: k1=1.2, b=0.5
TF-IDF cosine          recall@10 0.523  MRR 0.885  nDCG@10 0.603  (seen 0.601, unseen 0.606)
BM25 k1=1.2 b=0.75     recall@10 0.523  MRR 0.880  nDCG@10 0.599  (seen 0.597, unseen 0.601)
BM25 k1=1.2 b=0.5      recall@10 0.524  MRR 0.881  nDCG@10 0.599  (seen 0.598, unseen 0.601)
share of queries whose own spam document is ranked first:
  b=0.0: k1=0.5: 0.170  k1=1.2: 0.303  k1=3.0: 0.580  k1=1e6 (linear): 0.750
  b=0.75: k1=0.5: 0.173  k1=1.2: 0.287  k1=3.0: 0.563  k1=1e6 (linear): 0.750

== Listing 4.3: the InfoNCE bound log N - L_N <= I(X;Y) ==
rho=0.9: true mutual information I = 0.830 nats
   N     L_N (se)         log N - L_N   log N
     2   0.355 (0.001)       0.338    0.693
     4   0.834 (0.002)       0.552    1.386
    16   2.021 (0.002)       0.752    2.773
    64   3.350 (0.002)       0.809    4.159
   256   4.720 (0.002)       0.826    5.545
rho=0.999: true mutual information I = 3.108 nats
   N     L_N (se)         log N - L_N   log N
     2   0.039 (0.001)       0.655    0.693
     4   0.112 (0.001)       1.274    1.386
    16   0.482 (0.002)       2.291    2.773
    64   1.319 (0.002)       2.840    4.159
   256   2.515 (0.002)       3.030    5.545

== Listing 4.4: dense retrieval with a two-tower model ==
d=32, V=2793, steps=800, temperature 0.05, Adam lr 0.02
InfoNCE batch loss: first 20 steps 3.346, last 100 steps 0.001 (chance level for a batch of 80 documents: 4.382)
BM25                   recall@10 0.523  MRR 0.880  nDCG@10 0.599  (seen 0.597, unseen 0.601)
LSA (SVD, k=32)        recall@10 0.166  MRR 0.180  nDCG@10 0.122  (seen 0.118, unseen 0.128)
LSA (SVD, k=128)       recall@10 0.273  MRR 0.355  nDCG@10 0.242  (seen 0.232, unseen 0.256)
two-tower d=32         recall@10 0.660  MRR 0.687  nDCG@10 0.653  (seen 1.000, unseen 0.133)

== Listing 4.5: embedding dimension and recall ==
random projection: std of the inner-product error against sqrt((1 + rho^2) / d)
  d=  16: measured 0.2496   predicted 0.2511
  d=  64: measured 0.1236   predicted 0.1255
  d= 256: measured 0.0625   predicted 0.0628
  d=1024: measured 0.0313   predicted 0.0314
exact TF-IDF top-1 document found in the top 10 of the projected ranking:
  d=8: 0.106  d=16: 0.281  d=32: 0.593  d=64: 0.835  d=128: 0.894  d=256: 0.946  d=512: 0.961  d=1024: 0.983
worst-case Johnson-Lindenstrauss dimension 6 ln(4N/delta) / eps^2 for eps=0.1, delta=0.05, N=2880: 7409
recall@10 of the relevant documents by dimension (all / seen / unseen entities):
   d    two-tower              LSA
    2   0.044 / 0.068 / 0.008    0.008
    4   0.512 / 0.847 / 0.009    0.031
    8   0.606 / 0.997 / 0.021    0.041
   16   0.642 / 0.999 / 0.105    0.073
   32   0.660 / 1.000 / 0.149    0.166
   64   0.683 / 1.000 / 0.207    0.292
  128   0.663 / 1.000 / 0.159    0.273
counting bound, smallest d consistent with realising every k-subset of N points as a top-k set:
  N=2880, k=2: d>=1  N=2880, k=10: d>=9  N=2880, k=100: d>=99

== Listing 4.6: hybrid retrieval with reciprocal rank fusion ==
BM25                   recall@10 0.523  MRR 0.880  nDCG@10 0.599  (seen 0.597, unseen 0.601)
dense d=32             recall@10 0.660  MRR 0.687  nDCG@10 0.653  (seen 1.000, unseen 0.133)
RRF(BM25, dense)       recall@10 0.746  MRR 0.822  nDCG@10 0.708  (seen 0.895, unseen 0.428)
queries with no relevant document in the top 10: BM25 0.108, dense 0.257, both 0.044 (independence would give 0.028), RRF 0.082
RRF constant k (nDCG@10):  k=1: 0.797  k=10: 0.790  k=60: 0.708  k=300: 0.651
k chosen on dev queries: 1; test nDCG@10 at that k: 0.797
oracle that picks the better of BM25 and dense per query: nDCG@10 0.840
cases where fusion does not help (nDCG@10):
  BM25 0.599, TF-IDF 0.603, RRF(BM25, TF-IDF) 0.602  (correlated errors)
  BM25 0.599, dense d=2 0.028, RRF(BM25, dense d=2) 0.387  (weak partner)
  entity-free queries: BM25 0.036, dense 0.020, RRF 0.033
  unseen entities only, RRF minus BM25 nDCG@10: -0.173 [-0.200, -0.146]
  seen entities only, RRF minus dense nDCG@10: -0.105 [-0.118, -0.091]

== Listing 4.7: metrics with bootstrap confidence intervals ==
95 percent bootstrap CIs over 720 queries (2000 resamples)
  BM25        recall@10 0.523 [0.501, 0.544]   MRR 0.880 [0.858, 0.903]   nDCG@10 0.599 [0.577, 0.619]
  dense d=32  recall@10 0.660 [0.628, 0.692]   MRR 0.687 [0.655, 0.720]   nDCG@10 0.653 [0.621, 0.685]
  RRF         recall@10 0.746 [0.722, 0.770]   MRR 0.822 [0.798, 0.848]   nDCG@10 0.708 [0.686, 0.732]
  dense d=64  recall@10 0.683 [0.654, 0.714]   MRR 0.706 [0.675, 0.736]   nDCG@10 0.669 [0.639, 0.700]
paired differences in nDCG@10 (A minus B), percentile CI, wins/losses over queries:
  RRF - BM25: +0.110 [+0.087, +0.134]  wins 379, losses 156, ties 185
  RRF - dense d=32: +0.055 [+0.035, +0.076]  wins 212, losses 288, ties 220
  dense d=64 - dense d=32: +0.016 [+0.002, +0.030]  wins 104, losses 77, ties 539
  unpaired CIs for the last pair: [0.637, 0.701] and [0.621, 0.684]; the paired interval is the one that answers the question
width of the 95 percent CI for RRF nDCG@10 by number of queries:
  n= 30: width 0.217
  n=100: width 0.125
  n=300: width 0.073
  n=720: width 0.046

== Listing 4.8: verifying that a cited span supports a claim ==
claim = (entity word, attribute word, value word); sentence-level IDF weights; 1500 pairs per type
span      synonyms  theta  false-reject | false-accept: wrong value  wrong entity  wrong attr  unrelated
sentence  no        0.8   0.734      |      0.003        0.000        0.291       0.000
sentence  no        1.0   0.859      |      0.000        0.000        0.000       0.000
sentence  yes       0.6   0.439      |      0.561        0.000        0.589       0.000
sentence  yes       0.8   0.439      |      0.019        0.000        0.585       0.000
sentence  yes       1.0   0.439      |      0.000        0.000        0.000       0.000
passage   no        0.8   0.511      |      0.066        0.000        0.516       0.000
passage   no        1.0   0.744      |      0.032        0.000        0.264       0.000
passage   yes       0.6   0.000      |      1.000        0.000        1.000       0.000
passage   yes       0.8   0.000      |      0.147        0.000        1.000       0.000
passage   yes       1.0   0.000      |      0.121        0.000        1.000       0.000

== Listing 4.9: grounded answers with citations ==
2160 questions; hybrid retriever, top 10 documents as context
context contains a sentence about the right entity and attribute: 0.920
toy generator (majority of 10 samples): accuracy 0.896, supported by the cited sentence 0.883
  accuracy when the context has the fact 0.960; when it does not 0.168
  correct but cited sentence does not support it: 0.0134 of all answers
extractive answerer: accuracy 0.400 (sentence only), 0.477 (with the document title attached)
verifier on pipeline outputs (sentence span, synonyms, theta=0.8): false-accept 0.000 of 253 unsupported answers, false-reject 0.388 of 1907 supported answers
verifier on pipeline outputs (passage span, synonyms, theta=0.8): false-accept 0.000 of 253 unsupported answers, false-reject 0.000 of 1907 supported answers
answers accepted by the passage verifier that are unsupported: 0.0000 of all answers
zero false accepts in 253 trials: exact 95 percent upper bound on the rate 0.012

== Listing 4.10: detecting unsupported answers ==
unsupported answers: 253 of 2160 (0.117); no evidence in context 173, evidence present but wrong answer 80
AUROC for detecting unsupported answers:
  score                          all    no-evidence cases   evidence-present cases
  predictive entropy           0.964   0.974               0.943
  1 - vote share (10 samples)  0.936   0.936               0.938
  1 - max probability          0.967   0.970               0.961
extractive answerer: error rate 0.523, AUROC of (1 - overlap score) 0.633
unanimous votes: 0.752 of answers, of which unsupported 0.009

== Listing 4.11: abstention with conformal risk control ==
confidence = 1 - entropy / ln(NV); answer when confidence > tau; pool of 2160 questions
    tau  coverage  unsupported-and-answered  selective risk
   0.00   1.000        0.1171              0.117
   0.30   0.953        0.0718              0.075
   0.50   0.873        0.0398              0.046
   0.70   0.820        0.0171              0.021
   0.80   0.797        0.0060              0.008
   0.90   0.768        0.0028              0.004
   0.95   0.727        0.0009              0.001
target alpha = 0.05 for the unconditional rate P(answer and unsupported); 2000 random calibration/test splits
  n_cal  rule     mean risk (se)    share of sets with risk > alpha   mean coverage   mean selective risk
     50  CRC     0.0395 (0.0006)      0.286                          0.874           0.044
     50  naive   0.0578 (0.0007)      0.558                          0.912           0.062
    500  CRC     0.0493 (0.0002)      0.465                          0.899           0.055
    500  naive   0.0517 (0.0003)      0.548                          0.905           0.057

== Listing 4.12: failure modes, context size and a stale index ==
context size k (documents):  has-fact  accuracy  acc | has  acc | no fact  supported
  k=1                         0.767     0.779     0.998      0.060       0.765
  k=2                         0.819     0.830     0.999      0.066       0.818
  k=3                         0.847     0.861     0.998      0.103       0.845
  k=5                         0.879     0.887     0.995      0.103       0.874
  k=10                        0.920     0.896     0.960      0.168       0.883
  k=20                        0.946     0.888     0.930      0.145       0.880
stale index: 108 entities changed their facts after indexing (0.150 of questions)
  retrieval hit@10: changed 0.923, unchanged 0.919
  accuracy against the current facts: changed 0.000, unchanged 0.895
  supported by the cited (stale) text: changed 0.883; verifier accepts 0.883 of the answers for changed entities

== Listing 4.13: failure modes, chunking and injected passages ==
answer-bearing chunk in the top 5 (BM25 over chunks):
  chunk size (sentences)   plain   with document title prepended
  1                        0.408   0.575
  2                        0.593   0.771
  3                        0.746   0.839
  whole document           0.885   0.885
prompt injection (one repeated, keyword-stuffed passage per question, 400 questions):
  injected passage ranked first 0.415, in the top 10 0.960
  accuracy: clean index 0.917, poisoned index 0.068, poisoned index with source filter 0.930
  answers equal to the attacker's value: 0.915; cited sentence supports the (wrong) answer: 0.980
  mean vote share of the attacked answers 0.983; of the clean answers 0.915
  AUROC of predictive entropy for detecting incorrect answers: clean 0.960, poisoned 0.043

