In Retrieval-Augmented Generation (RAG) and vector databases (Pinecone, Qdrant, pgvector), text is converted into high-dimensional numerical vectors (e.g., 1,536 dimensions for OpenAI’s text-embedding-3-small).
To compare how semantically related two paragraphs are, we calculate their Cosine Similarity. But why cosine similarity instead of Euclidean distance? What actually happens in 1,536 dimensions?
1. The Geometry of Angle vs. Distance
Suppose we have two vectors, $\mathbf{A}$ and $\mathbf{B}$, in $n$-dimensional Euclidean space:
$$\mathbf{A} = [a_1, a_2, \dots, a_n], \quad \mathbf{B} = [b_1, b_2, \dots, b_n]$$
Euclidean Distance ($L_2$ Norm)
The Euclidean distance measures the physical straight-line distance between the tips of the two vectors:
$$d(\mathbf{A}, \mathbf{B}) = \sqrt{\sum_{i=1}^n (a_i - b_i)^2} = |\mathbf{A} - \mathbf{B}|$$
The Problem: Euclidean distance is heavily distorted by text length. If document $A$ is a 20-word summary of a topic and document $B$ is a 2,000-word comprehensive paper on the exact same topic, document $B$’s word frequencies will be 100x higher. Its vector magnitude $|\mathbf{B}|$ will be far larger, making the Euclidean distance massive even though the meaning is identical.
Cosine Similarity
Cosine similarity ignores magnitude completely and measures only the directional angle $\theta$ between the two vectors:
$$\cos(\theta) = \frac{\mathbf{A} \cdot \mathbf{B}}{|\mathbf{A}| |\mathbf{B}|} = \frac{\sum_{i=1}^n a_i b_i}{\sqrt{\sum_{i=1}^n a_i^2} \sqrt{\sum_{i=1}^n b_i^2}}$$
- When $\theta = 0^\circ$, $\cos(\theta) = 1.0$ (identical direction).
- When $\theta = 90^\circ$, $\cos(\theta) = 0.0$ (orthogonal, entirely unrelated).
- When $\theta = 180^\circ$, $\cos(\theta) = -1.0$ (diametrically opposed).
2. The Vector Normalization Shortcut
Notice that the denominator of cosine similarity is just the product of vector magnitudes: $|\mathbf{A}| |\mathbf{B}|$.
If we normalize our vectors to unit length ($|\mathbf{A}| = 1$) during ingestion:
$$\hat{\mathbf{A}} = \frac{\mathbf{A}}{|\mathbf{A}|}$$
Then the denominator becomes $1 \times 1 = 1$, and cosine similarity simplifies to a pure dot product:
$$\cos(\theta) = \hat{\mathbf{A}} \cdot \hat{\mathbf{B}} = \sum_{i=1}^n \hat{a}_i \hat{b}_i$$
This mathematical property is the reason vector search engines like pgvector and FAISS can search millions of vectors in single-digit milliseconds: computing a single dot product is SIMD and AVX-512 hardware-accelerated.
3. The Curse of Dimensionality in 1536D Space
In high dimensions, human spatial intuition breaks down. Two fascinating mathematical phenomena emerge:
- Orthogonality Dominance: If you pick two random vectors in 1,536 dimensions, the probability that their cosine similarity is close to $0.0$ approaches $1$. Almost all random directions are nearly perpendicular.
- Concentration of Measure: The volume of an $n$-dimensional sphere is overwhelmingly concentrated in a thin outer shell near the surface.
Because high-dimensional space is so sparsely populated, modern embedding models can encode tens of thousands of nuanced semantic concepts (tone, intent, domain-specific terminology) without running out of degrees of freedom.