TL;DR
Cosine similarity compares two vectors by the angle between them. It is the dot product of the two vectors divided by the product of their lengths, so the answer lies between and . A value of means the vectors point the same way, means they are at right angles, and means they point in opposite directions.
Because the lengths are divided out, cosine similarity ignores how big the vectors are and keeps only their direction. That is what you want when a long document and a short document about the same topic should count as alike.
Where you'll see it: in ranking search results and retrieval-augmented systems, where embeddings are compared with a cosine score, and in recommender systems that match a user vector to item vectors.
Intuition
Two arrows, one angle
Picture two arrows starting from the same point. If they point the same way, they agree completely. If they point at right angles, they have nothing in common. If they point in opposite directions, they disagree completely. The angle between the arrows captures this, and the cosine of that angle runs from (angle ) down through (angle ) to (angle ).
The plot shows for angles from to radians.
- cos θ
Why divide out the length
The plain dot product also reflects direction, but it is scaled by the lengths too. Double one vector and its dot product with anything else doubles. In many settings that scaling is noise. Suppose you describe two documents by counts of words. A long article about cats uses every cat word many times, and a short note about cats uses them fewer times. The counts are different, but the topic is the same. Dividing by the lengths removes that effect, and the score depends only on the proportions of the words.
This is why cosine similarity is the usual choice for comparing text, embeddings and other high-dimensional data where the overall size of a vector is less informative than its pattern.
flowchart LR A[Vector a] --> D[Dot product a · b] B[Vector b] --> D A --> N1[Length of a] B --> N2[Length of b] D --> C[Divide] N1 --> M[Multiply the lengths] N2 --> M M --> C C --> S[Cosine similarity]
The math
Definition
For two nonzero vectors and in , the cosine similarity is
where is the dot product and is the Euclidean length of .
Here is the sum of the products of matching entries. The length is the square root of the sum of the squared entries. Dividing by both lengths is the step that removes the effect of size.
The angle between the vectors is defined by , so . The inverse cosine returns an angle between and . Cosine similarity itself never requires computing that angle.
Range
The Cauchy-Schwarz inequality (the dot product of two vectors can never be larger in size than the product of their lengths) says . Dividing both sides by the positive number gives . Equality holds exactly when one vector is a scalar multiple of the other: for a positive multiple, for a negative one.
Unit vectors
If both vectors have length 1, the denominator is 1 and the cosine equals the dot product. Many systems normalise every vector to length 1 first, so that a plain dot product gives the same ranking as cosine similarity. For a unit vector , we have .
Distance
Cosine distance is . It is for vectors pointing the same way and for opposite ones. It is not a true metric, because the triangle inequality can fail, but it is a convenient way to turn a similarity into something that behaves like a distance.
Worked examples
Let and .
- Dot product: .
- Length of : .
- Length of : .
- Divide: .
- Angle: .
The vectors are 45 degrees apart. The arrow points along the horizontal axis and is the diagonal, so 45 degrees is what the picture shows.
Take three toy embeddings with three dimensions each: , and .
- Lengths: ; ; .
- Cat and dog: dot product . Cosine .
- Cat and car: dot product . Cosine .
- Dog and car: dot product . Cosine .
Interpretation: cat and dog point almost the same way (score near 1), so the model treats them as close. Car is only loosely related to either. The scores are the ranking a search engine would use to put "dog" ahead of "car" when the query is "cat".
Let and , then compare with .
- , , , so .
- , , so .
Doubling doubled the dot product and the length, and the score stayed at 0.96. The angle is .
Tradeoffs and failure modes
Cosine similarity is a good default when the direction of a vector carries the meaning and its size does not. It stops being the right tool when size carries information.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Vector length encodes confidence or popularity | Cosine throws that signal away | Dot product, or a distance that keeps magnitude |
| Vectors are already unit length | Nothing is wrong; cosine equals the dot product | Use the dot product, which is cheaper |
| A vector is all zeros | The denominator is zero and the score is undefined | Guard the zero case, or add a small epsilon |
| Entries have very different scales | One large entry dominates the direction | Standardise each dimension first |
| Need to ignore a common offset in the data | Cosine keeps the offset, so every score looks high | Subtract the mean first, which gives Pearson correlation |
| Need a true distance for indexing structures | Cosine distance fails the triangle inequality | Euclidean distance on unit vectors, which is monotone with cosine |
Two practical points. First, if you normalise your vectors once when you store them, you can use a fast dot product at query time and still get cosine rankings. Second, scores from cosine are comparable across queries in a way that raw dot products are not, which makes thresholds easier to set.
History
The idea comes from geometry and linear algebra, where the cosine of the angle between two vectors had long been used. Its use as a measure of document similarity grew out of information retrieval in the 1960s and 1970s. Researchers building systems such as Gerard Salton's SMART project represented documents as vectors of term weights and ranked them by the angle to the query. Cosine was attractive because it did not favour long documents, which contain more words and would otherwise look more relevant. The vector space model became the standard framework for text search, and cosine remained its usual score. When word and sentence embeddings became common in the 2010s, cosine similarity was carried over unchanged, because learned embeddings are also compared by direction.
Check yourself
QuestionWhat is the cosine similarity of two vectors that are perpendicular to each other?Show answer
The dot product of perpendicular vectors is zero. Dividing zero by the product of the lengths still gives zero, so the cosine is . The angle is , and .
QuestionVectors and . Compute their cosine similarity.Show answer
The dot product is . The lengths are and . So . The angle is .
QuestionWhy is cosine similarity often preferred to the plain dot product when comparing documents of very different lengths?Show answer
The dot product grows with the size of both vectors, so a long document would score higher against almost any query just because it has more words. Dividing by the lengths removes size and leaves only the direction, which reflects the proportions of the words. Two documents on the same topic then score as similar regardless of their length.
QuestionYou store unit-length embeddings for a million items. Why can a search system use a dot product at query time and still rank by cosine similarity?Show answer
If every vector has length 1, the denominator in the cosine formula is . So the cosine equals the dot product for every pair, and the ranking by dot product is exactly the ranking by cosine. Normalising once at storage time means the cheaper dot product does the work at query time, and the query vector is normalised once per query.