# Cosine Similarity

Canonical: https://aimastermindlab.com/glossary/cosine-similarity

---
Cosine similarity measures the angle between two vectors in high-dimensional space. In AI, it quantifies how semantically similar two embeddings are — a score of 1.0 means identical direction (highly similar), 0 means orthogonal (unrelated), -1 means opposite meaning.

## What is Cosine Similarity?

Cosine similarity measures the cosine of the angle between two vectors. In embedding space, it's the standard way to measure how semantically similar two pieces of text are, and is the core distance metric used by most vector databases and RAG systems.

## Formula

```
cosine_similarity(A, B) = (A · B) / (|A| × |B|)
```

Where A · B is the dot product and |A|, |B| are the magnitudes. The result ranges from -1 to 1.

## Interpreting Scores

| Score | Interpretation |
|---|---|
| 1.0 | Vectors are identical in direction (maximally similar) |
| 0.9+ | Very high similarity |
| 0.7–0.9 | Good semantic match |
| 0.5–0.7 | Some relation |
| < 0.5 | Likely not related |
| ~0 | Orthogonal — unrelated |
| -1 | Opposite meaning (rare in text embeddings) |

## Cosine vs Dot Product vs Euclidean Distance

For normalized vectors (most embedding models output unit vectors), cosine similarity equals the dot product. Euclidean distance is also valid but less commonly used for high-dimensional embeddings due to the curse of dimensionality.

## Practical Implementation

```python
import numpy as np

def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
```

Most vector databases handle similarity computation internally — you provide the query embedding and they return the top-k most similar stored vectors.

## Example

Embedding 'I love machine learning' and 'I adore artificial intelligence' produces vectors with cosine similarity ~0.92 — very close. 'I love machine learning' and 'What should I have for dinner?' produces cosine similarity ~0.15 — very different.
---

Source: https://aimastermindlab.com/glossary/cosine-similarity — AI Mastermind Lab (https://aimastermindlab.com)
