Angular/Similarity-Based Distances
1. Cosine Similarity
Cosine Similarity is a similarity measure (not a distance metric) that calculates the cosine of the angle between two vectors in
It is widely used in artificial intelligence and natural language processing to compare documents or search intent.
👉 Key Insight: Two vectors pointing in the same direction have high similarity, regardless of their length
Formula
Given two vectors
where
- Dot Product
: Sum of element-wise products
- Magnitude (
and ): Length (Euclidean norm) of each vector
- Cosine of the Angle (
): Normalized dot product
Value Range and Interpretation:
- +1: Vectors point in exactly the same direction (maximum similarity, 0° angle)
- 0: Vectors are orthogonal/perpendicular (no similarity, 90° angle)
- -1: Vectors point in opposite directions (maximum dissimilarity, 180° angle)
Example in 2D Space:
Consider two 2D vectors:
-
Calculate dot product:
-
Compute magnitudes:
-
Calculate cosine similarity:
The cosine similarity is approximately 0.9985, indicating the vectors are highly similar in direction (angle ≈ 3.15°).
Cosine Distance:
To convert cosine similarity to a distance metric:
- Range:
(often normalized to for non-negative vectors) - Lower values = more similar
When to Use:
- Text data and NLP (document similarity, word embeddings)
- High-dimensional sparse data
- When magnitude doesn't matter (only direction/orientation)
- Recommendation systems (user-item preferences)
Advantages:
- ✅ Scale invariant: Not affected by vector magnitude
- ✅ Efficient for sparse data (only non-zero elements matter)
- ✅ Works well in high dimensions: Common in NLP (TF-IDF, word2vec)
- ✅ Bounded range: [-1, 1] makes it easy to interpret
- ✅ Robust to document length: Perfect for text analysis
Disadvantages:
- ❌ Ignores magnitude: [1, 2] and [10, 20] have similarity = 1
- ❌ Not a true metric: Doesn't satisfy triangle inequality
- ❌ Sensitive to zero vectors: Undefined for zero-length vectors
- ❌ Less intuitive than Euclidean for geometric problems
Applications:
- NLP & Text Mining: Document similarity, plagiarism detection, information retrieval
- Recommendation Systems: User-item similarity (collaborative filtering)
- Image Recognition: Comparing feature vectors
- Bioinformatics: Gene expression analysis
- Anomaly Detection: Identifying outliers in high-dimensional spaces