1 Reading ¶ Material related to this page, as well as additional exercises, can be found in VMLS 3.2.
2 Learning Objectives ¶ By the end of this page, you should know:
the Euclidean distance between two vectors the properties of a general distance function 3 The Euclidean Distance ¶ A distance function, or metric, describes how far apart 2 points are.
A familiar starting point for our study of distances will be the Euclidean distance, which is closely related to the Euclidean norm on R n \mathbb R^n R n :
Definition 1 (The Euclidean Distance)
For vectors u , v ∈ R n \vv u, \vv v \in \mathbb{R}^n u , v ∈ R n , the Euclidean distance is defined as the Euclidean norm of their difference u − v \vv u - \vv v u − v . In other words,
dist ( u , v ) = ∥ u − v ∥ = ⟨ u − v , u − v ⟩ \begin{align*}
\text{dist}(\vv u, \vv v) = \| \vv u - \vv v\| = \sqrt{\langle \vv u - \vv v, \vv u - \vv v \rangle}
\end{align*} dist ( u , v ) = ∥ u − v ∥ = ⟨ u − v , u − v ⟩ Note that this is measuring the length of the arrow drawn from point x \vv x x to point y \vv y y :
3.1 Python break! ¶ We use the np.linalg.norm
function in Python to compute the Eucledian distance between two vectors by computing the norm of the difference between the vectors.
# Distance between vectors
import numpy as np
v1 = np.array([1, 2])
v2 = np.array([3, 4])
euc_dist = np.linalg.norm(v1 - v2)
print("Eucledian distance: ", euc_dist)
Eucledian distance: 2.8284271247461903
4 General Distances ¶ In this course, we will only work with the Euclidean distance. However, given any vector space with a general norm (i.e., R n \mathbb{R}^n R n with the Euclidean norm), we may construct a distance function as the norm of their difference. This leads us to a more general notion of distances:
Definition 2 (General Distances)
For a set S S S , a function d : S × S → R d : S \times S \to \mathbb R d : S × S → R is a distance function, or metric, if it satisfies the following:
Symmetry. For all x , y ∈ S x, y \in S x , y ∈ S ,d ( x , y ) = d ( y , x ) \begin{align*}
d(x, y) = d(y, x)
\end{align*} d ( x , y ) = d ( y , x ) Positivity. For all x , y ∈ S x, y \in S x , y ∈ S ,d ( x , y ) ≥ 0 \begin{align*}
d(x, y) \geq 0
\end{align*} d ( x , y ) ≥ 0 and d ( x , y ) = 0 d(x, y) = 0 d ( x , y ) = 0 if and only if x = y x = y x = y .
Triangular Inequality. For all x , y , z ∈ S x, y, z \in S x , y , z ∈ S ,d ( x , z ) ≤ d ( x , y ) + d ( y , z ) \begin{align*}
d(x, z) \leq d(x, y) + d(y, z)
\end{align*} d ( x , z ) ≤ d ( x , y ) + d ( y , z ) Try to convince yourself why the Euclidean distance fits this definition.
When the distance ∥ x − y ∥ \| \vv x - \vv y \| ∥ x − y ∥ between two vectors x , y ∈ V \vv x, \vv y \in V x , y ∈ V is small, we say they are “close.” If the distance between ∥ x − y ∥ \| \vv x - \vv y \| ∥ x − y ∥ is large, we say they are “far.” What constitutes close or far is typically application dependent.
Note that one vector space can admit many distance functions. From here on, unless otherwise mentioned, we will only be considering the Euclidean distance .
Example 1 (Matrix Norms and their Induced Distances)
Let M ∈ R n × n M \in \mathbb{R}^{n \times n} M ∈ R n × n be a symmetric square matrix such that x T M x > 0 x^T M x > 0 x T M x > 0 for all nonzero x ∈ R n x\in \mathbb{R}^n x ∈ R n . Such a matrix is called positive definite ; some equivalent definitions of positive definite matrices are symmetric matrices which may be decomposed as A T A A^TA A T A , where A A A is an invertible square matrix, or symmetric matrices with all strictly positive eigenvalues. A familiar positive definite matrix is the identity matrix, I n I_n I n .
Then, M M M induces an inner product given by ⟨ u , v ⟩ M = u T M v \langle \vv u, \vv v \rangle_M = \vv u^T M \vv v ⟨ u , v ⟩ M = u T M v . In the case that M M M is diagonal, this is the weighted dot product we have seen before. Try for yourselves to verify that ⟨ u , v ⟩ M \langle \vv u, \vv v \rangle_M ⟨ u , v ⟩ M indeed satisfies all axioms of an inner product.
The inner product in turn induces a norm ∥ v ∥ M = ⟨ v , v ⟩ M = v T M v \|\vv v\|_M = \sqrt{\langle \vv v, \vv v\rangle}_M = \sqrt{\vv v^T M \vv v} ∥ v ∥ M = ⟨ v , v ⟩ M = v T M v .
TODO: Probably want to move this to the norms lesson
Example 2 (Distances on a Connected Graph)
In this example, we’ll demonstrate how the definition of a distance function can be satisfied when the underlying space isn’t a vector space. We will consider the shortest walk distance on a connected undirected graph .
An undirected graph consists of a set of vertices and edges which connect 2 vertices. Often times, undirected graphs are drawn as follows: the vertices are dots, and edges are lines connecting 2 dots. So we can represent an example of an undirected graph with the image below. (For our purposes, we will assume that each pair of vertices can have at most 1 edge connecting them, and that no vertex has an edge to itself.)
A walk in an undirected graph is a sequence of vertices v 1 , v 2 , . . . , v k − 1 , v k v_1, v_2, ..., v_{k - 1}, v_k v 1 , v 2 , ... , v k − 1 , v k such that there is an edge between adjacent vertices in this sequence. The number of edges in the walk is its length . In the image, for example, 3 → 5 → 6 → 10 3 \to 5\to 6\to 10 3 → 5 → 6 → 10 is a walk with length 3.
We say an undirected graph is connected if there is at least one walk between every pair of vertices. The above graph is connected.
If a graph is connected, then we can define the shortest walk distance as follows. For vertices u , v u, v u , v in our graph, their shortest walk distance is defined as
dist ( u , v ) = length of shortest walk starting at u and ending at v \begin{align*}
\text{dist}(u, v) = \text{length of shortest walk starting at $u$ and ending at $v$}
\end{align*} dist ( u , v ) = length of shortest walk starting at u and ending at v We will verify that the shortest walk distance indeed satisfies the three axioms of a distance function.
Symmetry. For any two vertices u , v u, v u , v , let P = u → . . . → v P = u \to ... \to v P = u → ... → v be a minimum length walk (with length l l l ) starting at u u u and ending at v v v . If we reverse P P P , we get a walk starting at v v v and ending at u u u with length l l l . This means that d ( v , u ) ≤ l = d ( u , v ) d(v, u) \leq l = d(u, v) d ( v , u ) ≤ l = d ( u , v ) .
Next, let Q = v → . . . → u Q = v\to ...\to u Q = v → ... → u be a minimum length walk starting at v v v and ending at u u u (with length l ′ l' l ′ ). If we reverse Q Q Q , we get a walk starting at u u u and ending at v v v with length l ′ l' l ′ . This means that d ( u , v ) ≤ l ′ = d ( v , u ) d(u, v) \leq l' = d(v, u) d ( u , v ) ≤ l ′ = d ( v , u ) .
Taken together, these two inequalities imply that d ( u , v ) = d ( v , u ) d(u, v) = d(v, u) d ( u , v ) = d ( v , u ) , i.e., the shortest walk distance is symmetric.
Positivity. For vertices u ≠ v u \neq v u = v , it will take at least one edge to go from u u u to v v v , implying that d ( u , v ) > 0 d(u, v) > 0 d ( u , v ) > 0 if u ≠ v u \neq v u = v . Also, d ( v , v ) = 0 d(v, v) = 0 d ( v , v ) = 0 because we can take the trivial walk P = v P = v P = v , which has no edges.
Triangular Inequality. For vertices u , v , w u, v, w u , v , w , we want to show that
d ( u , w ) ≤ d ( u , v ) + d ( v , w ) \begin{align*}
d(u, w) \leq d(u, v) + d(v, w)
\end{align*} d ( u , w ) ≤ d ( u , v ) + d ( v , w ) Note that if P u v P_{uv} P uv is a walk of length d ( u , v ) d(u, v) d ( u , v ) from u u u to v v v , and P v w P_{vw} P v w is a walk of length d ( v , w ) d(v, w) d ( v , w ) from v v v to w w w , then we can concatenate P u v → P v w P_{uv} \to P_{vw} P uv → P v w to get a walk starting from u u u which ends at w w w and has length d ( u , v ) + d ( v , w ) d(u, v) + d(v, w) d ( u , v ) + d ( v , w ) . Since the shortest walk from u u u to w w w can’t be longer than this walk we just constructed, this implies that d ( u , w ) ≤ d ( u , v ) + d ( v , w ) d(u, w) \leq d(u, v) + d(v, w) d ( u , w ) ≤ d ( u , v ) + d ( v , w ) , i.e., the triangular inequality holds.