Mathamatics used in AI vectordatabase --
Core Native Metrics
- Inner Product (Dot Product)
- Euclidean Distance (L₂)
Specialized Native Metrics
- Hamming Distance (Used only for binary embeddings)
- Jaccard Distance (Used only for sparse token vectors)
- Manhattan Distance (L₁) (Supported by select engines like FAISS or Milvus for specific use cases)
In vector databases, Euclidean Distance and the Pythagorean theorem are fundamental tools used to measure the geometric space between vectors. However, to handle high-dimensional data at scale, vector databases rely on several other core mathematical concepts.
Here are the main mathematical concepts used in vector databases, grouped by how they work:
1. Alternative Distance & Similarity Metrics
While Euclidean distance (\(L_{2}\) norm) measures the straight-line distance, other metrics are often more effective depending on how data is normalized.
- Cosine Similarity: Measures the cosine of the angle between two vectors, ignoring their magnitude. It evaluates directional alignment rather than distance. It is calculated using the dot product divided by the product of the vectors' magnitudes:
\(\text{Cosine\ Similarity}=\frac{A\cdot B}{\|{}A\|{}\|{}B\|{}}\) - Dot Product (Inner Product): Multiplies corresponding components of two vectors and sums the results (\(A \cdot B = \sum A_i B_i\)). If vectors are normalized (magnitude of 1), the dot product is exactly equal to Cosine Similarity. It is computationally very fast.
- Manhattan Distance (\(L_{1}\) Norm): Measures the distance navigating only along axis-parallel segments (like walking the grid of city streets). It calculates the sum of the absolute differences of their coordinates.
2. Dimensionality Reduction
High-dimensional vectors (often containing 768 to 1536+ dimensions from AI models) suffer from the "curse of dimensionality," making searches slow. Databases use math to shrink them.
- Principal Component Analysis (PCA): A linear transformation that rotates the vector space to project the data onto fewer dimensions while preserving as much variance (information) as possible.
- Random Projection: Based on the Johnson-Lindenstrauss lemma, this concept states that a set of points in a high-dimensional space can be embedded into a much lower-dimensional space in a way that nearly preserves the distances between the points.
3. Quantization (Compression)
To fit billions of vectors into RAM, databases compress floating-point numbers into smaller integers.
- Vector Quantization (VQ) & Product Quantization (PQ): PQ breaks a high-dimensional space into a Cartesian product of lower-dimensional subspaces. It clusters these subspaces using K-Means Clustering and represents vectors as a collection of compact centroid IDs (codewords) rather than raw floating-point coordinates.
4. Graph Theory & Proximity Graphs
To avoid searching every single vector linearly, databases index data using geometric graph structures.
- Voronoi Diagrams / Delaunay Triangulation: Divides the vector space into regions (cells) around central points (centroids). When a query arrives, the database determines which Voronoi cell it falls into, restricting the search to that immediate neighborhood (Inverted File Indexing).
- Hierarchical Navigable Small World (HNSW): A graph-based structure inspired by Skip Lists and Small-World Networks. It creates multi-layered geometric graphs where top layers have long-range links for fast routing across the vector space, and bottom layers have short-range links for precise local exploration.
What Vector Databases Actually Use
Most vector databases (like Pinecone, Milvus, Qdrant, FAISS, and pgvector) natively support only three core metrics:
- Cosine Similarity (or Cosine Distance)
- Inner Product (Dot Product)
- Euclidean Distance (L₂)
Used outside the database (in the AI pipeline)
These are not used by the vector database itself to scan vectors. They are applied after retrieval or handled by different software layers:
- Lexical & Search Algorithms (BM25, TF-IDF): Handled by traditional search engines (like Elasticsearch) or separate python libraries, then combined with vector results.
- Model-Based Systems (Cross-Encoders, LLMs): Implemented in your application code to re-rank the top 10–25 results after the vector database gives them to you.
- Fusion Systems (RRF): A sorting algorithm used in your application code to merge keyword results with vector results.
Comments
Post a Comment