Search

Search papers and lessons

Metric Space

Level 2 · 9 min read

A metric space is a set with a distance function that is non-negative, zero only between identical points, symmetric, and obeys the triangle inequality.

On this page

  • Colour = topic mix
  • Discrete math
  • Sign in to see your progress

TL;DR

A metric space is a set with a rule for the distance between any two of its points. The rule must never be negative, must be zero only between identical points, must treat both directions the same, and must obey the triangle inequality: a detour through a third point is never shorter than the direct route. Once a set has such a distance, ideas such as closeness, limits and continuity make sense on it.

Where you'll see it: in the definitions of limits and continuity, in error measures for machine learning, and in the Wasserstein distance for comparing probability distributions.

Intuition

Distance without a map

On a flat map, distance is the straight-line length between two points. Beyond maps, "distance" can mean many things: the number of single-letter edits needed to turn one word into another (see Edit Distance), the number of blocks you walk in a city grid, or how far apart two signals are. Mathematicians want one vocabulary that covers all of these, so they ask which properties every reasonable notion of distance shares. There are three, and they are the metric axioms.

Distance is how we say "close". A Sequence converges when its terms get closer and closer to a target, and a function is continuous when small changes in the input give small changes in the output (see also Limit of a Function). Both statements need a distance. A metric space provides one on any set, not only on the real line.

flowchart LR
  A[A distance function] --> B[Open balls: what is near a point]
  B --> C[Limits of sequences]
  C --> D[Continuity of functions]

The math

Definition: Metric space

A metric space is a pair (X,d)(X, d), where XX is a set and d:X×X→Rd : X \times X \to \mathbb{R} is a function that, for all x,y,z∈Xx, y, z \in X, satisfies

  1. Non-negativity and identity: d(x,y)≥0d(x, y) \geq 0, and d(x,y)=0d(x, y) = 0 exactly when x=yx = y.
  2. Symmetry: d(x,y)=d(y,x)d(x, y) = d(y, x).
  3. Triangle inequality: d(x,z)≤d(x,y)+d(y,z)d(x, z) \leq d(x, y) + d(y, z).

Here XX is the set of points, and d(x,y)d(x, y) is the number the rule assigns to the pair xx and yy. The metric is part of the definition. The same set with two different distance rules gives two different metric spaces.

Open balls

The open ball of radius r>0r \gt 0 around a point xx is the set of points closer than rr to xx:

B(x,r)={y∈X:d(x,y)<r}.B(x, r) = \left\{ y \in X : d(x, y) \lt r \right\}.

Balls turn the distance into a notion of nearness. A set is called open when every one of its points has a ball around it that stays inside the set.

Standard examples

  • The real line: the set of all real numbers, with d(x,y)=∣x−y∣d(x, y) = |x - y|, the absolute value (the size, ignoring sign) of the difference. The triangle inequality follows from ∣a+b∣≤∣a∣+∣b∣|a + b| \leq |a| + |b| with a=x−ya = x - y and b=y−zb = y - z.
  • The plane, Euclidean: d2((x1,x2),(y1,y2))=(x1−y1)2+(x2−y2)2d_2\big((x_1, x_2), (y_1, y_2)\big) = \sqrt{(x_1 - y_1)^2 + (x_2 - y_2)^2}, the straight-line distance, from the Pythagorean theorem.
  • The plane, taxicab: d1((x1,x2),(y1,y2))=∣x1−y1∣+∣x2−y2∣d_1\big((x_1, x_2), (y_1, y_2)\big) = |x_1 - y_1| + |x_2 - y_2|, the number of grid blocks walked along a city street grid.
  • Any set, discrete: d(x,y)=1d(x, y) = 1 when x≠yx \neq y, and d(x,x)=0d(x, x) = 0. Every pair of different points is the same distance apart.

Subsets inherit the metric

If (X,d)(X, d) is a metric space and A⊆XA \subseteq X, then AA with the same rule dd restricted to its pairs is also a metric space. The three axioms only mention pairs of points, and every pair from AA is also a pair from XX, so all three still hold. This is why the real line, the unit interval and a curve in the plane can all be treated with the same tools.

Worked examples

Example
1. Three points with a discrete distance (tiny)

Let X={a,b,c}X = \{a, b, c\} and set d(x,y)=0d(x, y) = 0 when x=yx = y, and d(x,y)=1d(x, y) = 1 when x≠yx \neq y.

  1. Identity: d(a,a)=0d(a, a) = 0, and d(a,b)=1≠0d(a, b) = 1 \neq 0 because a≠ba \neq b. The distance is zero only between equal points.
  2. Symmetry: d(a,b)=1d(a, b) = 1 and d(b,a)=1d(b, a) = 1. The rule treats both orders the same way.
  3. Triangle inequality, case of three different points: d(a,c)=1d(a, c) = 1, and d(a,b)+d(b,c)=2d(a, b) + d(b, c) = 2, so 1≤21 \leq 2.
  4. Triangle inequality, case where two points coincide: if x=yx = y, the inequality reads d(x,z)≤0+d(x,z)d(x, z) \leq 0 + d(x, z), which is true.

Interpretation: all three axioms hold, so this is a metric space. Its open balls of radius 12\tfrac{1}{2} contain only their centre, so every point is isolated from the others.

Example
2. Two distances between the same two points (standard)

Take p=(1,2)p = (1, 2) and q=(4,6)q = (4, 6) in the plane.

  1. Coordinate differences: ∣1−4∣=3|1 - 4| = 3 and ∣2−6∣=4|2 - 6| = 4.
  2. Euclidean distance: d2(p,q)=32+42=25=5d_2(p, q) = \sqrt{3^2 + 4^2} = \sqrt{25} = 5.
  3. Taxicab distance: d1(p,q)=3+4=7d_1(p, q) = 3 + 4 = 7.
  4. Triangle check with the origin o=(0,0)o = (0, 0) under d2d_2: d2(p,o)=1+4≈2.236d_2(p, o) = \sqrt{1 + 4} \approx 2.236 and d2(o,q)=16+36≈7.211d_2(o, q) = \sqrt{16 + 36} \approx 7.211. Their sum, about 9.4479.447, is at least 55, as the triangle inequality requires. One check does not prove the general case, but the structure of the proof is the same.

Interpretation: the same two points are 5 apart in one metric and 7 apart in another. Both are metric spaces on the plane. Taxicab distance matches walking along a grid of city streets, and Euclidean distance matches flying in a straight line.

Example
3. Error-correcting codewords (realistic)

Binary words of length 5 are compared by Hamming distance: the number of positions where two words differ. Take u=10110u = 10110, v=11100v = 11100 and w=00111w = 00111.

  1. Compare uu and vv position by position: they differ in positions 2 and 4. So d(u,v)=2d(u, v) = 2.
  2. Compare vv and ww: they differ in positions 1, 2, 4 and 5. So d(v,w)=4d(v, w) = 4.
  3. Compare uu and ww: they differ in positions 1 and 5, and agree in positions 2, 3 and 4. So d(u,w)=2d(u, w) = 2.
  4. Triangle inequality: d(u,w)=2≤d(u,v)+d(v,w)=2+4=6d(u, w) = 2 \leq d(u, v) + d(v, w) = 2 + 4 = 6.

Interpretation: the smallest distance between any two of these codewords is 2. A received word with one flipped bit is then detected, but it may sit at distance 1 from two codewords, so it cannot be corrected with confidence. A code whose smallest distance is 3 can correct any single flipped bit, because the corrupted word is still closest to the word that was sent.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
A rule such as d(x,y)=(x−y)2d(x, y) = (x - y)^2 on the real numbersThe triangle inequality fails: d(0,2)=4d(0, 2) = 4 but d(0,1)+d(1,2)=2d(0, 1) + d(1, 2) = 2Use ∣x−y∣\lvert x - y \rvert, the square root of the squared difference
You need the size of a vector, not just distances between pointsA metric gives distances between points but no built-in length for a single vectorUse a norm, which defines a metric by d(x,y)=∥x−y∥d(x, y) = \lVert x - y \rVert
You need nearness but have no sensible distanceForcing a metric onto the set can add structure that is not realUse a topological space, which keeps only the open sets

Dropping an axiom gives a related but different structure. Without the identity condition, two different points may have distance zero, and a sequence can then converge to two different limits, so limits stop being unique. Dropping symmetry gives an asymmetric notion of distance, which is useful for one-way costs such as travel with one-way streets.

History

Maurice Fréchet introduced the idea in the early 1900s, while studying spaces of functions. Felix Hausdorff gave the name "metric space" in 1914. The axioms caught on because they are the least structure that lets analysts prove the basic facts about limits and continuity. Later work generalised them to topological spaces, which keep the notion of nearness and drop the numerical distance.

Recap

A metric space is a set XX with a distance dd that is non-negative, zero only between identical points, symmetric and subject to the triangle inequality. Open balls use dd to say what is near a point, and that is the basis for limits and Continuity. Any subset inherits the distance. Examples run from the real line with absolute difference to codewords with Hamming distance. The idea leads to norms, which build metrics from lengths, to the Wasserstein distance and Optimal Transport, which measure how far apart two probability distributions are, and to the distance form of the Jaccard Index.

Check yourself

QuestionRecall: name the three metric axioms.Show answer

Non-negativity, with the distance zero only between identical points: d(x,y)≥0d(x, y) \geq 0 and d(x,y)=0d(x, y) = 0 exactly when x=yx = y. Symmetry: d(x,y)=d(y,x)d(x, y) = d(y, x). The triangle inequality: d(x,z)≤d(x,y)+d(y,z)d(x, z) \leq d(x, y) + d(y, z).

QuestionComputation: find the Euclidean and taxicab distances between (2,1)(2, 1) and (5,5)(5, 5).Show answer

The coordinate differences are 33 and 44. The Euclidean distance is 32+42=25=5\sqrt{3^2 + 4^2} = \sqrt{25} = 5. The taxicab distance is 3+4=73 + 4 = 7.

QuestionWhy: suppose a rule satisfies symmetry and the triangle inequality, and d(x,x)=0d(x, x) = 0, but two different points can have distance zero. What breaks?Show answer

Limits stop being unique. If d(x,y)=0d(x, y) = 0 with x≠yx \neq y, then any sequence that converges to xx is also within every distance of yy, so it converges to yy as well. A notion of limit is only well defined when the limit is unique, and that is why the identity condition is needed.

QuestionApplied: is the rule d(x,y)=(x−y)2d(x, y) = (x - y)^2 on the real numbers a metric? Give a counterexample if not.Show answer

No. Take x=0x = 0, y=1y = 1 and z=2z = 2. Then d(0,2)=4d(0, 2) = 4, while d(0,1)+d(1,2)=1+1=2d(0, 1) + d(1, 2) = 1 + 1 = 2. The triangle inequality fails. Taking the square root of this rule gives ∣x−y∣|x - y|, which is the ordinary metric on the real line.

Sources