Contents

Common Distance Measures

Cover image generated by ChatGPT.

Introduction

How to calculate distance is an important aspect of modern statistical analysis. Choosing different distance metrics may lead to different results in statistical analysis methods. The following introduces commonly used distance metrics.

Euclidean Distance

Euclidean Distance, also known as $L_2$ distance, is one of the most common distance metrics. It refers to the straight-line distance between two points.

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Common%20Distance%20Measures/Euclidean%20Distance.jpg
Euclidean Distance.

As shown in the figure, suppose

$$ \begin{align*} & x=(x_1 , \cdots , x_n); \\ & y=(y_1 , \cdots , y_n), \end{align*} $$

the distance between the two points $d(x, y)$ can be expressed as

$$ \begin{align*} d(x,y) & = \sqrt{(x_1-y_1)^2 +(x_2-y_2)^2 + \cdots +(x_n-y_n)^2} \\ & = \sqrt{\sum_{i = 1}^n (x_i - y_i)^2}. \end{align*} $$

We can also compute the vector from the origin $(0, \cdots, 0)$ to the point $x$, as shown in the figure below:

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Common%20Distance%20Measures/Euclidean%20Distance%20vector.jpg
Vector from the origin to point $x$.

Here, the vector representation of $x$ is denoted as $\overrightarrow{x}$. The length of this vector, known as the norm or the $L_2$ norm, is denoted as $\|x\|_2$, and is calculated as follows:

$$ \|\vec{x}\|_2 = \sqrt{|x_1|^2 + \cdots + |x_n|^2} $$

Example

Suppose there exist two points

$$ \begin{align*} & x=(1, 2, 3); \\ & y=(1, 0, 6). \end{align*} $$

Then their Euclidean distance is

$$ \sqrt{(1 - 1)^2 + (2 - 0)^2 + (3 - 6)^2} = \sqrt{13} \approx 3.61 $$

Manhattan Distance

Manhattan Distance, also known as City Block Distance or $L_1$ distance, refers to the total distance traveled only along the coordinate axes. It was first used to describe the shortest driving path in cities with large square blocks, such as Manhattan in New York City, USA.

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Common%20Distance%20Measures/Manhattan%20Distance.jpg
Manhattan Distance.

As shown in the figure above, the red, blue, and green lines all represent the Manhattan distance between two points, and all paths have the same length.

Suppose there exist two points

$$ \begin{align*} & x=(x_1, \cdots, x_n); \\ & y=(y_1, \cdots, y_n), \end{align*} $$

the distance between the two points $d(x, y)$ can be expressed as

$$ \begin{align*} d(x, y) & = |x_1 - y_1| + |x_2 - y_2| + \cdots + |x_n - y_n| \\ & = \sum_{i = 1}^n |x_i - y_i|. \end{align*} $$

Example

Suppose there exist two points

$$ \begin{align*} & x=(1, 2, 3); \\ & y=(1, 0, 6). \end{align*} $$

Then their Manhattan distance is

$$ |1 - 1| + |2 - 0| + |3 - 6| = 5 $$

Chebyshev Distance

Chebyshev Distance, also known as Chessboard Distance or $L_\infty$ distance, refers to the maximum difference between two points along any dimension.

abcdefgh
8
a8 five
b8 four
c8 three
d8 two
e8 two
f8 two
g8 two
h8 two
a7 five
b7 four
c7 three
d7 two
e7 one
f7 one
g7 one
h7 two
a6 five
b6 four
c6 three
d6 two
e6 one
f6 white king
g6 one
h6 two
a5 five
b5 four
c5 three
d5 two
e5 one
f5 one
g5 one
h5 two
a4 five
b4 four
c4 three
d4 two
e4 two
f4 two
g4 two
h4 two
a3 five
b3 four
c3 three
d3 three
e3 three
f3 three
g3 three
h3 three
a2 five
b2 four
c2 four
d2 four
e2 four
f2 four
g2 four
h2 four
a1 five
b1 five
c1 five
d1 five
e1 five
f1 five
g1 five
h1 five
8
77
66
55
44
33
22
11
abcdefgh
Chessboard, retrieved from Wikipedia on June 27, 2025.

As shown in the figure above, defining the edge length of each square as 1, for any given square—such as the king (♔) in the image—its eight neighboring squares have a distance of 1, the next surrounding ring has a distance of 2, and so on.

Suppose there exist two points

$$ \begin{align*} & x=(x_1, \cdots, x_n); \\ & y=(y_1, \cdots, y_n). \end{align*} $$

the distance between the two points $d(x, y)$ can be expressed as

$$ \begin{align*} d(x, y) & = \max(|x_1 - y_1|, |x_2 - y_2|, \cdots, |x_n - y_n|) \\ & = \max_i(|x_i - y_i|). \end{align*} $$

The Chebyshev distance is actually the limiting form of the $L_k$ norm as $k \to \infty$. The definition of the $L_k$ norm is as follows:

$$ \|w\|_k = \left( \sum_{i=1}^n |w_i|^k \right)^{1/k}. $$

Suppose there exist two points

$$ \begin{align*} & x=(x_1, \cdots, x_n); \\ & y=(y_1, \cdots, y_n). \end{align*} $$

The distance vector between the two points can be expressed as

$$ \left( \sum_{i=1}^n |x_i - y_i|^k \right)^{1/k}. $$

As $k \to \infty$, we obtain the distance between the two points as

$$ \begin{align*} d(x, y) & = \lim_{k \to \infty} \left( \sum_{i=1}^n |x_i - y_i|^k \right)^{1/k} \\ & = \max_i(|x_i - y_i|), \end{align*} $$

which is the Chebyshev distance.

The proof of this limit is as follows:

Proof

設 $a_i = |x_i - y_i|$ ,令 $a = \max_i \{a_i\}$ ,則:

  • Upper bound:

    $$ \sum_{i=1}^n a_i^k \le n \cdot a^k \quad \Rightarrow \quad \left( \sum_{i=1}^n a_i^k \right)^{1/k} \le a \cdot n^{1/k} $$

  • Lower bound:

    $$ \sum_{i=1}^n a_i^k \ge a^k \quad \Rightarrow \quad \left( \sum_{i=1}^n a_i^k \right)^{1/k} \ge a $$

By Squeeze Theorem, we have:

$$ \begin{align*} & \qquad a \le \left( \sum_{i=1}^n a_i^k \right)^{1/k} \le a \cdot n^{1/k} \\ & \Rightarrow \lim_{k \to \infty} \left( \sum_{i=1}^n a_i^k \right)^{1/k} = a = \max_i \{a_i\}, && \text{when } \lim_{k \to \infty} n^{1/k} = 1. \end{align*} $$

Therefore, as $k \to \infty$, the limit of the $L_k$ norm is exactly the Chebyshev distance, that is:

$$ d(x, y) = \max_i |x_i - y_i|. $$

Note
As $k$ increases, only the largest term $|x_i - y_i|^k$ has a decisive impact on the sum, while the other terms tend to zero relatively, so the result approaches the absolute value of the maximum term.
Example

Suppose there exist two points

$$ \begin{align*} & x=(1, 2, 3); \\ & y=(1, 0, 6). \end{align*} $$

Then their Chebyshev distance is

$$ \max \left( |1 - 1| + |2 - 0| + |3 - 6| \right) = 3 $$

Minkowski Distance

Minkowski Distance, also known as the $L_k$ distance, is a generalized form of both Euclidean distance and Manhattan distance.

Suppose there exist two points

$$ \begin{align*} & x=(x_1, \cdots, x_n); \\ & y=(y_1, \cdots, y_n). \end{align*} $$

The distance between two points can be expressed as

$$ d(x, y) = \left( \sum_{i=1}^n |x_i - y_i|^k \right)^{1/k}. $$

We can also observe the following points:

  • When $k \to 1$, the $L_k$ norm degenerates to the Manhattan Distance ($L_1$ distance)

    $$ \| x - y \|_1 = \sum_{i=1}^n |x_i - y_i|. $$

  • When $k \to 2$, the $L_k$ norm degenerates to the Euclidean Distance ($L_2$ distance)

    $$ \| x - y \|_2 = \sqrt{\sum_{i=1}^n (x_i - y_i)^2}. $$

  • When $k \to \infty$, the $L_k$ norm becomes the Chebyshev Distance ($L_\infty$ distance)

    $$ \| x - y \|_\infty = \max_i(|x_i - y_i|). $$

Example

Suppose there exist two points

$$ \begin{align*} & x=(1, 2, 3); \\ & y=(1, 0, 6). \end{align*} $$

Then their Minkowski distance is

$$ \left( |1 - 1|^3 + |2 - 0|^3 + |3 - 6|^3 \right)^{1/3} = \sqrt[3]{35} \approx 3.27 $$

Canberra Distance

Canberra Distance is a numerical measure of the distance between points in a vector space. It is more sensitive to differences in small values, emphasizing relative differences rather than absolute differences. It is commonly used to detect small but relatively significant changes.

Suppose there exist two points

$$ \begin{align*} & x=(x_1, \cdots, x_n); \\ & y=(y_1, \cdots, y_n). \end{align*} $$

The distance between two points can be expressed as

$$ d(x, y) = \sum_{i=1}^{n} \frac{|x_i - y_i|}{|x_i| + |y_i|} $$

Example

Suppose there exist two points

$$ \begin{align*} & x=(1, 2, 3); \\ & y=(1, 0, 6). \end{align*} $$

Then their Canberra distance is

$$ \frac{|1 - 1|}{|1| + |1|} + \frac{|2 - 0|}{|2| + |0|} + \frac{|3 - 6|}{|3| + |6|} = \frac{4}{3} \approx 1.33 $$

Cosine Distance

Before discussing Cosine Distance, it is necessary to mention Cosine Similarity.

Cosine Similarity measures similarity by calculating the cosine of the angle between two vectors formed by any two points and the origin. Since the cosine calculation applies to the real number space $\mathbb{R}$, the range of Cosine Similarity is $(-1, 1)$; however, when calculating distance between two points, it is often applied in the positive real space $\mathbb{R}^+$, so the range is usually $(0, 1)$.

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Common%20Distance%20Measures/Cosine%20Distance.png
Cosine Distance.

Suppose there exist two points

$$ \begin{align*} & x=(x_1, \cdots, x_n); \\ & y=(y_1, \cdots, y_n). \end{align*} $$

The cosine similarity between two points is

$$ \begin{align*} \cos(\theta) & = {x \cdot y \over \|x\| \|y\|} \\ & = \frac{ \sum\limits_{i=1}^{n}{x_i \times y_i} }{ \sqrt{\sum\limits_{i=1}^{n}{(x_i)^2}} \times \sqrt{\sum\limits_{i=1}^{n}{(y_i)^2}} }. \end{align*} $$

In cosine similarity, the smaller the angle between the vectors formed by two points and the origin, the closer the value is to 1; conversely, it approaches 0 or -1.

Cosine distance is calculated as

$$ \text{Cosine Distance} = 1 - \text{Cosine Similarity}, $$

which can be expressed as

$$ d(x, y) = 1 - \cos(\theta), $$

where $\theta$ is the angle between the vectors formed by $x$ and $y$ with the origin.

Example

Suppose there exist two points

$$ \begin{align*} & x=(1, 2, 3); \\ & y=(1, 0, 6). \end{align*} $$

Then their cosine distance is

$$ 1 - \frac{1 \times 1 + 2 \times 0 + 3 \times 6}{\sqrt{1^2 + 2^2 + 3^2} \times \sqrt{1^2 + 0^2 + 6^2}} = 1 - \frac{19}{\sqrt{14} \times \sqrt{37}} \approx 0.17 $$

Bray–Curtis Dissimilarity

Bray–Curtis Dissimilarity is a commonly used distance measure in biology, ecology, and taxonomy to assess the difference between two samples in species composition or feature distribution. Its range is between $(0, 1)$, where 0 indicates complete similarity and 1 indicates complete dissimilarity.

Its properties are as follows:

  • If for any dimension $x_i = y_i = 0$, that dimension does not affect the calculation, making it suitable for sparse vector data.
  • It does not satisfy the distance axioms, so it is considered a “dissimilarity” rather than a strictly defined “distance.”

Suppose there exist two samples

$$ \begin{align*} & x=(x_1, \cdots, x_n); \\ & y=(y_1, \cdots, y_n). \end{align*} $$

The similarity between the two samples can be expressed as

$$ \begin{align*} \text{Bray–Curtis}(x, y) & = \frac{\sum_{i=1}^{n} |x_i - y_i|}{\sum_{i=1}^{n} (x_i + y_i)} \\ & = 1 - \frac{2 \sum_{i=1}^{n} \min(x_i, y_i)}{\sum_{i=1}^{n} (x_i + y_i)}. \end{align*} $$

Example

Suppose there exist two samples

$$ \begin{align*} & x=(1, 2, 3); \\ & y=(1, 0, 6). \end{align*} $$

Then their Bray–Curtis dissimilarity is

$$ \frac{|1 - 1| + |2 - 0| + |3 - 6|}{(1 + 1) + (2 + 0) + (3 + 6)} = \frac{5}{13} \approx 0.38 $$

Hamming Distance

Hamming Distance refers to the number of differing characters at corresponding positions between two strings of equal length. In other words, it is the number of character substitutions required to transform one string into another. It is important to note that Hamming Distance cannot be used for continuous numerical vectors.

Suppose there exist two strings

$$ \begin{align*} & x=(x_1, \cdots, x_n); \\ & y=(y_1, \cdots, y_n). \end{align*} $$

Then the Hamming distance between the two strings is

$$ d(x, y) = \sum_{i=1}^{n} \delta(x_i, y_i), $$

其中,$ \delta(x_i, y_i) = \begin{cases} 0, & \text{if } x_i = y_i; \\ 1, & \text{if } x_i \ne y_i. \end{cases} $

Example

Suppose there are three data strings of equal length as follows:

  • First set: $x = 1234567$
  • Second set: $y = 7654321$
  • Third set: $z = 1234321$

We can obtain the Hamming distances between each pair as follows:

PairComparisonHamming Distance
$d(x, y)$First set: 1234567
Second set: 7654321
6
$d(x, z)$First set: 1234567
Third set: 1234321
3
$d(y, z)$Second set: 7654321
Third set: 1234321
3

The characters marked in red indicate positions where the characters differ.

Jaccard Distance

Mentioning Jaccard Distance inevitably involves the Jaccard Index.

Intersection of sets A and B, retrieved from Wikipedia on June 28, 2025.
Union of sets A and B, retrieved from Wikipedia on June 28, 2025.

The Jaccard Index, also called the Jaccard similarity coefficient, is a method used to compare the similarity between two finite sets. It is defined as follows:

Suppose there exist two finite sets $A \in \mathbb{R}^m$ and $B \in \mathbb{R}^n$, then

$$ \begin{align*} \text{Jaccard Index} = J(A, B) & = \frac{|A \cap B|}{|A \cup B|} \\ & = \frac{|A \cap B|}{|A| + |B| - |A \cap B|}, \end{align*} $$

where $0 \le J(A, B) \le 1$.

From this, the Jaccard distance can be derived as

$$ d(A, B) = 1 - \frac{|A \cap B|}{|A \cup B|} $$

Example

Suppose there exist two sets

$$ \begin{align*} & A=\{1, 2, 3\}; \\ & B=\{1, 0, 6\}. \end{align*} $$

Then their Jaccard distance is

$$ 1 - \frac{|{1}|}{|{0,1,2,3,6}|} = 1 - \frac{1}{5} = 0.8 $$

Conclusion

Different distance metrics are suitable for different data types and application scenarios, serving as core tools for measuring relationships between data. Distance not only represents the gap between points but can also represent similarity between sets, providing models with different perspectives to capture more meaningful patterns and differences.

References