Answers · The mathematics underneath
Why does cosine similarity ignore how long the vectors are?
In shortCosine similarity compares direction only because dividing by both vector lengths cancels their size exactly, leaving the angle between them.
Because the dot product contains the product of the two lengths as a factor, and dividing by those same lengths cancels that factor exactly. What remains depends only on direction: multiplying either vector by any positive number leaves the cosine unchanged, while the dot product itself grows or shrinks with that factor.
The cancellation
The dot product has two forms. Algebraically it is the sum of coordinate products. Geometrically it is , where is the angle between the vectors. The cosine formula is the second form rearranged: divide the dot product by the two lengths and the length factor is gone. This is exact, not an approximation. If the stored vector is doubled, its dot product with a query is doubled and its length is doubled, so the ratio has the same value. If it is halved, both numerator and denominator are halved and the ratio again does not move. The only thing that can change the answer is a change of direction.
A concrete pair shows why this is not a cosmetic rescaling. Let the query be , and let the stored vectors be and . The dot product with is the first coordinate in each case, so scores and scores . The lengths are not equal, and that is the whole story.
| vector | dot with q | length | cosine |
|---|---|---|---|
| a=(10,9) | 10 | sqrt181≈13.454 | 10/sqrt181≈0.743 |
| b=(2,0.1) | 2 | sqrt4.01≈2.0025 | 2/sqrt4.01≈0.999 |
Under the dot product wins, by against . Under the cosine wins, because is smaller than . The long vector is less aligned with the query, while is almost exactly on it. The dot product rewarded for being long enough to accumulate a large sum; the cosine asked only how well the directions line up. That is what ignoring length means in practice: the comparison is between directions, not between magnitudes.
What goes wrong
The natural guess is that dividing by the lengths only rescales the score, so the ranking should stay the same. That would be true if every pair were divided by one common number. It is not: the divisor is , which depends on the pair. In the example the divisor for is and for is , so the two dot products are shrunk by different amounts. A single global scaling would leave intact. Dividing by different numbers turns it into . The division is therefore doing more than changing the units of the score: it removes the length advantage that the dot product was giving to .
The cosine always lies between minus one and one. A value near one means the directions coincide, a value near minus one means they oppose, and a value near zero means perpendicular in the geometric sense. In real learned collections the scores often occupy only part of that range, because vectors cluster in a narrow cone rather than spreading over all directions. A high score can be excellent in one collection and ordinary in another, so the absolute number is less reliable than the ranking within the same collection.
When length matters
Dividing out length is a decision, not a cleanup. If length encodes how strong a feature is, how confident a model is, or how much text a document contains, the cosine throws that information away. A faint vector pointing in the right direction and an overwhelming vector pointing the same way receive the same score, so an emphatic match and a tentative one become indistinguishable. That is exactly what is wanted when two documents of different lengths discuss the same subject. It is exactly wrong when the task is to prefer the stronger signal. If every vector is first given unit length, the denominator is one and the dot product and cosine rank pairs identically; without that normalisation the choice between them is a choice about whether length should count.
Worth keeping
- Scaling a vector by a positive number changes its dot product with another vector by the same factor and changes its length by that factor, so the cosine is unchanged.
- Dividing by the two lengths is not a single rescaling of every score: the divisor is different for each pair, which is why it can change the ranking.
- Cosine similarity throws away length on purpose, so it is the wrong choice when length encodes confidence, strength, or document size.
Where this goes next
This question is one step of Vectors, Distance and Similarity, a course that works through its subject from the beginning. The page that teaches this part of it is Comparing Direction Alone, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- What does a negative dot product tell you about two vectors?A negative dot product means the angle between the vectors is greater than a right angle, so they point broadly apart rather than together.
- Why does Manhattan distance give a different answer from straight-line distance?They measure different things: one is the straight gap, the other is the total along a grid, so they disagree whenever the points differ in more than one coordinate.
- Why do distances between random points become almost equal in high dimensions?Each coordinate adds a small independent amount to the squared distance, and many such amounts average out, so typical distances grow while their spread does not keep up.
- What's the difference between a vector as a point and a vector as a direction?A vector as a point is a location, while a vector as a direction is an arrow from the origin whose length and orientation matter; the same numbers can be read either way.
- What is the difference between variance and standard deviation?They measure the same spread: variance is the average squared distance from the mean, standard deviation is its square root, so you report the standard deviation and calculate with the variance.
- How do you make a vague claim precise enough to tell whether it's true?Fix what is being talked about, what is asserted, which cases are covered and the cut-off, then name an observation that would prove it false.