Mid · Core path · 8 chapters
Vector arithmetic
Dot product, cosine similarity, normalization
These operations are simple, but a wrong metric or unexamined magnitude effect quietly corrupts retrieval rankings.
What you will know
Compute a dot product by hand on two short vectors
A sign or component-order mistake is enough to change a ranking downstream.
Compare cosine similarity and raw dot product for vectors with the same direction but different lengths, and record the ranking change
Magnitude can dominate the score, allowing longer vectors to outrank shorter ones despite identical direction.
Normalize two vectors in code and verify their dot product matches cosine similarity within a stated tolerance
The equality depends on both operands having unit L2 norm, and floating-point arithmetic makes the tolerance explicit.
Predict whether two sentences will score closer than two others, and be right more often than not
Use an explicit set of sentence pairs and a scoring rule; intuition is especially unreliable for negation and shared vocabulary.
Construct a high-similarity sentence pair and test whether the score implies entailment or correctness
Embedding proximity is neither logical entailment nor factual validation, and the useful threshold depends on the model and dataset.
Chapters
- 1
The dot product
The dot product lines two equal-length vectors up component by component, multiplies each pair and sums the results, so [1, -2, 3] against [4, 0, -1] is 1 rather than 7. This chapter works several by hand until the arithmetic is automatic, connects it to the geometry it encodes — length times length times the cosine of the angle — separates the part of the answer that survives a rescaling from the part that does not, and prices the same loop at a million rows.
- 2
Cosine similarity and vector magnitude
A document embedded as 3v beats one embedded as v by exactly three times under a raw dot product, even though both point in precisely the same direction and are equally relevant. Cosine similarity divides the two lengths back out and scores them identically. This chapter derives that, scores the same three documents both ways to see which orderings move, and then sets out the cases where a magnitude is genuine signal rather than an artifact of length.
- 3
Embedding models
An embedding model maps a piece of text to one fixed-length dense vector, so that texts with similar meaning land in similar directions and a retrieval system can rank stored documents against a query with one arithmetic operation. This chapter calls one, reads what comes back, prices the dimensions and the tokens, names the failures that do not raise an error, and runs the dot product and cosine of the previous two chapters over real sentences.
- 4
Metrics on unit vectors
Once both operands have unit L2 norm, the dot product equals cosine similarity and Euclidean distance produces an identical ordering, which settles several questions at once. This chapter derives the two identities, checks them in code against a stated floating-point tolerance, and then works out what actually decides the metric in a vector database when it cannot change a single result: cost per comparison, what the index supports, and what happens the next time the write path changes.
- 5
Normalisation in the write path
Unit norm is a promise a particular provider makes at the end of a particular model, and plenty of write paths break it: self-hosted encoders, mean-pooled token vectors, averaged or concatenated vectors, truncated Matryoshka prefixes, mean-centring. This chapter measures the L2 norms of what is about to be stored, catalogues the sources that do not deliver unit vectors, compares the four places normalisation can live, and prices an in-place repair against a re-embed.
- 6
Chunk length and magnitude bias
Move from embedding whole documents to embedding passages and the top hit becomes the longest passage nearly every time, because a mean-pooling encoder gives long chunks long vectors and an inner-product index multiplies that length into every score. This chapter works the arithmetic on four real chunks, shows the one-line fix and the regression test that holds it, explains why capping chunk size only compresses the length distribution, and names what normalisation leaves untouched.
- 7
Similarity and entailment
The drug reduced mortality by 30% and the drug did not reduce mortality score 0.87 together, because the negation is one small token inside a long shared string; the Supplier indemnifies the Customer scores 0.95 against its exact role reversal for the same reason. This chapter separates similarity, which is symmetric and has no channel for argument roles or truth, from entailment, which is directional, builds a pair set you predict before you score, and sets out what a high number is actually evidence of.
- 8
Calibrating a similarity threshold
0.80 is a fact about one embedding model measured on one collection of pairs, and it moves the moment either changes, so a global rule about sameness travels badly. This chapter calibrates a cut-off against labelled pairs, reads precision and recall at each operating point, shows why raising the bar to 0.95 discards a third of the real duplicates while keeping the reversed-role clause that prompted the change, and puts the score where it belongs: in front of a second stage that can see what cosine cannot.
External resources
- Measuring similarity from embeddingsdevelopers.google.com
Google's formal treatment of similarity measures over embeddings.
- Vector similarity measures and scoring in Elasticsearchelastic.co
How each metric is actually scored and configured in a real vector index.
- Vector Similarity Explainedpinecone.io
Intuition for dot product vs cosine vs Euclidean and when each fits.
- Vector Similarity Metrics: Cosine SimilarityZilliz
Shows why cosine equals the dot product of normalized vectors.
- 5. Cosine Similarity vs. Euclidean Distance vs. Dot Product: Choosing the Right Metric for AI SearchSH AI Academy
Picking the right metric for an AI search stack.