Metric Spaces
A set plus a rule for distance, so closeness, convergence and continuity make sense far beyond the real line.
Why Does This Exist?
Machine learning compares things constantly: which training point is nearest, which cluster a point belongs to, how far an embedding drifted. Each question needs a notion of distance, and different distances give different answers on the same data.
A metric space is the minimal contract that makes "near" behave. A set plus a function where distances are non-negative, symmetric, zero only for identical points, and satisfy the triangle inequality. Once that holds, limits, continuity and open sets all carry over from the real line.
This page is about the contract itself. Concrete distances live in distance and similarity metrics, and lengths from inner structure live in norms.
The one idea you need first
Convergence means distances shrink to zero: when as real numbers. Every metric question is a question about such distances. The full treatment of limits is in real analysis.
Think of It Like This
Towns and road distances
Towns are points. The road distance between two towns is the metric. It is never negative, the distance from A to B equals B to A, and going A to B directly is never longer than going A to C to B.
That last rule is the triangle inequality, and it is the whole game. It stops absurd maps where a detour beats the direct road. Where the analogy stops: metrics need not come from roads or coordinates; Hamming distance on strings and edit distance on sequences obey the same rules with no map at all.
How It Actually Works
A metric satisfies for all : with equality exactly when ; symmetry ; and .
An open ball is all points within radius . Open sets are unions of balls. A map between metric spaces is continuous when preimages of open sets are open, equivalently when implies .
Worked example
Points , , in the plane with Euclidean distance. Then . Via : and , sum . The inequality holds with slack . Swap to Manhattan distance and still, but the geometry of balls becomes diamonds, which changes who counts as nearest.
Watch Out For
Assuming all distances behave like Euclidean
Symptom: using triangle-inequality reasoning on cosine dissimilarity , which violates it, then wondering why ball queries return inconsistent neighbours. Fix: check the four axioms before trusting geometric intuition; switch to angular distance when a true metric is needed.
Forgetting the metric when claiming convergence
Symptom: "embeddings converged" with no stated distance, while Euclidean drift is small and cosine drift is large. Fix: name the metric and threshold, for example in Euclidean norm, every time a convergence claim is made.
The Quick Version
- A metric space is a set with a distance obeying non-negativity, symmetry and triangle inequality.
- Balls, open sets, convergence and continuity all follow from the metric alone.
- The same points with different metrics have different neighbours and different limits.
- Euclidean, Manhattan and Hamming distances are all metrics; cosine dissimilarity is not.
- Name the metric whenever distances decide an ML outcome.