- Introduced by
- Gerard Salton
- Year
- 1975
- Type
- Vector similarity measure
- Family
- Distance metric
What does it do?
Cosine distance is a dissimilarity measure between two non-zero vectors, defined as one minus their cosine similarity: the dot product divided by the product of their Euclidean norms.
It depends only on the angle between the vectors, so it ignores their lengths. For vectors with non-negative entries the value lies between 0 (same direction) and 1 (orthogonal, with no overlapping non-zero components); for general real vectors it lies between 0 and 2.
The intuition behind the method
Think of each vector as an arrow from the origin. Cosine distance measures the angle between two arrows and ignores how long they are, so an arrow and a copy stretched to three times its length are at distance 0. Arrows pointing in nearly the same direction score close to 0, perpendicular arrows score 1, and arrows pointing in opposite directions score 2. This suits comparisons where orientation carries the information, such as term-frequency vectors of documents of different lengths or normalized preference profiles, where the proportions matter more than the totals.
History
The measure is most often described in the context of the vector space model of information retrieval, in which documents are represented as term vectors and compared by the cosine of the angle between them.
When to use it
Choose cosine distance when the question concerns proportions or direction rather than size, for example whether two documents use terms in similar relative frequencies, or whether two normalized preference profiles agree. It is commonly applied to sparse, high-dimensional data because zero entries add nothing to the dot product. Avoid it when absolute magnitude is the signal, such as raw counts or quantities where a larger vector should count as more different. Do not use it when a vector can be all zeros and no handling rule for that case is defined, and avoid it when an algorithm requires a true metric satisfying the triangle inequality. The vectors must have the same dimensions, with components corresponding feature by feature.
Limitations and considerations
- Ignores magnitude entirely: a vector and any positive multiple of it are at distance 0, regardless of scale.
- Requires equal dimensionality, with components aligned feature by feature across the two vectors.
- For nearly parallel vectors the value is very small, because 1 - cos(theta) is approximately theta squared divided by 2 for small angles, so modest angular differences are compressed near 0.
- Not a metric in the strict sense, since 1 - cosine similarity does not satisfy the triangle inequality; this can matter for algorithms that assume a metric.
- With mixed-sign entries the range extends to 2, so the reading of 0 to 1 as a bounded scale no longer holds.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Invariant to positive scaling: multiplying a vector by a positive constant leaves its distance to other vectors unchanged.
- Efficient for sparse vectors, since only components that are non-zero in both vectors contribute to the dot product.
- For non-negative vectors the value lies in [0, 1] and is easy to read: 0 for proportional vectors, 1 when the vectors share no non-zero components.
- Suited to preference profiles or term-frequency vectors, where the proportions across features matter more than their totals.
Applications
- Document similarity and text retrieval: comparing TF-IDF or word embedding vectors across scientific papers
- Recommendation systems: measuring user preference similarity in collaborative filtering
- Decision-making aggregation: comparing normalized preference vectors across multiple criteria
- Image processing and computer vision: comparing histogram or feature vectors for image retrieval
- Clustering and topic modeling: grouping similar documents or data points based on angular distance
How it works
How it works
Check that both vectors are non-zero and have the same number of components. Cosine distance does not require prior scaling, because the norms in the denominator remove magnitude; if the vectors are already of unit length, the norms equal 1 and only the dot product remains.
Compute the dot product a·b and the Euclidean norms ||a|| and ||b||, then form the cosine similarity (a·b) / (||a|| ||b||). The denominator is zero only when one of the vectors is the zero vector.
Subtract the cosine similarity from 1 to obtain the cosine distance, 1 - (a·b) / (||a|| ||b||). Values near 0 indicate nearly the same direction, and the upper bound depends on the signs of the entries (see the FAQ). No universal cut-off separates small from large distances; the threshold should be chosen for the application and checked against the data.
Common pitfalls
- Zero vectors have no direction, and the formula divides by ||a|| ||b||, which is zero whenever either vector is zero. Such pairs need an explicit rule before the computation.
- Reading a distance of 0 as identical vectors: the measure ignores length, so profiles with proportional entries but very different totals score 0 even though they are not equal.
- Applying it where magnitude carries the information, for example raw counts of different sizes, where a long and a short vector with the same proportions are treated as identical.
- Using the value as a metric inside algorithms that assume the triangle inequality, such as some indexing structures. The angle itself is a different quantity, angular distance: arccos of the cosine similarity, with range 0 to π radians. Where a metric is required, angular distance is often substituted, but it should not be confused with the 1 minus cosine similarity defined on this page.
Frequently asked questions
How does cosine distance differ from Euclidean distance?
Euclidean distance measures absolute separation between two points, while cosine distance measures only the angle between two vectors and ignores their lengths. For unit-length vectors the squared Euclidean distance equals twice the cosine distance, so the two give the same ranking on normalized data. Cosine distance is preferable when vector lengths differ for reasons unrelated to the comparison.
Can cosine distance be used with negative vector values?
Yes. The formula accepts any real-valued vectors, including negative entries. With negative entries the value can reach 2, so the reading of 0 to 1 no longer applies, and the result should be interpreted as a measure of angle. Check that negative values have a meaningful interpretation in your data.
Why does cosine distance range from 0 to 1?
It does so only for non-negative vectors. Cosine similarity lies between -1 and +1 for general real vectors, so 1 minus it lies between 0 and 2. When all entries are non-negative, the similarity is never negative and the distance stays within [0, 1]: 0 means the same direction, and 1 means no overlapping non-zero components.
Is normalization required before calculating cosine distance?
No. The norms in the denominator already remove magnitude, so the value is unchanged if either vector is rescaled by a positive constant. Pre-normalizing vectors to unit length gives the same result and reduces each comparison to a single dot product, which can be useful when many vectors are compared.
Sources
Spearman, C. (1904). The proof and measurement of association between two things. American Journal of Psychology, 15(1), 72-101.
Open DOI referenceSalton, G., & McGill, M. J. (1975). Introduction to Modern Information Retrieval. McGraw-Hill.
Open publication reference