A distance metric is a function that quantifies how dissimilar two data points are, satisfying non-negativity, identity, symmetry and the triangle inequality. In machine learning it defines the geometry of a feature space and thereby governs nearest-neighbour search, clustering and similarity-based retrieval. Choosing or learning an appropriate metric is often as important as the model itself for tasks driven by proximity.
Semantic Classification
Content
Compositional Relationships (Components)
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:hasPart ai:EuclideanDistance))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:hasPart ai:ManhattanDistance))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:hasPart ai:MahalanobisDistance))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:hasPart ai:CosineSimilarity))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:hasPart ai:HammingDistance))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:hasPart ai:MinkowskiDistance))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:hasPart ai:JaccardDistance))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:hasPart ai:EditDistance))
Dependency Relationships
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:requires ai:FeatureRepresentation))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:requires ai:VectorEmbedding))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:requires ai:LinearAlgebra))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:dependsOn ai:DataNormalisation))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:dependsOn ai:CovarianceMatrix))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:dependsOn ai:FeatureSpace))
Capability Relationships
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:enables ai:NearestNeighbourSearch))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:enables ai:Clustering))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:enables ai:AnomalyDetection))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:enables ai:Classification))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:enables ai:MetricLearning))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:enables ai:SemanticSearch))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:enables ai:InformationRetrieval))
Implementation Relationships
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:implements ai:MetricSpace))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:implements ai:SimilarityFunction))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:implements ai:ProximitySearch))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:implements ai:KNNClassifier))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:implements ai:VectorIndexing))
Reduction Relationships
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:reducesTo ai:EuclideanDistance))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:reducesTo ai:InnerProduct))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:reducesTo ai:AngularDistance))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:reducesTo ai:HammingDistance))
Contrastive Relationships
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:contrastsWith ai:KernelFunction))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:contrastsWith ai:GraphDistance))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:contrastsWith ai:EarthMoverDistance))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:contrastsWith ai:KLDivergence))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:contrastsWith ai:MutualInformation))
SubClassOf(ai:DistanceMetric
ObjectSomeValuesFrom(ai:contrastsWith ai:SimilarityFunction))
About
The concept of a distance metric is one of the oldest and most mathematically fundamental notions in analysis and geometry, with roots in the abstract topology formalised by Maurice Fréchet (1906) and Felix Hausdorff (1914). The four metric axioms — non-negativity, identity of indiscernibles, symmetry, and the triangle inequality — define a metric space: a set endowed with a notion of distance that behaves in an intuitively correct manner. Every metric space is simultaneously a topological space, meaning that a distance function induces an open-set topology from which all continuity and convergence properties follow. This deep mathematical pedigree distinguishes proper distance metrics from ad hoc similarity functions that may be useful but lack the theoretical guarantees that metric structure provides — particularly the triangle inequality, which is exploited by indexing data structures to prune search spaces and is the mathematical foundation of efficient approximate nearest-neighbour algorithms.
In the context of machine learning, distance metrics entered as proxies for the notion of “similar inputs should have similar outputs” — the geometric expression of the smoothness assumption that underlies most supervised and unsupervised learning. The k-nearest-neighbour (k-NN) classifier, proposed in a 1967 paper by Cover and Hart, is the purest embodiment of this idea: classify a query point by the majority label among its k nearest neighbours according to some distance function. The choice of metric directly determines which points are neighbours and thus which labels are retrieved. Fix’s observation that k-NN error rate converges to within twice the Bayes error rate under mild conditions holds only when the distance metric is aligned with the true class structure — motivating the entire field of metric learning, which asks: can we learn a distance function from supervision that better captures the task-relevant notion of similarity than any handcrafted metric?
The curse of dimensionality (Bellman, 1961) is the central challenge for distance metrics in high-dimensional feature spaces. As dimensionality d grows, the ratio of the distances to the nearest and farthest points from any query converges to 1 under broad distributional assumptions — all points become equally far away, and the notion of “nearest neighbour” loses meaning. This degradation motivates both lower-dimensional Representation Learning (encoding inputs into a compact latent space where distances remain meaningful) and the development of metrics specifically suited to high-dimensional geometry, such as cosine distance (which is robust to uniform scale expansion of all dimensions) and Mahalanobis distance (which removes the effect of correlated dimensions by whitening). Modern deep metric learning sidesteps the curse by learning a dimensionality-reducing neural encoder that maps high-dimensional inputs (images, text documents, protein sequences) into a compact embedding space where a simple Euclidean or cosine metric captures the structure of interest, effectively learning both the representation and the metric jointly from labelled examples.
Components / Architecture
A production distance metric system comprises several interconnected layers, each contributing to the overall geometry of the similarity computation:
-
Metric family selection: The choice of base metric family — L_p norms (Euclidean = L2, Manhattan = L1, Chebyshev = L∞), cosine/angular distance, Mahalanobis distance, or a learned neural embedding distance — determines the fundamental geometric assumptions. L2 distance assumes isotropic feature space where all dimensions contribute equally; Mahalanobis distance assumes a Gaussian covariance structure that can be estimated from data; cosine distance assumes directional alignment is more meaningful than absolute magnitude; learned distances make no fixed parametric assumption about geometry.
-
Feature normalisation: Raw features must be normalised before most distance metrics perform sensibly. Min-max scaling places all features in [0,1]; standard z-score normalisation gives each dimension unit variance; L2 normalisation projects vectors onto the unit hypersphere (converting cosine distance to Euclidean distance equivalence). Without normalisation, high-variance features dominate Euclidean distance, causing it to ignore informative lower-variance dimensions entirely.
-
Dimensionality reduction: Dimensionality Reduction via PCA, UMAP, or neural encoder reduces d from thousands or millions to hundreds or tens, concentrating variance in the reduced space and alleviating the curse of dimensionality before distance computation. The right combination of dimensionality reduction and metric selection is often task-specific.
-
Metric learning module: When labelled similarity supervision is available, metric learning algorithms (LMNN, NCA, ITML, or deep contrastive/triplet training) fit the metric parameters to maximise alignment between geometric distance and semantic similarity. This can be as simple as learning a positive definite matrix M such that the Mahalanobis distance d_M(x, y) = √((x-y)^T M (x-y)) separates same-class points while bringing cross-class neighbours together, or as complex as a full deep neural encoder trained with InfoNCE or triplet loss.
-
Indexing structure: The triangle inequality property of proper metrics is the key that unlocks efficient approximate search: branch-and-bound in KD-trees and ball trees exploit the triangle inequality to prune subtrees that cannot contain the true nearest neighbour; HNSW graph search converges through navigating a proximity graph whose construction relies on the locality implied by the metric; Locality-Sensitive Hashing for L2 and cosine metrics probabilistically collapses nearby points into the same hash bucket. All of these techniques are impossible or significantly degraded for non-metric similarity functions.
-
Distance computation hardware: Modern systems accelerate distance computation via BLAS/LAPACK routines for L2 and inner products, AVX-512 SIMD for vectorised Euclidean and cosine computation on CPU, and cuBLAS/CUDA tensor cores on GPU for batched similarity matrix computation. FAISS (Meta AI) provides GPU-accelerated exact and approximate distance computation for L2 and inner product, handling billion-scale corpora.
Major Metric Families
-
Euclidean Distance (L2): d(x,y) = √(Σᵢ (xᵢ - yᵢ)²). The standard geometric distance, optimal for Gaussian-distributed features with uncorrelated dimensions and equal variance. Sensitive to feature scale — requires normalisation. Used in k-means clustering, k-NN classification, PCA dimensionality reduction.
-
Manhattan Distance (L1): d(x,y) = Σᵢ |xᵢ - yᵢ|. Measures grid-path distance; preferable to L2 in high-dimensional spaces and for sparse features because it does not square large deviations. Used in LASSO regression, robust statistics, and city-grid routing problems. L1 distance preserves contrast in high dimensions longer than L2 before the curse of dimensionality erodes discriminative power.
-
Minkowski Distance (Lp): d(x,y) = (Σᵢ |xᵢ - yᵢ|^p)^(1/p). Generalises L1 (p=1) and L2 (p=2); L∞ (p→∞) equals the Chebyshev distance, the maximum dimension-wise difference. Different values of p make different geometric assumptions about feature space geometry. p < 1 violates the triangle inequality and produces non-metric but sometimes useful functions.
-
Mahalanobis Distance: d_Σ(x,y) = √((x-y)^T Σ⁻¹ (x-y)) where Σ is the feature covariance matrix. Accounts for correlations and heterogeneous variances by whitening the feature space: it measures how many standard deviations separate x and y along the principal axes of the covariance. Reduces to Euclidean distance when Σ = I (identity). Particularly effective for multivariate outlier detection, Gaussian discriminant analysis, and the learned Mahalanobis formulation in metric learning (LMNN, ITML). The covariance matrix Σ or its inverse (precision matrix) can be learned from labelled data, giving a convex metric learning formulation.
-
Cosine Distance / Angular Distance: 1 - cos(θ) or arccos(cos(θ))/π. Measures directional dissimilarity rather than absolute spatial distance. The angular distance arccos(A·B / ‖A‖‖B‖) is a proper metric satisfying the triangle inequality; raw cosine similarity (1 - angle/π) is a similarity not a distance. Dominant in text and Vector Embedding applications where vector magnitude is uninformative. Computed efficiently as inner product after L2 normalisation.
-
Hamming Distance: Count of positions where two fixed-length strings differ. Proper metric for categorical/binary data; used in error-correcting codes (Hamming code), DNA sequence analysis (single-nucleotide polymorphism counting), and binary neural network quantisation search. Hamming distance search is implementable via popcount instructions, enabling extremely fast similarity search on binary embeddings.
-
Edit Distance (Levenshtein): Minimum number of single-character insertions, deletions, or substitutions to transform one string into another. A proper metric; used in spell-checking, DNA/protein sequence alignment, OCR post-processing, and plagiarism detection. Dynamic programming computes exact edit distance in O(m·n) for strings of length m, n; approximate fast methods exist for large-scale search.
-
Jaccard Distance: 1 - |A∩B|/|A∪B| for sets or binary vectors. Measures set dissimilarity; used in de-duplication, near-duplicate document detection (MinHash approximation), and bioinformatics species composition analysis. Jaccard is a proper metric.
-
Earth Mover’s Distance (Wasserstein-1): Measures the minimum work required to transform one probability distribution into another. A proper metric on probability distributions; used in generative model evaluation, domain adaptation, and optimal transport problems. Computationally expensive (linear programming) but can be approximated efficiently with entropy regularisation (Sinkhorn iterations).
-
Hyperbolic Distance: d_H(x,y) in the Poincaré ball or hyperboloid model of negatively curved Riemannian space, defined by the Riemannian metric tensor. Grows exponentially from the origin, making it naturally suited to hierarchical data (trees, taxonomies, ontologies) where Euclidean distance fails to embed tree structure without distortion. Used in knowledge graph embeddings, taxonomic classification, and drug-disease hierarchical link prediction.
Use Cases / Major Families
-
Nearest Neighbor Search and Retrieval-Augmented Generation: The choice of distance metric is the first decision in designing a retrieval pipeline. FAISS, Qdrant, Weaviate, and Milvus support L2, inner product (cosine on normalised vectors), and Manhattan distance natively. The dominant choice in 2026 RAG pipelines is cosine similarity over dense embeddings from transformer encoders, because it aligns with the contrastive training objectives of modern embedding models.
-
Clustering (k-means, DBSCAN, hierarchical): k-means with Euclidean distance is the industry workhorse; DBSCAN uses a configurable metric to define neighbourhood density and works well with non-Euclidean metrics for geospatial or graph data; hierarchical clustering supports arbitrary metrics via linkage methods. The metric choice determines which cluster geometries can be recovered: k-means with L2 recovers spherical clusters; DBSCAN with appropriate metric handles arbitrary shapes.
-
Face recognition and person re-identification: Deep metric learning with triplet loss or ArcFace loss trains embedding networks to cluster identities in Euclidean space. FaceNet (Google, 2015) demonstrated that a deep convolutional network trained to produce L2-normalised embeddings where Euclidean distance equals face dissimilarity achieves human-level face verification. The margin-based softmax variants (CosFace, ArcFace, SphereFace) impose angular constraints directly, making cosine distance the natural verification metric.
-
Drug discovery and molecular similarity: Molecular fingerprints (Morgan/circular fingerprints, ECFP4) encode chemical structures as binary vectors; Tanimoto similarity (equivalent to Jaccard similarity on binary fingerprints) is the dominant molecular distance in virtual screening. Continuous molecular embeddings from graph neural networks use L2 or cosine distance to retrieve structurally similar candidate compounds for lead optimisation.
-
Bioinformatics sequence analysis: Edit distance (Levenshtein) for protein/DNA sequence alignment, Hamming distance for SNP comparison, and tree edit distance for RNA secondary structure comparison are the domain-standard metrics. Learned metric embeddings from protein language models (ESM-2, ProtTrans) enable cosine or L2 distance search for functional protein similarity, supplementing or replacing handcrafted string metrics.
-
Anomaly Detection: Mahalanobis distance is the theoretical-optimal anomaly score for Gaussian data — it measures how many standard deviations a point lies from the class mean, correctly accounting for correlations. k-NN anomaly detection with appropriate metric identifies outliers as points whose distance to their nearest neighbours is abnormally large, without assuming any specific distribution.
-
Recommendation systems: Collaborative filtering embeds users and items in a latent space; cosine or inner product distance identifies items similar to a user’s consumption history. Mahalanobis metric learning adapts the attribute space to user preference geometry, improving context-aware recommendation by up to 7% over cosine similarity on benchmark datasets.
-
Geospatial Intelligence and location-based services: Haversine distance measures great-circle distance on the Earth’s surface, enabling efficient radius searches and proximity clustering for geospatial data. Spatial indexing with R-trees and geohashes exploits metric properties for sub-millisecond proximity lookups in GPS-scale datasets.
Academic Context
The formal definition of a metric space was introduced independently by Maurice Fréchet in his 1906 doctoral thesis at the Collège de France and systematised by Felix Hausdorff in his 1914 foundational text Grundzüge der Mengenlehre. The specific application of distance metrics to data analysis emerged through the development of multivariate statistics; Prasanta Chandra Mahalanobis introduced his eponymous distance in a 1936 paper published in the Proceedings of the National Institute of Sciences of India, providing the first statistical metric that accounted for feature correlations and scale differences.
The machine learning application of distance metrics crystallised through Cover and Hart’s 1967 IEEE Transactions on Information Theory paper establishing the theoretical guarantees of k-nearest-neighbour classification, and through Duda and Hart’s 1973 textbook Pattern Classification and Scene Analysis, which placed distance-based methods within the broader framework of statistical pattern recognition. Metric learning as a distinct research programme began with Eric Xing et al.’s seminal NeurIPS 2002 paper “Distance Metric Learning with Application to Clustering with Side Information,” which formulated learned Mahalanobis distance as a semidefinite programming problem. Weinberger and Saul’s 2009 JMLR paper introducing LMNN (Large Margin Nearest Neighbour) formulated metric learning as a convex optimisation problem requiring same-class k-NN targets to be pulled together while opposite-class impostors are pushed beyond a margin, establishing the framework that dominated classical metric learning.
The deep metric learning era was inaugurated by Chopra, Hadsell and LeCun’s 2005 CVPR paper introducing contrastive loss for learning face verification metrics via Siamese networks, followed by Schroff, Kalenichenko and Philbin’s 2015 CVPR paper “FaceNet: A Unified Embedding for Face Recognition and Clustering,” which introduced triplet loss training at Google scale and achieved state-of-the-art face verification. Numerous proxy-based and classification-based alternatives to triplet and contrastive loss have since emerged: ProxyNCA (Movshovitz-Attias et al., 2017), ArcFace (Deng et al., 2019), and the multi-similarity loss (Wang et al., 2019) all seek to improve convergence stability and mining efficiency over batch-based sampling. A 2022 survey by Kaya and Bilge in MDPI Symmetry provided a comprehensive taxonomy of deep metric learning methods, distinguishing pair-based, proxy-based, and classification-based families. The February 2026 paper “A Review on Riemannian Metric Learning: Closer to You than You Imagine” (arXiv:2503.05321) surveys the frontier of non-Euclidean metric learning, covering hyperbolic, spherical, and product-space metrics for hierarchical and heterogeneous data.
Current Landscape (2026)
In 2026, distance metrics are implemented at scale through specialised vector database infrastructure. FAISS (Meta AI), Qdrant, Weaviate, Milvus, and pgvector all support multiple distance metrics natively, with the dominant production choice being cosine similarity (implemented as inner product on L2-normalised embeddings) for dense vector embeddings from transformer encoders. Euclidean distance remains important for structured, normalised tabular data and for k-means clustering within vector database indexing (IVF cluster assignment). The VIBE (Vector Index Benchmark for Embeddings) benchmark, presented in 2025, provides standardised evaluation of recall and latency trade-offs across distance metrics and index configurations, complementing the established ANN-Benchmarks.
Deep metric learning has matured substantially. Contrastive learning objectives (SimCLR, CLIP, E5, BGE) have displaced hand-tuned triplet mining in large-scale settings; these objectives implicitly learn a cosine metric space by training the encoder to place positive pairs close and negative pairs far apart in embedding space. Matryoshka Representation Learning (Kusupati et al., 2022, widely deployed 2024–2025) produces embeddings where cosine distance is preserved at multiple nested dimensionalities, allowing the effective distance metric to be computed at lower cost by truncating the embedding vector without reindexing.
Riemannian and non-Euclidean metric learning is an active research frontier. Hyperbolic embeddings in the Poincaré ball (Nickel and Kiela, 2017) and on the hyperboloid (Ganea et al., 2018) outperform Euclidean embeddings for hierarchical data such as ontology subgraph embeddings and taxonomic classification, and are increasingly used in Knowledge Graph Embedding applications. The February 2026 paper RiemannGL explores the integration of Riemannian geometry into graph deep learning, extending message-passing operations to curved manifolds via exponential and logarithmic maps. Pseudo-Riemannian metrics (mixed-signature manifolds) and product-space metrics (combining Euclidean, spherical, and hyperbolic components) are emerging as flexible frameworks for heterogeneous real-world data where no single geometry is globally appropriate.
GPU-accelerated distance computation via NVIDIA CUVS (CUDA Unified Vector Search) and FAISS-GPU enables billion-scale exact or approximate distance computation in seconds on A100/H100 clusters, making precise metric selection less of a bottleneck than it once was and allowing practitioners to evaluate multiple metrics empirically rather than choosing by mathematical intuition alone.
UK Context
The UK has made substantial contributions to the theoretical foundations and practical applications of distance metrics. William Rowan Hamilton’s development of quaternions at Trinity College Dublin (1843) and Arthur Cayley’s matrix algebra (Cambridge, 1858) provided the mathematical foundations for linear metric spaces. The metric-space framework itself was partly shaped by British mathematicians in the early twentieth century, including Bertrand Russell’s foundational work on mathematical structure at Cambridge. Mahalanobis’s distance, though Indian in origin, was developed partly through his collaboration with British statistical institutions, particularly his engagement with Karl Pearson’s Biometric Laboratory and Ronald Fisher at Rothamsted Experimental Station.
In contemporary machine learning, Stephen Robertson’s BM25 (Robertson and Spärck Jones, 1994 — City University London / British Library) is a cornerstone retrieval function that implements a probabilistic similarity measure over TF-IDF feature vectors, representing a practically successful alternative to pure L2 distance for text. Karen Spärck Jones’s IDF weighting (Cambridge, 1972) — which underpins TF-IDF and thereby the entire text vector space model — is the British contribution that makes cosine distance meaningful for text data by de-weighting common terms that contribute more noise than signal to Euclidean or cosine distances.
University of Edinburgh’s School of Informatics maintains active research in efficient nearest-neighbour search and metric learning, including contributions to approximate distance computation in graph databases. The University of Manchester’s Information Management Group has a long track record in ontology-based matching and semantic distance for biomedical knowledge graphs, using Mahalanobis and cosine metrics for ontology alignment. Sheffield NLP at the University of Sheffield contributes to metric-based sentence similarity evaluation. Imperial College London’s machine learning groups have published on Riemannian metric learning for biomedical data and on hyperbolic geometry applications to clinical knowledge graphs.
UK industry deployment is extensive. Graphcore’s IPU architecture provides hardware-accelerated L2 and inner product distance computation for embedding similarity workloads. Faculty AI uses metric learning in production fraud detection systems for UK financial institutions. Deepmind (London) employs learned distance metrics in AlphaFold protein structure retrieval, where cosine and Euclidean distances over structure embedding spaces guide candidate retrieval for drug target identification. NHS AI applications use embedding-based similarity search with cosine and Mahalanobis metrics for patient cohort identification and clinical trial matching, with the NHS AI Lab coordinating deployment standards across trusts.
In Northern England, the Leeds Institute for Data Analytics uses metric-based clustering for social science and health data, including geospatial Haversine distance for deprivation index mapping. The Alan Turing Institute, headquartered in London with university partners including Manchester and Edinburgh, coordinates UK research on distance metric selection for fairness-aware machine learning — an area where inappropriate metrics can encode and amplify demographic disparities.
Future Directions (2026–2030)
-
Learned adaptive metrics: Rather than training a single metric for a task, meta-learning approaches adapt the effective distance metric per query or per domain at inference time, enabling specialised similarity judgment for different user intents without reindexing. Instruction-tuned embedding models (GTE-Qwen, E5-instruct) already condition the metric space on a natural language description of the desired similarity concept.
-
Riemannian and product-space metrics: Combining Euclidean, spherical, and hyperbolic metric components in learned product-space embeddings to handle mixed hierarchical and non-hierarchical data structure. Pseudo-Riemannian and ultra-hyperbolic spaces extend this to indefinite-signature manifolds, potentially capturing more complex topological structure.
-
Quantum-metric-inspired distances: Quantum information distance measures (quantum Wasserstein distance, quantum relative entropy) are being explored as distance metrics for quantum data in quantum machine learning; though primarily theoretical in 2026, they may become practical as quantum hardware matures.
-
Privacy-preserving distance computation: Secure multi-party computation and homomorphic encryption protocols for computing Euclidean and cosine distances over encrypted embedding vectors without revealing raw embeddings — enabling privacy-preserving Nearest Neighbor Search in federated settings. This is particularly relevant for clinical data applications where raw patient embeddings must remain confidential.
-
Neurosymbolic distance metrics: Integrating symbolic knowledge (ontology axioms, logical constraints) into learned metric spaces so that distances reflect not just statistical co-occurrence but also logical structure — for example, ensuring that ontologically related concepts are metrically close even when they rarely appear together in training data.
-
Binary and sparse metric approximations: Extreme compression of dense embedding vectors to 1-bit (Hamming distance) or sparse quantised representations, achieving orders-of-magnitude speedup in distance computation with controlled recall loss. GPU popcount and SIMD bitwise operations make binary distance computation far faster than float32 L2 or inner product, enabling trillion-scale retrieval on commodity hardware.
-
Calibrated distance metrics: Post-hoc calibration of raw distance scores to meaningful probability values, analogous to calibrated cosine similarity — a calibrated distance of 0.3 means something specific about semantic similarity rather than being an arbitrary scale-dependent number. Isotonic regression and Platt scaling applied to metric outputs are the near-term practical approaches.
Formal Algorithms
Metric-Based k-NN Classification
Input: Training set {(x_1, y_1), ..., (x_n, y_n)}, query x, metric d, k Output: Class label for x 1. Compute d(x, x_i) for all i in 1..n 2. Sort distances in ascending order 3. Select k points with smallest distances: N_k(x) = {x_(1), ..., x_(k)} 4. Return majority label: argmax_{c} |{i : x_(i) in N_k(x), y_i = c}|Time complexity: O(n·d) for exact search; O(log n · d) with HNSW indexing (approximate).
LMNN Objective (Learned Mahalanobis Metric)
Minimize over M (PSD): Σ_{i,j∈N(i)} d_M(x_i, x_j)² + c · Σ_{i,j∈N(i),l∉C(i)} max(0, 1 + d_M(x_i, x_j)² - d_M(x_i, x_l)²) Subject to: M is positive semidefinite Where: N(i) = target neighbours of x_i (same class) C(i) = same-class set of x_i c = regularisation parameter balancing pull vs. push objectivesTriplet Loss Gradient Update
For triplet (anchor x_a, positive x_p, negative x_n): L = max(0, d(f(x_a), f(x_p))² - d(f(x_a), f(x_n))² + margin) If L > 0: (active triplet) ∂L/∂f(x_a) = 2(f(x_n) - f(x_p)) [move anchor away from positive, toward negative] ∂L/∂f(x_p) = 2(f(x_a) - f(x_p)) [move positive toward anchor] ∂L/∂f(x_n) = 2(f(x_a) - f(x_n)) [move negative away from anchor] Backpropagate through f to update encoder parametersApproximate Nearest Neighbour via LSH (Cosine)
Index construction: For r in 1..L (hash functions): Sample random unit vector r_r ~ N(0, I) h_r(x) = sign(r_r · x) [random hyperplane hash] Store each x_i in bucket (h_1(x_i), ..., h_L(x_i)) Query(x, K): Candidate set C = all points in bucket (h_1(x), ..., h_L(x)) Return K points from C with highest cosine similarity to x Note: |C| << n typically, achieving sub-linear query timeFormal Mathematical Properties
The four axioms that define a metric d: X × X → R on a set X encode concrete mathematical guarantees about the behaviour of the distance function:
Non-negativity: d(x, y) ≥ 0 for all x, y ∈ X. This is the minimal requirement for a meaningful notion of distance — no two points can be “negatively” far apart. Combined with the identity axiom, non-negativity implies that distance is always a non-negative real number, with zero achievable only for identical points.
Identity of indiscernibles: d(x, y) = 0 if and only if x = y. This axiom rules out “pseudo-metrics” (which only require d(x, x) = 0) and ensures that the distance function fully characterises point identity. In machine learning applications, this axiom is sometimes violated by similarity functions over approximate or hashed representations, where distinct inputs may receive identical compact representations; such functions are pseudo-metrics, not proper metrics.
Symmetry: d(x, y) = d(y, x). Distance is direction-independent: the distance from x to y equals the distance from y to x. Most machine learning similarity functions satisfy this property. Notable exceptions include directed divergences (Kullback-Leibler divergence, asymmetric edit distances for insertion vs deletion) and the asymmetric cosine similarity variant used in maximum inner product search (MIPS) where query and document embeddings are treated differently.
Triangle inequality: d(x, z) ≤ d(x, y) + d(y, z) for all x, y, z ∈ X. This is the most computationally powerful axiom, expressing that no detour can be shorter than a direct path. The triangle inequality enables branch-and-bound search pruning in metric trees: if d(q, centre) > r + R (where r is the known nearest-neighbour distance and R is the region radius), the entire region can be excluded without checking individual points. Approximate nearest-neighbour algorithms that violate the triangle inequality (e.g., those using learned similarity functions without metric guarantees) cannot exploit this pruning and must rely on sampling or hashing approaches instead.
A function satisfying only non-negativity, symmetry, and the triangle inequality (but allowing d(x, y) = 0 for x ≠ y) is a semi-metric or pseudo-metric. Functions satisfying all four axioms are proper metrics. The family of L_p distances for p ≥ 1 are all proper metrics (by Minkowski’s inequality); for 0 < p < 1, the triangle inequality fails, producing quasi-metrics that are sometimes used in sparse vector space models.
Metric space topology: Every metric induces a natural topology via open balls B(x, r) = {y : d(x, y) < r}. This topology determines which sequences converge, which functions are continuous, and which sets are compact — all notions with direct machine learning implications. A learned embedding function f: X → R^d is continuous with respect to the original metric on X and the Euclidean metric on R^d if and only if semantically similar inputs are mapped to nearby embeddings, which is precisely the objective of deep metric learning.
Completeness and separability: A metric space is complete if every Cauchy sequence converges within the space, and separable if it has a countable dense subset. These properties determine whether standard analysis tools (fixed-point theorems, spectral theory) apply. The Euclidean space R^d is complete and separable; the space of text embeddings generated by a neural encoder, viewed as a metric subspace of R^d under Euclidean distance, inherits these properties. Riemannian manifolds such as the Poincaré ball model of hyperbolic space are complete and separable Riemannian metric spaces, making them suitable for learning metric embeddings with hyperbolic distance.
Classical Metric Learning: From LMNN to Deep Methods
Classical metric learning operates on handcrafted feature representations and learns a parametric transformation of the feature space such that the induced distance better aligns with class structure or domain-specific similarity. The most studied parametric family is the Mahalanobis distance d_M(x, y) = √((x-y)^T M (x-y)) for positive semidefinite matrix M. Learning M is a semidefinite programme (SDP) that constrains same-class distances to be small and cross-class distances to be large; the learned M can be factored as M = L^T L, where L is a linear transformation that maps inputs into a “whitened” space where Euclidean distance equals the learned Mahalanobis distance.
LMNN (Large Margin Nearest Neighbour): Introduced by Weinberger and Saul (2009), LMNN learns M to satisfy two types of constraints: (1) target neighbours — the k designated same-class neighbours of each training point should remain close (pulled in); and (2) impostors — opposite-class points that intrude within the target neighbourhood should be pushed outside a margin of 1 + d_M(x, target_neighbour). The objective is a sum of two terms: the summed target-neighbour distances (encourage clustering within classes) and the summed hinge losses on impostor pairs (encourage between-class separation). LMNN is a convex SDP that can be solved efficiently via gradient descent on the Frobenius norm of the learning matrix, and achieves strong k-NN classification results on benchmarks including MNIST, USPS, and various tabular datasets. The Nearest Neighbor Search performance improvements from LMNN over Euclidean distance can be dramatic for datasets with correlated or irrelevant features: on some biomedical datasets, LMNN reduces k-NN error by 40%+ relative to the baseline.
NCA (Neighbourhood Components Analysis): Goldberger et al. (2005) proposed optimising a stochastic k-NN objective — the expected fraction of correctly classified training examples under a softened k-NN rule — directly. NCA parameterises the distance metric via a linear transformation A (so d(x, y) = ‖Ax - Ay‖), and learns A to maximise the sum of probabilities that each training example selects a same-class neighbour under a softmax over Euclidean distances in the transformed space. Unlike LMNN, NCA is not convex and may converge to local optima, but it directly optimises the quantity of interest (correct neighbourhood) without the hinge-loss approximation.
ITML (Information-Theoretic Metric Learning): Davis et al. (2007) formulated metric learning as a Bregman optimisation problem, minimising the KL divergence from a prior metric (typically Euclidean) to the learned Mahalanobis metric subject to distance constraints. ITML handles both upper bounds (similar pairs should be close) and lower bounds (dissimilar pairs should be far) on pairwise distances, making it flexible for datasets with diverse constraint types.
Deep Metric Learning Objectives: The move to deep neural encoders required objectives that work with stochastic mini-batch sampling and non-convex optimisation. The key objectives are:
-
Contrastive loss (Chopra et al., 2005): For positive pairs (xᵢ, xⱼ) and negative pairs (xᵢ, xₖ), minimise d(fᵢ, fⱼ)² + max(0, m - d(fᵢ, fₖ))² for margin m. The contrastive loss is simple and interpretable but suffers from hard negative mining instability and slow convergence on large datasets.
-
Triplet loss (Schroff et al., 2015): For anchor xₐ, positive xₚ, negative xₙ from different class, minimise max(0, d(fₐ, fₚ) - d(fₐ, fₙ) + m). The triplet loss more directly encodes relative ordering requirements and is particularly effective for few-shot learning when used with hard online mining, but requires careful triplet sampling to avoid degenerate solutions where all gradients are zero.
-
Multi-similarity loss (Wang et al., 2019): A generalised pairwise weighting that mines pairs by both self-similarity and relative similarity, addressing the instability of contrastive and triplet sampling while providing tighter objective-gradient alignment.
-
ProxyNCA (Movshovitz-Attias et al., 2017): Represents each class by a learnable proxy vector; classification-style softmax loss over proxy-point distances avoids the O(n²) or O(n³) pair/triplet enumeration overhead while approximating the pairwise objective.
-
ArcFace/CosFace (Deng et al., 2019; Wang et al., 2018): Angular margin penalty added to the cosine similarity between embeddings and class-weight vectors in softmax classification, directly optimising the angular (cosine) geometry of the embedding space rather than the Euclidean geometry.
Indexing Structures That Exploit Metric Properties
The computational value of proper distance metrics in machine learning systems derives largely from their enablement of efficient indexing structures for Nearest Neighbor Search. These structures exploit the triangle inequality to prune exponential numbers of distance computations:
KD-Tree (k-dimensional tree): A binary space-partitioning tree that recursively bisects the feature space along alternating dimensions at median values. At query time, the triangle inequality enables ball pruning: when the distance from the query to the nearest point in a subtree’s bounding box exceeds the current best distance, the entire subtree can be discarded. KD-trees are optimal for low-dimensional Euclidean space (d ≤ 20) where the pruning is highly effective; in high dimensions, the bounding boxes become loose approximations and most subtrees must be explored, degrading toward linear scan. The cosine metric is handled by projecting onto the unit sphere before KD-tree construction.
Ball Tree: Partitions space using hyperspheres (balls) rather than axis-aligned boxes, achieving tighter pruning bounds in moderately high dimensions when the data lies on a lower-dimensional manifold. The triangle inequality pruning condition becomes: if d(q, centre) - R > best_d, prune (where R is the ball radius). Ball trees work with arbitrary metrics — including Mahalanobis and cosine distances — by computing distances to ball centres and radii in the target metric.
Locality-Sensitive Hashing (LSH): For L2 and cosine metrics, LSH uses families of random projection hash functions h_r(x) = sign(r · x) (cosine LSH) or h_r,b(x) = ⌊(r · x + b) / w⌋ (L2 LSH) such that nearby points collide in the same hash bucket with probability proportional to their similarity. LSH is sub-linear at query time and supports streaming updates, making it suitable for dynamic corpora. The locality-sensitive property depends on the metric: LSH constructions exist for L2, L1, cosine, Hamming (via bit sampling), and Jaccard (MinHash), but not for all metrics.
HNSW (Hierarchical Navigable Small World): A graph-based ANN index that constructs a multi-layer proximity graph: higher layers connect distant nodes for fast long-range navigation, lower layers connect local neighbours for precise retrieval. The greedy graph traversal at query time implicitly exploits the metric’s triangle inequality by preferring candidates that are closer to the query than the current node. HNSW supports arbitrary symmetric distance functions (not just Euclidean) — it is the primary index structure for cosine similarity search in production vector databases (Qdrant, Weaviate, Milvus).
Inverted File Index (IVF): Clusters the corpus using k-means (Euclidean distance) into Voronoi cells; at query time, only the nearest
nprobeclusters are searched. IVF depends on Euclidean distance for both cluster assignment and within-cluster search, making it less suitable for non-Euclidean metrics. Combined with Product Quantisation (PQ), IVF+PQ enables billion-scale Nearest Neighbor Search in FAISS with memory reduced by 8–32× through lossy vector compression. The Euclidean distance is approximated via precomputed codebook lookup tables, enabling fast asymmetric distance computation.Benchmark Datasets and Evaluation
Systematic evaluation of distance metrics and metric learning algorithms relies on standardised datasets and protocols:
-
CUB-200-2011 (Caltech-UCSD Birds): 11,788 images of 200 bird species; standard benchmark for deep metric learning with species as the semantic class. Performance measured by Recall@k (fraction of queries with a same-species image in the top k returned) with the standard 100/100 train/test class split.
-
Stanford Online Products (SOP): 120,053 product images from eBay; 22,634 classes with roughly 5 images each. Recall@1, @10, @100 are the standard metrics. A challenging benchmark due to fine-grained intra-class variation and large class count.
-
In-Shop Clothes Retrieval: 7,982 clothing items with 52,712 images; evaluated on Recall@k and mean Average Precision (mAP) for retrieval of matching clothing items across pose and appearance variation.
-
DML-Benchmark (2022): A standardised evaluation suite introduced by Musgrave et al. (2020) applying consistent train/test splits, architecture constraints, and hyperparameter budgets across methods, revealing that many claimed improvements in deep metric learning do not survive controlled comparison — a cautionary result for the field’s benchmarking practices.
-
ANN-Benchmarks (ann-benchmarks.com): The reference benchmark for Nearest Neighbor Search efficiency, measuring recall vs. queries-per-second Pareto frontiers across HNSW, IVF+PQ, ScaNN, LSH, and other index types on standard datasets (SIFT-1M, GIST-1M, Fashion-MNIST, GloVe-25/100/200) under L2 and cosine distance.
-
BEIR Benchmark: For text-domain retrieval with cosine similarity, BEIR’s 18 datasets cover diverse retrieval tasks; performance is measured by NDCG@10. Cosine similarity over dense embeddings is the standard distance function for BEIR evaluation.
Metric Learning in the 2026 Landscape
The practical deployment of metric learning in 2026 is dominated by the large-scale contrastive and vision-language pre-training paradigm, which has largely superseded classical convex metric learning methods for high-dimensional inputs. The CLIP model (Radford et al., OpenAI 2021) trained image and text encoders with a symmetric contrastive loss over 400 million image-caption pairs, effectively learning a shared Euclidean/cosine metric space where image and text representations of the same concept cluster together. This approach scales effortlessly to internet-scale data, produces general-purpose metrics that transfer across domains, and requires no manual label engineering — only paired co-occurrence supervision. DINOv2 (Oquab et al., Meta 2024) extended this to vision-only self-supervised metric learning using distilled self-attention objectives, producing visual embeddings where nearest-neighbour retrieval by Euclidean distance achieves state-of-the-art image classification without any task-specific fine-tuning.
The Vector Database industry has standardised metric support substantially. Qdrant (as of v1.9, 2025) supports L2 (Euclidean), cosine (dot product on normalised vectors), dot product (unnormalised inner product), and Manhattan (L1) as native distance metrics, with the ability to specify per-collection metrics and mixing metrics across named vector spaces in the same collection. Weaviate similarly supports L2 and cosine natively. Milvus adds Hamming and Jaccard for binary embeddings. pgvector (PostgreSQL extension) supports L2, cosine, and inner product with IVFFLAT and HNSW indexing. The standardisation of metric support across vector databases makes it practical to evaluate multiple metrics for a given task empirically, enabling data-driven metric selection rather than purely theoretically motivated choices.
Qdrant’s benchmarks (2025) show that for typical dense text embedding corpora (OpenAI ada-002, E5-large), cosine similarity and L2 distance produce identical HNSW index recall-latency performance when embeddings are L2-normalised before indexing — confirming the theoretical equivalence on the unit sphere. For unnormalised embeddings (e.g., from models trained without normalisation), cosine slightly outperforms L2 on retrieval quality because it ignores magnitude variation that is an artefact of model calibration rather than semantic content.
The 2025 Qdrant “awesome-metric-learning” curated list documents the current practical state of metric learning applications, including: face recognition (ArcFace on MS-Celeb and Glint360K), person re-identification (triplet loss with hard mining on Market-1501 and DukeMTMC), vehicle re-identification, medical image retrieval (contrastive learning on CheXpert and NIH-ChestX-ray14), satellite image search (CLIP-style contrastive learning on remote sensing datasets), and code search (CodeBERT with contrastive fine-tuning on CodeSearchNet).
Recent work on calibrated distance metrics addresses the fundamental problem that raw distances from deep metric learning have arbitrary scale — a distance of 0.5 may mean “highly similar” for one model and “moderately dissimilar” for another, making threshold-based retrieval decisions fragile across models. Isotonic regression calibration (mapping raw distances to empirically calibrated probabilities) and temperature scaling have been proposed as post-hoc fixes; the Calibrated Similarity paper (2026) applies this to cosine similarity directly, but the same approach is applicable to Euclidean distances from deep metric learning. Calibrated metrics are important for Anomaly Detection applications where absolute distance thresholds determine the anomaly/normal boundary.
Distance Metrics in Multimodal and Cross-Modal Settings
The extension of distance metrics to multimodal settings — where inputs may be text, images, audio, video, or structured data — requires metrics that operate in a shared embedding space where distances are meaningful across modalities. The CLIP architecture (Radford et al., 2021) established the paradigm: train separate encoders for text and images with a contrastive loss that maximises cosine similarity between paired (image, caption) embeddings while minimising cosine similarity between non-paired embeddings from the same batch. The resulting shared metric space supports cross-modal Nearest Neighbor Search — finding images by text query, or finding text captions by image query — using standard cosine distance.
ImageBind (Girdhar et al., Meta 2023) extended this to six modalities (image, text, audio, depth, thermal, IMU), training all encoders to align with image embeddings using the CLIP contrastive objective. The resulting metric space supports any-to-any cross-modal retrieval: an audio clip can retrieve related images, which can retrieve related text descriptions, because all modalities share the same cosine metric space. This “binding” property of the shared metric space is particularly powerful for multimodal Retrieval-Augmented Generation pipelines where context may be drawn from different modalities.
Specialised domains have developed domain-adapted metric spaces. ChemBERTa (Chithrananda et al., 2020) and MolBERT (Fabian et al., 2020) produce molecular embeddings where Euclidean distance approximates Tanimoto chemical similarity; ESM-2 (Lin et al., Meta 2023) produces protein sequence embeddings where cosine distance correlates with functional and structural relatedness; UniXcoder (Guo et al., Microsoft 2022) produces code embeddings where cosine distance captures semantic code similarity. In each domain, the distance metric encodes domain-specific similarity that would require manual engineering with handcrafted features and classical metrics.
Distance Metrics and Fairness
An often-overlooked property of distance metrics in machine learning is their interaction with demographic fairness. When a distance metric — or more typically a learned embedding metric — assigns systematically different inter-group distances than intra-group distances for protected attribute groups (gender, race, age), downstream systems that use the metric for clustering, retrieval, or classification can perpetuate or amplify historical biases present in training data.
Empirical work has documented several forms of metric bias: facial recognition systems trained with Euclidean or cosine deep metric learning exhibit substantially higher false match rates for darker-skinned faces (Joy Buolamwini and Timnit Gebru’s “Gender Shades” work, MIT Media Lab 2018); word embedding spaces trained with cosine metric objectives encode occupational gender stereotypes (Bolukbasi et al., “Man is to Computer Programmer as Woman is to Homemaker?”, NeurIPS 2016); clinical embedding models may encode racial biases in diagnosis coding patterns, causing cosine similarity to cluster patients by demographic attributes rather than clinical similarity.
Fairness-aware metric learning addresses these issues by adding demographic parity, equalised odds, or counterfactual fairness constraints to the metric learning objective. The Alan Turing Institute (London, with partners at Manchester, Edinburgh, and UCL) coordinates UK research on fair metric learning, producing tools for auditing learned distance metrics for demographic bias and methods for post-hoc debiasing. The EU AI Act’s high-risk system classification for biometric identification directly implicates distance metrics used in face recognition and person re-identification systems, requiring documented metric audits and bias assessments before deployment.
Distance Metrics in Generative AI and Diffusion Models
The rise of Diffusion Models and generative AI has introduced new distance metrics and metric-like functions that play central roles in these systems:
Fréchet Inception Distance (FID): The de facto evaluation metric for generative image models. FID measures the Wasserstein-2 distance between the distribution of Inception network features of real images and generated images. Formally, FID = ‖μ_r - μ_g‖² + Tr(Σ_r + Σ_g - 2(Σ_r Σ_g)^{1/2}), where (μ, Σ) are the mean and covariance of Inception features for real (r) and generated (g) images. FID is a proper metric on distributions and has become the standard benchmark for comparing Diffusion Models, GANs, and VAEs. Lower FID indicates higher-quality, more diverse generated images. FID is computed in the 2048-dimensional Inception feature space, so it measures both the fidelity and diversity of generated images relative to the real data distribution.
CLIP distance and CLIP score: Cosine Similarity in CLIP’s joint image-text embedding space is used to evaluate semantic alignment between generated images and text prompts (CLIP score) and to measure visual similarity across modalities. CLIP-FD (CLIP Fréchet Distance) applies the Fréchet Distance formula in CLIP’s embedding space rather than Inception space, providing a more semantically grounded image quality metric. CLIP-based retrieval and reranking are increasingly used in text-to-image generation pipelines to filter generated samples for semantic quality before presenting to users.
Guidance distances in diffusion sampling: Classifier-free guidance in diffusion models modifies the score function during denoising by interpolating between conditional and unconditional score estimates: s_guided = s_unconditional + w × (s_conditional - s_unconditional). This interpolation is effectively a metric operation in score space; the guidance weight w controls the trade-off between image quality (strong guidance → low diversity but high FID on held-out sets) and prompt adherence. DDPM-based Nearest Neighbor Search for nearest-neighbour image generation (kNN diffusion, Blattmann et al. 2022) uses CLIP distance to retrieve training images similar to a text prompt and conditions generation on these retrieved neighbours, combining metric retrieval with generative modelling.
Reward model distances: Reinforcement learning from human feedback for Large Language Models (RLHF) trains a reward model that assigns scalar rewards to model outputs based on their quality relative to human preferences. The reward model’s output can be viewed as a learned quasi-distance from the generated text to the distribution of human-preferred outputs; policy gradient updates move the policy toward lower effective reward-distance. Direct Preference Optimisation (DPO, Rafailov et al. 2023) bypasses explicit reward models by directly optimising a classification objective on preferred vs. rejected pairs, implicitly learning a metric on the space of language model outputs.
Distance Metrics in Robotics and Spatial Computing
Robotics and spatial computing applications require domain-specific distance metrics that operate in physical or configuration spaces:
Configuration space distance (C-space distance): In robot motion planning, the configuration space C is the space of all possible robot configurations (joint angles, positions). Distance in C-space governs the behaviour of sampling-based motion planners: RRT (Rapidly-exploring Random Trees) and PRM (Probabilistic Roadmaps) connect configurations whose C-space distance falls below a threshold. The choice of C-space metric is non-trivial: Euclidean distance in joint-angle space weights all joints equally, but a small change in a proximal joint (shoulder) produces a much larger end-effector displacement than the same change in a distal joint (finger). Workspace distance (distance between end-effector positions) may be more appropriate for some planning problems.
Point cloud distances for LiDAR SLAM: Simultaneous Localisation and Mapping (SLAM) algorithms align successive LiDAR scans using nearest-neighbour distance in 3D Euclidean space. The ICP (Iterative Closest Point) algorithm iterates between assigning each source point to its nearest neighbour in the target cloud and estimating the rigid body transformation that minimises the sum of squared distances. Chamfer distance (sum of nearest-neighbour distances in both directions) and Earth Mover’s distance provide alternative measures of point cloud similarity used in 3D shape generation evaluation.
Spatial distance in Augmented Reality: AR systems track user pose and anchor virtual content to physical locations, requiring precise Euclidean distance computation in 3D camera or world coordinate space. The distance between a virtual anchor point and its tracked real-world location determines visual registration accuracy; submillimetre tracking errors require high-precision 3D distance computation under real-time constraints. Weaviate’s multimodal Vector Database and similar systems increasingly support combined spatial + semantic distance queries for location-aware AR information retrieval.
Relationship to Information Theory
Distance metrics connect to information-theoretic divergences, which provide an alternative framework for measuring dissimilarity between probability distributions:
Kullback-Leibler (KL) divergence: D_KL(P ‖ Q) = Σ P(x) log(P(x)/Q(x)) measures the expected log-likelihood ratio of P over Q, quantifying how many bits are needed to encode samples from P using a code optimised for Q. KL divergence is not a metric (it is asymmetric and violates the triangle inequality), but it is the foundation of variational inference (VAEs, variational inference in Bayesian Neural Networks) and KL-divergence-based losses in Reinforcement Learning (KL constraint in PPO, TRPO). The symmetrised KL divergence (Jensen-Shannon divergence) is a proper metric and is used in GAN theory.
Mutual information as a distance: Mutual information I(X; Y) = H(X) + H(Y) - H(X, Y) measures the information shared between two random variables. Normalised mutual information (NMI = 2I(X;Y)/(H(X)+H(Y))) is a symmetric distance-like measure in [0,1] used to evaluate Clustering quality by measuring agreement between predicted cluster assignments and ground-truth labels. InfoNCE loss (used in CLIP, SimCLR, and most modern contrastive learning) is derived from a lower bound on mutual information, connecting Contrastive Learning to information-theoretic metric learning.
Rényi divergence: A parametric family of divergences that includes KL divergence (α=1), Hellinger distance squared (α=1/2), and collision probability (α=2) as special cases. Rényi divergence is used in differential privacy (ε-Rényi differential privacy) and in theoretical analysis of Federated Learning convergence, connecting metric theory to privacy-preserving distributed training.
Key Terminology
-
Metric space: A set X together with a distance function d: X × X → R satisfying non-negativity, identity, symmetry, and triangle inequality. The mathematical structure underlying all properly metrised machine learning.
-
Mahalanobis distance: A metric that accounts for feature covariance by whitening the feature space: d_Σ(x, y) = √((x-y)^T Σ⁻¹ (x-y)). Scale-invariant and rotation-invariant in the eigenbasis of Σ.
-
Cosine distance: 1 - cos(θ) where θ is the angle between two vectors. Properly 1 - (A·B / ‖A‖‖B‖); the complementary angular distance arccos(cos(A,B))/π is a proper metric satisfying the triangle inequality.
-
Curse of dimensionality: The phenomenon in high-dimensional spaces whereby all pairwise distances concentrate around a common value, rendering the notion of “nearest neighbour” statistically meaningless. Affects L2 more severely than L1 or cosine distance.
-
Triangle inequality: d(x, z) ≤ d(x, y) + d(y, z); the axiom that enables metric tree pruning and defines proper metrics. Violated by many practical similarity functions (Cosine Similarity, inner product, KL divergence) but satisfied by their distance-form counterparts.
-
LMNN: Large Margin Nearest Neighbour; a classical metric learning algorithm that learns a Mahalanobis metric to improve k-NN classification by pulling same-class neighbours close and pushing impostors outside a margin.
-
Triplet loss: A deep metric learning objective that learns embeddings by minimising d(anchor, positive) - d(anchor, negative) + margin for anchor-positive-negative triplets.
-
Contrastive loss: A siamese network objective that minimises distance between positive pairs and maximises distance between negative pairs subject to a margin.
-
Pseudo-metric: A distance function satisfying all metric axioms except identity of indiscernibles (allowing d(x, y) = 0 for x ≠ y). Occurs when inputs hash to identical compact representations.
-
Hyperbolic distance: Distance in negatively curved Riemannian space (Poincaré ball model); grows exponentially from the origin, enabling distortion-free embedding of tree-structured hierarchical data.
-
FID (Fréchet Inception Distance): Wasserstein-2 distance between distributions of Inception network features of real and generated images; the standard evaluation metric for Diffusion Models and generative image models.
-
CLIP distance: Cosine Similarity distance in CLIP’s joint image-text embedding space; used to measure semantic alignment and enable cross-modal Nearest Neighbor Search in multimodal retrieval systems.
-
Tanimoto similarity: Jaccard similarity applied to molecular fingerprints: T(A, B) = |A∩B|/|A∪B| for binary fingerprint vectors. The dominant molecular similarity measure in drug discovery and virtual screening.
-
Chamfer distance: Sum of nearest-neighbour Euclidean distances in both directions between two point clouds; used to evaluate 3D shape generation quality and LiDAR scan alignment in Robotics.
-
KL divergence: Non-symmetric information-theoretic divergence measuring expected log-likelihood ratio; foundational to variational inference, Reinforcement Learning policy constraints, and the InfoNCE contrastive learning loss.
-
Product quantisation (PQ): Lossy compression of embedding vectors by independently quantising sub-vectors; used in FAISS IVF+PQ indices to enable billion-scale Nearest Neighbor Search with approximate distance computation.
-
Angular distance: arccos(cos(A,B))/π; a proper metric on the unit sphere derived from cosine similarity, satisfying the triangle inequality and enabling metric tree indexing. Equivalent to Cosine Similarity up to monotonic transformation for ranking purposes.
Distance Metrics in Knowledge Graph and Ontology Systems
Distance metrics play a foundational role in Knowledge Graph Embedding and ontology-based reasoning systems, enabling entity resolution, link prediction, and semantic similarity queries at scale.
In knowledge graph embedding models (TransE, RotatE, DistMult, ComplEx, RESCAL), entities and relations are embedded as vectors in R^d and the plausibility of a triple (head, relation, tail) is scored by a distance or similarity function. TransE models the relation r as a translation in embedding space: score(h, r, t) = -‖h + r - t‖ (L1 or L2 distance), encouraging the tail embedding to be close to the head embedding displaced by the relation vector. Knowledge bases where each entity participates in few relation types are well-modelled by TransE; hierarchical and many-to-many relation structures require more expressive distance-based models.
Ontology alignment — matching entities across different ontologies that use different naming conventions or schema structures — relies on string-based edit distance metrics for lexical matching and embedding-based cosine distance for semantic matching. The OAEI (Ontology Alignment Evaluation Initiative) benchmarks compare alignment systems that use combinations of string metrics (Levenshtein, Jaro-Winkler), structural metrics (shared neighbourhood overlap), and semantic metrics (Cosine Similarity of entity embeddings from pre-trained LLMs) to identify equivalent entities across ontologies. Manchester’s Information Management Group has contributed alignment tools to OAEI using both handcrafted string metrics and learned embedding distances.
The construction of this knowledge graph itself relies on distance metrics at multiple stages: semantic deduplication of candidate entities uses cosine distance over sentence embeddings to identify near-duplicate page topics; cross-reference suggestion uses cosine Nearest Neighbor Search to identify existing pages related to a new concept; and quality assessment of ontology enrichment uses a combination of embedding distance from the predicted semantic class and structural metrics over the wikilink graph.
Distance Metrics in Healthcare and Clinical AI
Healthcare applications of distance metrics in the UK NHS and academic medical centres illustrate the real-world consequences of metric choice for sensitive decision systems.
Clinical record similarity: Electronic health records (EHRs) are heterogeneous: they contain structured data (diagnosis codes, medication lists, lab values), semi-structured data (clinical note text), and unstructured data (radiology images, pathology slides). Computing similarity between patients for cohort identification, trial matching, or case-based reasoning requires metrics over this heterogeneous data. Specialist metrics include: Hamming distance over binary comorbidity vectors (does the patient have condition i?), Euclidean distance over normalised lab value vectors (haemoglobin, creatinine, etc.), cosine distance over clinical note embeddings from BioMedBERT or ClinicalBERT, and feature-weighted Mahalanobis distance over composite patient representations that account for correlations between correlated risk factors.
Imaging similarity: Radiology AI systems at Moorfields Eye Hospital, the Royal Marsden NHS Foundation Trust, and University College London Hospitals use deep metric learning to identify similar cases from historical imaging libraries. Convolutional neural networks trained with triplet or contrastive loss on annotated pathology produce embedding spaces where Euclidean distance over image embeddings correlates with radiological similarity, enabling retrieval-augmented diagnosis support where similar historical cases are presented alongside current patient images.
Drug interaction and pharmacogenomics: The British National Formulary and MHRA yellow card data are embedded in molecular and clinical feature spaces where distance metrics identify structurally similar drugs with potentially shared interaction profiles. The Wellcome Sanger Institute uses molecular fingerprint Tanimoto distance for drug repositioning — identifying approved drugs whose molecular distance from known active compounds is small enough to warrant wet-lab testing for new indications.
Summary: Metric Selection Guide
Choosing the appropriate distance metric for a machine learning application depends on the data type, dimensionality, available supervision, and downstream task:
| Data Type | Recommended Metric | Rationale |
|---|---|---|
| Dense continuous features, low-to-medium dimensionality | Euclidean (L2) or Mahalanobis | Optimal for Gaussian features; Mahalanobis removes feature correlation effects |
| Sparse high-dimensional features (e.g. TF-IDF vectors) | Cosine distance | Magnitude-invariant; robust to document length variation |
| Dense neural embeddings (text, image, audio) | Cosine distance (L2-normalised inner product) | Aligns with contrastive training objectives; hardware-accelerated via BLAS |
| Binary/categorical features | Hamming distance | Counts position mismatches; popcount hardware acceleration |
| String sequences (DNA, text) | Edit (Levenshtein) distance | Handles insertions, deletions, substitutions |
| Molecular fingerprints | Tanimoto (Jaccard) | Standard in cheminformatics; appropriate for binary circular fingerprint vectors |
| Probability distributions | Wasserstein (Earth Mover’s) or KL divergence | Measures distributional overlap; Wasserstein is a proper metric |
| Hierarchical / tree-structured data | Hyperbolic distance (Poincaré ball) | Exponential growth from origin enables low-distortion hierarchy embedding |
| Point clouds in 3D space | Chamfer distance or ICP (Euclidean NNS) | Standard for LiDAR SLAM and 3D shape comparison |
| Any type with labelled similarity supervision | Learned deep metric (contrastive/triplet/ArcFace) | Neural encoder adapts to task-specific similarity; dominates when data is available |
When in doubt and for dense embeddings from modern neural encoders in Semantic Search and Retrieval-Augmented Generation applications, cosine distance (or equivalently, inner product on L2-normalised embeddings) is the appropriate default in 2026 due to its alignment with how these encoders are trained, its hardware efficiency, and its robust performance across a wide range of tasks as evidenced by the BEIR and MTEB benchmarks.
Research and Literature
- Fréchet, M. (1906). “Sur quelques points du calcul fonctionnel.” Rendiconti del Circolo Matematico di Palermo, 22, 1–72. (Formal introduction of metric spaces.)
- Mahalanobis, P.C. (1936). “On the Generalised Distance in Statistics.” Proceedings of the National Institute of Sciences of India, 2(1), 49–55.
- Cover, T., Hart, P. (1967). “Nearest Neighbor Pattern Classification.” IEEE Transactions on Information Theory, 13(1), 21–27.
- Duda, R.O., Hart, P.E. (1973). Pattern Classification and Scene Analysis. Wiley.
- Bellman, R. (1961). Adaptive Control Processes: A Guided Tour. Princeton University Press. (Curse of dimensionality.)
- Xing, E., Jordan, M., Russell, S., Ng, A. (2002). “Distance Metric Learning with Application to Clustering with Side Information.” NeurIPS 2002.
- Weinberger, K.Q., Saul, L.K. (2009). “Distance Metric Learning for Large Margin Nearest Neighbor Classification.” JMLR, 10, 207–244. (LMNN.)
- Chopra, S., Hadsell, R., LeCun, Y. (2005). “Learning a Similarity Metric Discriminatively, with Application to Face Verification.” CVPR 2005. (Contrastive loss.)
- Schroff, F., Kalenichenko, D., Philbin, J. (2015). “FaceNet: A Unified Embedding for Face Recognition and Clustering.” CVPR 2015. arXiv:1503.03832 (Triplet loss.)
- Goldberger, J., Roweis, S., Hinton, G., Salakhutdinov, R. (2005). “Neighbourhood Components Analysis.” NeurIPS 2005.
- Davis, J., Kulis, B., Jain, P., Sra, S., Dhillon, I. (2007). “Information-Theoretic Metric Learning.” ICML 2007. (ITML.)
- Movshovitz-Attias, Y., Toshev, A., Leung, T.K., et al. (2017). “No Fuss Distance Metric Learning using Proxies.” ICCV 2017.
- Deng, J., Guo, J., Xue, N., Zafeiriou, S. (2019). “ArcFace: Additive Angular Margin Loss for Deep Face Recognition.” CVPR 2019. arXiv:1801.07698
- Nickel, M., Kiela, D. (2017). “Poincaré Embeddings for Learning Hierarchical Representations.” NeurIPS 2017. arXiv:1705.08039
- Ganea, O., Bécigneul, G., Hofmann, T. (2018). “Hyperbolic Neural Networks.” NeurIPS 2018. arXiv:1805.09112
- Wang, X., Han, X., Huang, W., Dong, D., Scott, M. (2019). “Multi-Similarity Loss with General Pair Weighting for Deep Metric Learning.” CVPR 2019.
- Kaya, M., Bilge, H.Ş. (2022). “Deep Metric Learning: A Survey.” MDPI Symmetry, 11(9), 1066.
- Bellet, A., Habrard, A., Sebban, M. (2013). “A Survey on Metric Learning for Feature Vectors and Structured Data.” arXiv:1306.6709
- Kulis, B. (2013). “Metric Learning: A Survey.” Foundations and Trends in Machine Learning, 5(4), 287–364.
- Robertson, S.E., Spärck Jones, K. (1994). “Simple, Proven Approaches to Text Retrieval.” Technical Report, Cambridge Computer Lab.
- Indyk, P., Motwani, R. (1998). “Approximate Nearest Neighbors: Towards Removing the Curse of Dimensionality.” STOC 1998. (Locality-Sensitive Hashing.)
- Malkov, Y.A., Yashunin, D.A. (2018). “Efficient and Robust Approximate Nearest Neighbor Search Using Hierarchical Navigable Small World Graphs.” IEEE TPAMI, 42(4), 824–836.
- Johnson, J., Douze, M., Jégou, H. (2021). “Billion-Scale Similarity Search with GPUs.” IEEE Transactions on Big Data, 7(3). (FAISS.) arXiv:1702.08734
- Kusupati, A., et al. (2022). “Matryoshka Representation Learning.” NeurIPS 2022.
- Vyas, A., et al. (2020). “A Survey on Metric Learning for Feature Vectors and Structured Data.” Neurocomputing, 408, 1–25. (Spectral, probabilistic and deep metric learning survey.)
- Kaya, M., Bilge, H.Ş. (2022). “Spectral, Probabilistic, and Deep Metric Learning: Tutorial and Survey.” arXiv:2201.09267
- “A Review on Riemannian Metric Learning: Closer to You than You Imagine.” (2026). arXiv:2503.05321
- “Comparing Contrastive and Triplet Loss: Variance Analysis and Optimization Behavior.” (2025). arXiv:2510.02161
Connections to Probability Theory and Statistics
Distance metrics connect intimately with probability theory and statistical estimation, linking geometric intuitions to probabilistic inference:
Maximum likelihood and Euclidean distance: Minimising mean squared error (MSE) — the average squared Euclidean distance between predictions and targets — is equivalent to maximum likelihood estimation under a Gaussian noise model. This connection explains why Euclidean distance (L2 loss) is the natural error criterion for Gaussian processes, linear regression, and neural networks trained with MSE loss; it encodes the assumption that prediction errors are Gaussian-distributed.
Robust regression and L1 distance: Minimising mean absolute error (MAE) — the average L1 distance between predictions and targets — is equivalent to maximum likelihood under a Laplacian noise model, which has heavier tails than Gaussian and assigns more probability mass to large errors. L1 loss is therefore more robust to outliers than L2 loss, motivating its use in Anomaly Detection and robust regression settings where outlying measurements should not dominate parameter estimates.
Bregman divergences and generalised metrics: Bregman divergences D_φ(x, y) = φ(x) - φ(y) - ∇φ(y)·(x-y) generalise Euclidean distance (φ(x) = ‖x‖²) and KL divergence (φ(x) = x log x) within a unified framework. Every regular exponential family distribution corresponds to a Bregman divergence via its log-partition function, and maximum likelihood estimation for exponential families (including Gaussian, Poisson, Bernoulli, multinomial) minimises the corresponding Bregman divergence between empirical and model distributions. This theoretical unification connects metric-based learning to information-geometric learning and probabilistic graphical models.
Wasserstein distance and optimal transport: The Wasserstein-p distance W_p(μ, ν) = (inf_{γ∈Γ(μ,ν)} E_{(x,y)~γ}[d(x,y)^p])^{1/p} is defined as the minimum expected distance to transport mass from distribution μ to ν over all joint distributions γ with marginals μ and ν. Wasserstein distances are proper metrics on the space of probability measures and satisfy key continuity properties not enjoyed by KL divergence or total variation distance. In GAN theory, the Wasserstein-1 distance serves as the generator loss in WGAN (Arjovsky et al., 2017), providing a more stable training signal than the Jensen-Shannon divergence used in the original GAN formulation.
Cross-References to Related Ontology Pages
The following pages in this knowledge graph are directly related to Distance Metric and should be consulted for adjacent concepts:
- Nearest Neighbor Search — the primary algorithmic application; distance metric choice determines NNS index type, recall, and latency trade-offs
- Cosine Similarity — the most widely deployed distance metric in production AI systems; dominates dense text and multimodal Vector Embedding retrieval
- Clustering — depends fundamentally on distance metric for cluster assignment and within-cluster cohesion measurement; k-means, DBSCAN, and hierarchical clustering are all metric-dependent
- Vector Embedding — the representational substrate over which distance metrics are computed; embedding quality and metric choice jointly determine retrieval quality
- Vector Database — provides indexed storage and distance-metric-based retrieval over embeddings; supports L2, cosine, and inner product natively in all major implementations
- Anomaly Detection — uses Mahalanobis distance and k-NN distance as anomaly scores; distance from class distribution centre is a principled anomaly criterion
- FAISS — Meta AI’s open-source library providing GPU-accelerated distance computation for L2 and inner product at billion scale; the reference implementation for production distance search
- HNSW — the dominant approximate nearest-neighbour index structure; graph-based search that exploits triangle inequality for efficient metric-space traversal
- Locality-Sensitive Hashing — hash-based ANN index with proven sensitivity to cosine and L2 metrics; exploits metric geometry to probabilistically group nearby points
- Representation Learning — produces the embedding spaces over which distance metrics are computed; deep metric learning jointly learns representation and metric
- Contrastive Learning — the training paradigm for deep metric learning; optimises encoder parameters to produce embeddings where cosine or L2 distance aligns with semantic similarity
- Knowledge Graph Embedding — applies distance metrics (TransE L1/L2, RotatE cosine) to score knowledge base triple plausibility; metric choice encodes structural assumptions about the knowledge graph
- Machine Learning — the broad discipline within which distance metrics are foundational; metric-based methods span supervised, unsupervised, and semi-supervised learning
- Dimensionality Reduction — preprocessing step that applies before distance computation to alleviate the curse of dimensionality; PCA, UMAP, and t-SNE are common precursors
- Retrieval-Augmented Generation — production AI application where distance metric (cosine over dense embeddings) determines retrieved context quality for Large Language Models