Skip to content
AI360Xpert
Beta

Metric Spaces

A set plus a rule for distance, so closeness, convergence and continuity make sense far beyond the real line.

A metric turns a set into geometry: the triangle inequality forces the direct distance to be the shortest path
A metric turns a set into geometry: the triangle inequality forces the direct distance to be the shortest path

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 XX plus a function dd 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: xn→xx_n \to x when d(xn,x)→0d(x_n, x) \to 0 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 d:X×X→[0,∞)d: X \times X \to [0, \infty) satisfies for all x,y,zx, y, z: d(x,y)≥0d(x,y) \ge 0 with equality exactly when x=yx = y; symmetry d(x,y)=d(y,x)d(x,y) = d(y,x); and d(x,z)≤d(x,y)+d(y,z)d(x,z) \le d(x,y) + d(y,z).

An open ball B(x,r)={y:d(x,y)<r}B(x, r) = \{y : d(x,y) < r\} is all points within radius rr. Open sets are unions of balls. A map ff between metric spaces is continuous when preimages of open sets are open, equivalently when xn→xx_n \to x implies f(xn)→f(x)f(x_n) \to f(x).

Worked example

Points A=(0,0)A = (0,0), B=(4,0)B = (4,0), C=(2,1.5)C = (2,1.5) in the plane with Euclidean distance. Then d(A,B)=4.0d(A,B) = 4.0. Via CC: d(A,C)=4+2.25=2.5d(A,C) = \sqrt{4 + 2.25} = 2.5 and d(C,B)=2.5d(C,B) = 2.5, sum 5.0≥4.05.0 \ge 4.0. The inequality holds with slack 1.01.0. Swap to Manhattan distance and d(A,B)=4d(A,B) = 4 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 1−cos⁡1 - \cos, 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 d<0.01d < 0.01 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.