Foundations to Frontier
Tests
A. Mathematical Foundations › A3 Calculus and Analysis › Real and functional analysis

Metric Spaces, Completeness and Compactness

A metric is any sensible notion of distance: road distance, edit distance between strings, the gap between two value functions. Completeness means sequences that ought to converge actually do, which makes Banach's fixed-point theorem (and with it value iteration in RL) work. Compactness means every sequence has a convergent subsequence, which guarantees that minimizers exist, and covering numbers measure how complex a set is.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Many ways to measure “how far”

A bird flies straight from home to school. A taxi has to follow the street grid. A chess king moves one square in any direction, so for the king, “distance” is the number of moves. All three are reasonable distances, and they give different numbers.

Distance doesn’t have to be about places. How far apart are the words “robot” and “rocket”? Count the letter edits needed to turn one into the other. Any rule like this that behaves sensibly is called a metric.

“Sensible” means three rules: you are 0 away from yourself (and only from yourself), the distance from A to B equals the distance from B to A, and a detour is never shorter than going direct.

Distances and sequences: unit balls, and Cauchy or not?
p = 2.00
AB
d(A, B) = 1.4142
d(A, 0) + d(0, B) = 2.0000
✅ triangle inequality holds here
Dashed: p = 1 (diamond, Manhattan), p = 2 (circle), p = ∞ (square, Chebyshev). For p ≥ 1 the ball is convex, which is the same as the triangle inequality holding. For p < 1 it caves in (a star shape) and the triangle inequality breaks. Sparse learning still uses these shapes as penalties, because their corners favour zeros.
Why engineers care

A robot planner needs a distance between robot poses. A spell-checker needs one between words. A reinforcement-learning agent needs one between value tables. The right metric makes algorithms provably converge, and the wrong one can quietly break them.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Taxi distance from (0,0) to (3,4) on a grid?
  2. 2. The bird's straight-line distance for the same trip?
  3. 3. Can a detour through C be shorter than going direct from A to B?

Level 1: Beginner

★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.

The axioms and the usual examples

A metric space (X,d)(X,d) is a set with a function dd such that, for all x,y,zx,y,z:

  1. d(x,y)≥0d(x,y)\ge0, and d(x,y)=0d(x,y) = 0 exactly when x=yx = y;
  2. d(x,y)=d(y,x)d(x,y) = d(y,x);
  3. d(x,z)≤d(x,y)+d(y,z)d(x,z)\le d(x,y) + d(y,z) (the triangle inequality).
Metric Formula Unit ball in the plane
Euclidean ℓ2\ell_2 ∑(xi−yi)2\sqrt{\textstyle\sum(x_i - y_i)^2} circle
Manhattan ℓ1\ell_1 ∑∣xi−yi∣\sum\lvert x_i - y_i\rvert diamond
Chebyshev ℓ∞\ell_\infty max⁡∣xi−yi∣\max\lvert x_i - y_i\rvert square
discrete 0 if equal, else 1 a single point
Hamming (strings) number of differing positions
edit (Levenshtein) fewest insert/delete/substitute operations

Not metrics:

  • squared Euclidean distance (the triangle inequality fails);
  • cosine “distance” 1−cos⁡θ1 - \cos\theta (parallel vectors get distance 0, and the triangle inequality can fail);
  • the ℓp\ell_p formula with p<1p < 1 (the triangle inequality fails, as the demo shows).
📝 Quick check (Level 1)
0/3 answered
  1. 1. Chebyshev distance from (1,2) to (4,−2)?
  2. 2. Hamming distance between 10110 and 11100?
  3. 3. Is squared Euclidean distance a metric?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

Convergence, Cauchy sequences, completeness

A sequence converges to xx if d(xn,x)→0d(x_n, x)\to0. It is Cauchy if its terms get close to each other: for every ε>0\varepsilon > 0 there is an NN such that d(xm,xn)<εd(x_m, x_n) < \varepsilon for all m,n≥Nm,n\ge N. The Cauchy test doesn’t need to know the limit in advance, which makes it the practical one.

Every convergent sequence is Cauchy. The converse can fail. Newton’s iterates 1,32,1712,577408,…1, \tfrac32, \tfrac{17}{12}, \tfrac{577}{408}, \dots for 2\sqrt2 are all rational and Cauchy, but 2∉Q\sqrt2\notin\mathbb Q. A space where every Cauchy sequence converges is complete:

  • complete: Rn\mathbb R^n with any norm, closed subsets of complete spaces, and the continuous functions on [a,b][a,b] with the sup metric (A3.5-2);
  • not complete: Q\mathbb Q, the interval (0,1](0,1], and polynomials under the sup metric.

Beware the trap of steps shrinking to zero. For Hn=∑k≤n1/kH_n = \sum_{k\le n}1/k, each step Hn+1−Hn→0H_{n+1} - H_n\to0, yet H4N−HN→ln⁡4H_{4N} - H_N\to\ln4. That’s not Cauchy, and the sequence diverges.

Open, closed, compact

  • The open ball is B(x,r)={y:d(x,y)<r}B(x,r) = \{y: d(x,y) < r\}. A set is open if each of its points has a ball inside the set. It is closed if it contains the limits of its convergent sequences.
  • A set is compact if every sequence in it has a subsequence converging in the set. In Rn\mathbb R^n, that’s exactly the closed and bounded sets (Heine–Borel).
  • Why it matters: a continuous function on a compact set attains its maximum and minimum (the extreme value theorem). That’s how you know an optimal policy, a best fit, or a minimum-energy configuration exists before you try to compute it.
🐍 Exercise 1: which 'distances' are really metrics?
Implement Euclidean, Manhattan, Chebyshev, ℓₚ, discrete, Hamming, edit and cosine distances, and an axiom checker. Find out which pass, and which axiom each failure breaks.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Every convergent sequence is Cauchy. The converse holds in…
  2. 2. Is (0, 1] complete with |x − y|?
  3. 3. Compact subsets of ℝⁿ are exactly the…

Level 3: Advanced

★★★★☆ Advanced⏱ 45 minGraduate level, with full derivations and proofs.

Banach’s fixed-point theorem

Theorem. Let (X,d)(X,d) be complete and T:X→XT: X\to X a contraction: d(Tx,Ty)≤q d(x,y)d(Tx, Ty)\le q\,d(x,y) with q<1q < 1. Then TT has exactly one fixed point x∗x^*. The iterates xk+1=Txkx_{k+1} = Tx_k converge to it from any start, with d(xk,x∗)≤qk1−qd(x1,x0)d(x_k, x^*)\le\frac{q^k}{1-q}d(x_1, x_0).

Proof.

  1. Steps shrink geometrically. d(xk+1,xk)≤qkd(x1,x0)d(x_{k+1},x_k)\le q^kd(x_1,x_0).
  2. The iterates are Cauchy. For m>nm > n, the triangle inequality and a geometric series give d(xm,xn)≤∑k=nm−1qkd1≤qn1−qd1→0d(x_m,x_n)\le\sum_{k=n}^{m-1}q^kd_1\le\frac{q^n}{1-q}d_1\to0.
  3. A limit exists. By completeness, xk→x∗x_k\to x^*.
  4. It’s a fixed point. TT is continuous (Lipschitz), so Tx∗=lim⁡Txk=lim⁡xk+1=x∗Tx^* = \lim Tx_k = \lim x_{k+1} = x^*.
  5. It’s unique. If Ty=yTy = y, then d(x∗,y)=d(Tx∗,Ty)≤q d(x∗,y)d(x^*,y) = d(Tx^*,Ty)\le q\,d(x^*,y), which forces d=0d = 0.

Letting m→∞m\to\infty in step 2 gives the error bound. ∎

Completeness is essential. On Q\mathbb Q, the contraction x↦x/2+1/xx\mapsto x/2 + 1/x (near 2\sqrt2) has no fixed point.

Bolzano–Weierstrass

Every bounded sequence in R\mathbb R has a convergent subsequence. Proof by bisection: one half of the interval contains infinitely many terms, so keep that half and repeat. The nested intervals shrink to a point, and choosing one term from each with increasing indices gives a Cauchy subsequence. By completeness of R\mathbb R, it converges (Practice Code 4). Extending this coordinate by coordinate proves Heine–Borel in Rn\mathbb R^n.

In infinite dimensions, closed and bounded is not enough

In ℓ2\ell^2, the unit vectors e1,e2,…\mathbf e_1,\mathbf e_2,\dots are bounded, but d(ei,ej)=2d(\mathbf e_i,\mathbf e_j) = \sqrt2 for all i≠ji\ne j. No subsequence is Cauchy. The right characterization is compact ⇔ complete and totally bounded, where totally bounded means a finite ε\varepsilon-net exists for every ε\varepsilon. This is why function spaces need extra conditions, such as equicontinuity in Arzelà–Ascoli, to guarantee compactness.

Exercise 3.1: Prove that every convergent sequence is Cauchy

Suppose xn→xx_n\to x and ε>0\varepsilon > 0. Pick NN with d(xn,x)<ε/2d(x_n,x) < \varepsilon/2 for n≥Nn\ge N. Then for m,n≥Nm,n\ge N, d(xm,xn)≤d(xm,x)+d(x,xn)<εd(x_m,x_n)\le d(x_m,x) + d(x,x_n) < \varepsilon. ∎

📝 Quick check (Level 3)
0/3 answered
  1. 1. Banach's theorem needs the space to be…
  2. 2. The a-priori error after k steps is at most…
  3. 3. The unit vectors in ℓ² have a convergent subsequence?

Level 4: Expert

★★★★★ Expert⏱ 45 minResearchers: original papers and open problems.

Contractions in reinforcement learning

The Bellman optimality operator (TV)(s)=max⁡a[R(s,a)+γ∑s′P(s′∣s,a)V(s′)](TV)(s) = \max_a\big[R(s,a) + \gamma\sum_{s'}P(s'|s,a)V(s')\big] is a γ\gamma-contraction in the sup norm: ∥TU−TV∥∞≤γ∥U−V∥∞\|TU - TV\|_\infty\le\gamma\|U - V\|_\infty, because a max of averages can’t move further than the inputs. The space of bounded value functions is complete. So Banach’s theorem says the optimal value function V∗V^* exists and is unique, and value iteration converges to it at rate γk\gamma^k (Bellman, 1957; Puterman, 1994; Bertsekas, 2012). Policy evaluation, Q-learning’s convergence proofs and many TD analyses rest on the same fact.

🐍 Exercise 2 (research): value iteration as Banach's fixed-point theorem
On a random 12-state MDP, verify that the Bellman operator is a γ-contraction in sup norm, run value iteration to its unique fixed point, measure the contraction ratio, and check that Banach's a-priori bound guarantees the requested accuracy.

Metrics everywhere in AI and robotics

  • Metric learning. Siamese and triplet networks learn embeddings where Euclidean distance means semantic similarity (Schroff et al., 2015, FaceNet). Retrieval and place recognition for robot localization use this.
  • Distances between distributions. The Wasserstein metric (optimal transport) metrizes weak convergence and gives Wasserstein GANs their stable gradients (Arjovsky et al., 2017). KL divergence is not a metric: it’s asymmetric and has no triangle inequality.
  • Covering numbers and generalization. If a function class has an ε\varepsilon-net of size N(ε)N(\varepsilon), uniform-convergence bounds scale like log⁡N(ε)/n\sqrt{\log N(\varepsilon)/n} (B1, B2). How fast N(ε)N(\varepsilon) grows is the “metric entropy”, which reveals the intrinsic dimension (Practice Code 5).
  • Robot configuration spaces. Distances on SO(3)SO(3) (rotation angle) and SE(3)SE(3) drive sampling planners like RRT and PRM (LaValle, 2006). Their probabilistic completeness proofs rely on compactness of the free space.
  • Existence of optimal controls. Compactness of the admissible set plus lower semicontinuity of the cost (Weierstrass/Tonelli) guarantees an optimal trajectory exists, as in Filippov’s theorem.

References

  1. W. Rudin (1976). Principles of Mathematical Analysis, 3rd ed. McGraw-Hill.
  2. E. Kreyszig (1978). Introductory Functional Analysis with Applications. Wiley.
  3. S. Banach (1922). “Sur les opérations dans les ensembles abstraits et leur application aux équations intégrales.” Fundamenta Mathematicae, 3.
  4. M. Puterman (1994). Markov Decision Processes. Wiley.
  5. D. Bertsekas (2012). Dynamic Programming and Optimal Control, Vol. II, 4th ed. Athena Scientific.
  6. F. Schroff, D. Kalenichenko, J. Philbin (2015). “FaceNet: a unified embedding for face recognition and clustering.” CVPR.
  7. M. Arjovsky, S. Chintala, L. Bottou (2017). “Wasserstein generative adversarial networks.” ICML.
  8. S. LaValle (2006). Planning Algorithms. Cambridge.
  9. V. Levenshtein (1966). “Binary codes capable of correcting deletions, insertions, and reversals.” Soviet Physics Doklady, 10.