liveBook · Manning

Cosine similarity

As explained in Retrieval Augmented Generation, The Foundational Ideas

Cosine similarity is a metric that measures the angular similarity between two vectors.

Cosine similarity measures how closely two vectors point in the same direction. Normalize each vector to length 1, then take their dot product; this emphasizes direction while discarding magnitude. section 2.2.1

This makes cosine similarity useful for semantic retrieval. Word vectors can connect terms such as “car,” “automobile,” and “vehicle,” allowing a ranking system to see related documents even when query words do not overlap exactly. Unlike BM25, it can expose vocabulary matches that keyword scoring assigns zero relevance. section 2.1.3

Interpret cosine similarity by its geometric value, while treating scores as model-dependent evidence rather than guaranteed meaning:

  • 1.0 indicates identical direction and usually represents near-duplicate text rather than synonymy. section 2.2.1
  • 0.0 indicates right angles, meaning the embedding model detects no relationship. section 2.2.1
  • Vector length no longer affects the score: documents pointing in the same direction score identically even when one vector is longer. section 2.2.1
  • High similarity does not guarantee equivalent meaning; contextual embeddings can place antonyms such as “hot” and “cold” close together. section 2.2.1

Ranking “automobile maintenance”

For the query “automobile maintenance,” semantic scores rank a document about automobiles and cars first at 0.79, followed by vehicle maintenance at 0.75. A computer-crash document still scores 0.55, showing that averaged word vectors improve vocabulary coverage but lose word order, negation, and sentence structure. section 2.1.3

Figure 2.2 What cosine similarity measures and discards. Top left: the query and doc A sit at angle θ, and doc B, twice as long and pointing the same way, scores identically, because normalizing removes magnitude before the dot product. The other three panels draw the values listed above: identical direction scores 1.0, right angles score 0.0, and opposite directions score −1.0, which text embeddings almost never produce.

Use the geometry figure to see what normalization preserves—angular direction—and what it removes—vector magnitude—alongside the characteristic score values. section 2.2.1

How Retrieval Augmented Generation, The Foundational Ideas covers it

How deep it goes
Explains normalization, dot products, geometric values, and retrieval failures, but stops short of detailed mathematical derivation.
Examples
Compares automobile-maintenance retrieval scores and illustrates normalized vectors with differing lengths.
What it assumes
Assumes basic vectors and dot products, while defining normalization and angular similarity directly.
What you can do after
Evaluate semantic-ranking behavior and diagnose errors caused by magnitude removal, averaging, antonyms, or lost sentence structure.
How it connects
Connects vector embeddings with Word2Vec, BM25, semantic search, and approximate nearest neighbor algorithms.

Also explained in

Compare how 25 books explain cosine similarity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Retrieval Augmented Generation, The Foundational Ideas. The section links point to the parts of the book it draws on.