Back
intermediate

RAG (Retrieval-Augmented Generation)

What does a similarity score really compare?

Calculate dot product, cosine similarity, and Euclidean distance, then test normalization and score interpretation.

Lesson 19 of 44About 28 min with practice

Two documents receive scores of 0.8 from different search systems. Are they equally relevant? Not necessarily. A score has meaning only in relation to the embedding model, metric, preprocessing, and task.

Before you begin: Know vectors and elementary arithmetic. Python is optional for the worked calculation.

Compare three measurements

The dot product multiplies corresponding vector coordinates and sums them. It is affected by direction and magnitude. Cosine similarity divides the dot product by the product of vector lengths, emphasizing direction. Euclidean distance measures straight-line distance; smaller values mean closer vectors.

For a query [1, 0], document A [2, 0] has dot product two and cosine one. Document B [1, 1] has dot product one and cosine about 0.707. Their Euclidean distances from the query are both one. The metrics can tie or rank candidates differently because they measure different properties.

Work with normalized vectors

A unit-normalized vector has length one. For two unit vectors, squared Euclidean distance equals 2 - 2 × dot_product. Their dot product also equals cosine similarity. Under those conditions, maximizing cosine and minimizing Euclidean distance produce equivalent rankings.

The conditions matter. If only documents are normalized or the model expects magnitude to carry information, the relationship changes. Follow the embedding model's guidance and the database metric configuration.

This complete Python 3 program checks the unit-vector relationship with a small example.

python
# Runnable: Python 3, standard library.
import math

def normalize(v):
    length = math.sqrt(sum(x*x for x in v))
    if length == 0:
        raise ValueError('Cannot normalize a zero vector')
    return [x/length for x in v]

a = normalize([1, 0])
b = normalize([1, 1])
dot = sum(x*y for x, y in zip(a, b))
distance_squared = sum((x-y)**2 for x, y in zip(a, b))
assert math.isclose(distance_squared, 2-2*dot)
print(round(dot, 3), round(distance_squared, 3))

Keep the embedding spaces compatible

Queries and documents must be encoded using a compatible model and procedure. Some retrieval models use different prefixes or encoding paths for queries and documents. Equal vector dimensions do not prove compatibility between unrelated checkpoints.

Record model revision, dimensions, normalization, tokenizer behavior, and preprocessing with the index. If any of these changes, evaluate whether re-embedding is required. A silent mixture can return plausible-looking numbers with poor retrieval.

Do not read probability into every score

A cosine score of 0.8 is not an 80% probability that the document answers the question. Similarity scores are not automatically calibrated confidence values. Thresholds must be evaluated for the chosen model, corpus, and query distribution.

Likewise, a reranker's output may be a raw logit or another score depending on the model and activation. Do not assume every relevance model returns a probability between zero and one. Check its documented output and calibration before displaying a confidence percentage.

Inspect the semantic boundary

“The class is cancelled” and “The class is not cancelled” are closely related in topic. A retriever may rank both highly. The answerer must interpret the negation and identify the authoritative notice. Similarity helps locate candidate evidence; it does not resolve truth or document authority by itself.

Include exact IDs, paraphrases, negation, and irrelevant but topically similar passages in your evaluation. A clean demonstration with obvious synonyms is not enough to choose a production threshold.

Choose a threshold responsibly

You see that every relevant example in a small development set scores above 0.75. Can you treat 0.75 as a universal threshold for all future collections?

Keep the threshold tied to evidence

No. Test it on held-out queries, other document types, and missing-answer cases. Measure the cost of returning irrelevant evidence versus withholding useful evidence. A threshold can be a practical operating choice, but it is not a property shared by every embedding model or corpus.

Next, we will assemble a small retrieval pipeline with source identities before introducing a more complex database or embedding service.

Sources

Sentence-BERT studies sentence representations for similarity. The Faiss metric notes explain distance choices and the role of normalization.

Continue to the next lesson.

Practice for this lesson

Choose a threshold you can defend

Understand what a similarity score is and pick a cut-off from data.

About 10 min55 points3 checks and one written task
Loading your lesson progress...