JJoeven

Reference/Math

Vectors, dot product, cosine

The geometry behind RAG: lists of floats, dot product, L2 norm, cosine in [0, 1] for TF vectors.

A vector here is a Python list of floats, length = vocab size.

Formulas

NameFormulaCode
DotΣ a_i b_isum(x*y for x,y in zip(a,b))
L2 normsqrt(Σ a_i²)math.sqrt(sum(x*x for x in a))
Cosinedot / (‖a‖‖b‖)0 if either norm is 0

Cosine of bag-of-counts vectors is in [0, 1] (non-negative counts). Centered embeddings can be negative; still clamp or just rank.

Why cosine not Euclidean

Long chunks have large L2. Cosine ignores length. Two copies of the same sentence have cosine 1.

Retrieval

  1. Vectorize query with the same vocab as chunks
  2. Score all N chunks (N tiny in class; ANN later)
  3. Sort by score desc, tie-break on id
  4. Return top-k and the scores

Zero query

All tokens OOV → zero vector → cosine 0 → refuse. Do not special-case "return chunk 0".

python
def cosine(a, b):
    na = math.sqrt(sum(x*x for x in a))
    nb = math.sqrt(sum(x*x for x in b))
    if na == 0 or nb == 0:
        return 0.0
    return sum(x*y for x,y in zip(a,b)) / (na * nb)
KnobFailure
tau too lowanswers equine insurance
tau too highrefuses real refunds
fixplot in-scope vs out-of-scope best scores; put tau in the gap