Understanding the cosine similarity and distance calculator — concept and principle
Cosine similarity measures how closely two vectors point in the same direction, regardless of their length. It is the standard way to compare text embeddings, document word-count vectors and user preference profiles in search engines and recommendation systems. Alongside it, this calculator reports the dot product, the Euclidean (straight-line) distance and the Manhattan (city-block) distance, which do depend on magnitude.
How it is calculated
For vectors A = (a₁, …, aₙ) and B = (b₁, …, bₙ) of the same length:
- Dot product A·B = Σ aᵢbᵢ
- Cosine similarity = A·B ÷ (‖A‖ × ‖B‖), where ‖A‖ = √(Σ aᵢ²) is the vector's length.
- Euclidean distance = √(Σ (aᵢ − bᵢ)²)
- Manhattan distance = Σ |aᵢ − bᵢ|
The principle
The dot product equals ‖A‖ × ‖B‖ × cos θ, where θ is the angle between the vectors. Dividing out both lengths leaves only cos θ: 1 when the vectors point the same way, 0 when they are perpendicular, −1 when they point in opposite directions. This is why cosine similarity suits text, where a long document and a short one about the same topic should look alike. Euclidean distance instead measures how far apart the two points are, so scaling one vector changes it. For vectors normalised to length 1, the two are directly linked: squared Euclidean distance = 2 − 2 × cosine.
Worked example
- A = (1, 2, 3, 4), B = (2, 3, 4, 5).
- Dot product = 2 + 6 + 12 + 20 = 40.
- ‖A‖ = √30 = 5.4772 and ‖B‖ = √54 = 7.3485, so ‖A‖ × ‖B‖ = √1,620 = 40.249.
- Cosine = 40 ÷ 40.249 = 0.99381, an angle of about 6.4°.
- Each difference is 1, so Euclidean = √4 = 2 and Manhattan = 1 + 1 + 1 + 1 = 4.
How to read the result
A cosine of 0.994 says the vectors point almost the same way even though they are 2 units apart. For non-negative data such as word counts, cosine ranges from 0 to 1. With embeddings, what counts as "similar" depends on the model, so compare scores within one model rather than using a fixed cut-off. Use Euclidean or Manhattan distance when absolute size matters, for example physical measurements.
Common mistakes
- Comparing vectors from two different embedding models.
- Treating cosine similarity as a percentage or a probability.
- Using cosine on a zero vector, which has no direction and gives an undefined result.
Frequently asked questions
What is cosine distance?
It is usually defined as 1 − cosine similarity. It is useful for ranking but does not satisfy the triangle inequality.
Is cosine similarity the same as Pearson correlation?
Pearson correlation is the cosine similarity of the two vectors after subtracting each one's mean.
Which is faster for search, cosine or dot product?
If all vectors are pre-normalised to length 1, the dot product equals cosine similarity and is the cheaper operation.
Sources: C. D. Manning, P. Raghavan and H. Schütze, Introduction to Information Retrieval, Cambridge University Press, 2008 · G. Strang, Introduction to Linear Algebra, Wellesley-Cambridge Press