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
A metric space is a pair , where is a set and is a function that, for all , satisfies
- Non-negativity and identity: , and exactly when .
- Symmetry: .
- Triangle inequality: .
Here is the set of points, and is the number the rule assigns to the pair and . 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 around a point is the set of points closer than to :
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 , the absolute value (the size, ignoring sign) of the difference. The triangle inequality follows from with and .
- The plane, Euclidean: , the straight-line distance, from the Pythagorean theorem.
- The plane, taxicab: , the number of grid blocks walked along a city street grid.
- Any set, discrete: when , and . Every pair of different points is the same distance apart.
Subsets inherit the metric
If is a metric space and , then with the same rule restricted to its pairs is also a metric space. The three axioms only mention pairs of points, and every pair from is also a pair from , 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
Let and set when , and when .
- Identity: , and because . The distance is zero only between equal points.
- Symmetry: and . The rule treats both orders the same way.
- Triangle inequality, case of three different points: , and , so .
- Triangle inequality, case where two points coincide: if , the inequality reads , which is true.
Interpretation: all three axioms hold, so this is a metric space. Its open balls of radius contain only their centre, so every point is isolated from the others.
Take and in the plane.
- Coordinate differences: and .
- Euclidean distance: .
- Taxicab distance: .
- Triangle check with the origin under : and . Their sum, about , is at least , 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.
Binary words of length 5 are compared by Hamming distance: the number of positions where two words differ. Take , and .
- Compare and position by position: they differ in positions 2 and 4. So .
- Compare and : they differ in positions 1, 2, 4 and 5. So .
- Compare and : they differ in positions 1 and 5, and agree in positions 2, 3 and 4. So .
- Triangle inequality: .
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
| Situation | What goes wrong | Better choice |
|---|---|---|
| A rule such as on the real numbers | The triangle inequality fails: but | Use , the square root of the squared difference |
| You need the size of a vector, not just distances between points | A metric gives distances between points but no built-in length for a single vector | Use a norm, which defines a metric by |
| You need nearness but have no sensible distance | Forcing a metric onto the set can add structure that is not real | Use 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 with a distance that is non-negative, zero only between identical points, symmetric and subject to the triangle inequality. Open balls use 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: and exactly when . Symmetry: . The triangle inequality: .
QuestionComputation: find the Euclidean and taxicab distances between and .Show answer
The coordinate differences are and . The Euclidean distance is . The taxicab distance is .
QuestionWhy: suppose a rule satisfies symmetry and the triangle inequality, and , but two different points can have distance zero. What breaks?Show answer
Limits stop being unique. If with , then any sequence that converges to is also within every distance of , so it converges to 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 on the real numbers a metric? Give a counterexample if not.Show answer
No. Take , and . Then , while . The triangle inequality fails. Taking the square root of this rule gives , which is the ordinary metric on the real line.