Cosine Similarity.

A metric that measures similarity between two vectors by calculating the cosine of the angle between them, widely used in vector search and retrieval systems.

Cosine similarity is a fundamental metric in machine learning and information retrieval that quantifies how similar two vectors are by measuring the cosine of the angle between them in high-dimensional space. Unlike Euclidean distance, cosine similarity focuses on the orientation of vectors rather than their magnitude, making it particularly valuable for comparing text embeddings, user preferences, and other high-dimensional data where the relative direction matters more than absolute values. This property makes it essential for semantic search, recommendation systems, and document clustering applications.

The calculation involves taking the dot product of two vectors and dividing by the product of their magnitudes, yielding values between -1 and 1, where 1 indicates identical direction, 0 indicates orthogonality, and -1 indicates opposite directions. In practice, most embedding models produce vectors in positive space, so cosine similarity typically ranges from 0 to 1. The metric is computationally efficient and scale-invariant, meaning that vectors pointing in the same direction will have high similarity regardless of their length, which is crucial when comparing embeddings of different text lengths or when vector magnitudes vary due to preprocessing differences.

Cosine similarity's main advantage is its robustness to vector magnitude differences, making it ideal for comparing embeddings where semantic meaning is more important than absolute feature values. However, it can be less effective when magnitude carries important information, such as in certain recommendation scenarios where user engagement intensity matters. A common misconception is that cosine similarity always outperforms other distance metrics; the choice depends on the specific use case and whether directional similarity or absolute distance is more meaningful for the application.