Tutorials › Exam SRM - Statistics for Risk Modeling › Euclidean and Manhattan Distances

Exam SRM · Hierarchical Clustering · Tutorial 727 of 926

Euclidean and Manhattan Distances

Calculate and compare two common distances, then organize the results in a pairwise dissimilarity matrix.

Advanced 10 min read

What You'll Learn

  • Calculate Euclidean distance as the square root of summed squared coordinate differences
  • Calculate Manhattan distance as the sum of absolute coordinate differences
  • Compare how the two measures respond to changes across coordinates
  • Construct and check a pairwise distance matrix
  • Avoid confusing Euclidean distance with squared Euclidean distance

Why the Choice of Distance Matters

Hierarchical clustering begins with a way to measure how dissimilar two observations are. When each observation is represented by several numerical features, the distance calculation combines differences across those features into one number. A different distance definition can give a different picture of which observations are close, even when the data are unchanged.

This tutorial focuses on Euclidean and Manhattan distance. Both are calculated from the coordinate differences between two observations, but they combine those differences differently. The resulting pairwise distances can be arranged in a dissimilarity matrix, the numerical input used to describe all pairs of observations.

Definition: Suppose each of \(n\) observations is represented by a vector of \(p\) numerical features, with observation \(i\) written \(x_i=(x_{i1},x_{i2},\ldots,x_{ip})\). For two observations \(i\) and \(j\), the Euclidean distance is $$ d_E(i,j)=\sqrt{\sum_{k=1}^{p}(x_{ik}-x_{jk})^2}, $$ and the Manhattan distance is $$ d_M(i,j)=\sum_{k=1}^{p}|x_{ik}-x_{jk}|. $$ These formulas require numerical coordinates defined for both observations on the same \(p\) features. A distance is nonnegative, is zero for identical feature vectors, is symmetric in its two observations, and satisfies the triangle inequality.

For Euclidean distance, each coordinate difference is squared before the differences are added; the square root returns the result to the units of the original features. For Manhattan distance, each difference is converted to its absolute value and then added. The absolute value prevents positive and negative coordinate changes from cancelling.

The names suggest two different ways of moving between points. In two dimensions, Euclidean distance is the straight-line length. Manhattan distance is the total distance when movement is restricted to horizontal and vertical segments along the coordinate axes. These interpretations also help explain why the two calculations need not agree.

Why the Euclidean Formula Uses a Square Root

For two features, let the signed coordinate differences be \(a=x_{i1}-x_{j1}\) and \(b=x_{i2}-x_{j2}\). The horizontal and vertical segments form a right triangle. By the Pythagorean theorem, the square of the straight-line length is \(a^2+b^2\); because length is nonnegative, the distance is \(\sqrt{a^2+b^2}\). With additional coordinates, the squared differences are added in the same way: each coordinate contributes a perpendicular component to the squared length.

Manhattan distance adds the lengths of the coordinate-aligned segments instead. In two dimensions those lengths are \(|a|\) and \(|b|\), so their total is \(|a|+|b|\). In \(p\) dimensions, add the absolute differences for all \(p\) coordinates.

Worked Example: Calculating Both Distances and a Matrix

A hypothetical analyst represents three risk profiles using two numerical features. Let \(A=(0,0)\), \(B=(3,4)\), and \(C=(3,0)\). Calculate the Euclidean and Manhattan distances for every pair, then form each pairwise dissimilarity matrix.

Method. For each pair, first find the coordinate differences. Square and sum them before taking the square root for Euclidean distance; take absolute values and add them for Manhattan distance. A common mistake is to calculate only one pair and assume the same distance applies to all pairs.

For \(A\) and \(B\), the coordinate differences are \(3-0=3\) and \(4-0=4\). Thus \(d_E(A,B)=\sqrt{3^2+4^2}=\sqrt{9+16}=5\), and \(d_M(A,B)=|3|+|4|=7\).

For \(A\) and \(C\), the differences are \(3\) and \(0\). Thus \(d_E(A,C)=\sqrt{3^2+0^2}=3\), and \(d_M(A,C)=|3|+|0|=3\).

For \(B\) and \(C\), the differences are \(3-3=0\) and \(4-0=4\). Thus \(d_E(B,C)=\sqrt{0^2+4^2}=4\), and \(d_M(B,C)=|0|+|4|=4\).

Place the observations in the order \(A,B,C\). Distances from an observation to itself are zero, and reversing a pair does not change its distance. The two matrices are therefore

$$ D_E= \begin{pmatrix} 0&5&3\\ 5&0&4\\ 3&4&0 \end{pmatrix}, \qquad D_M= \begin{pmatrix} 0&7&3\\ 7&0&4\\ 3&4&0 \end{pmatrix}. $$

Check. Both matrices are symmetric, have zero diagonals, and contain nonnegative entries. The \(A,B\) pair has different Euclidean and Manhattan distances, while the other two pairs lie along one coordinate axis and have equal distances under both measures.

How Coordinate Differences Affect the Result

Euclidean distance gives relatively more weight to a large coordinate difference because that difference is squared. Manhattan distance counts each unit of absolute change additively. As a result, a single large difference can have a stronger effect on Euclidean distance than several smaller differences with the same total absolute change.

This does not mean that Manhattan distance ignores large differences: it includes their full absolute size. Rather, the contrast is in how the differences combine. For a fixed total absolute difference, Euclidean distance is largest when that difference is concentrated in one coordinate. Splitting the same total across several coordinates can reduce the Euclidean distance.

For example, compare difference vectors \((4,0)\) and \((2,2)\). Each has Manhattan distance \(4\) from the origin. Their Euclidean distances are \(4\) and \(\sqrt{8}\), respectively. The shared Manhattan total does not imply a shared Euclidean distance.

Worked Example: Equal Manhattan Totals, Different Euclidean Distances

Consider a reference profile \(O=(0,0)\) and two hypothetical profiles \(U=(4,0)\) and \(V=(2,2)\). Compare both distances from \(O\) to each profile. Explain what the comparison reveals about how the coordinate differences are distributed.

Method. Calculate the absolute differences for Manhattan distance, then square the coordinate differences for Euclidean distance. A common mistake is to conclude that equal Manhattan distances must also produce equal Euclidean distances.

For \(U\), the differences from \(O\) are \(4\) and \(0\). Therefore, \(d_M(O,U)=|4|+|0|=4\), and \(d_E(O,U)=\sqrt{4^2+0^2}=\sqrt{16}=4\).

For \(V\), the differences are \(2\) and \(2\). Therefore, \(d_M(O,V)=|2|+|2|=4\), while \(d_E(O,V)=\sqrt{2^2+2^2}=\sqrt{4+4}=\sqrt{8}=2\sqrt{2}\), approximately \(2.828\).

Interpretation and check. The Manhattan distances match because both profiles have a total absolute difference of \(4\). The Euclidean distances do not match: the difference for \(U\) is concentrated in one coordinate, while \(V\)'s difference is split across two. The approximation \(2.828\) is rounded to three decimal places at the final step.

Constructing and Checking Pairwise Dissimilarity Matrices

For \(n\) observations, a pairwise dissimilarity matrix records the distance for every pair. Its row and column labels use the same observation order. The entry in row \(i\), column \(j\) is the distance between observations \(i\) and \(j\). For either Euclidean or Manhattan distance, the matrix is symmetric and has zeros on its diagonal.

A matrix has \(n^2\) entries but only \(n(n-1)/2\) distinct off-diagonal distances to calculate: there are that many unordered pairs of different observations. Each calculated pairwise distance can be entered twice, in the two symmetric positions. This is an efficient way to build the matrix and a useful check against transcription errors.

Worked Example: Building a Four-Observation Distance Matrix

A hypothetical dataset contains observations \(A=(0,0)\), \(B=(1,2)\), \(C=(4,2)\), and \(D=(4,6)\). Construct the Euclidean pairwise dissimilarity matrix. Give exact distances and, where needed, approximations rounded to three decimal places.

Method. Calculate the six distinct pair distances, then reflect each distance across the diagonal. A common mistake is to omit a pair or to place a distance in only one of its two matrix positions.

For \(A,B\), the differences are \((1,2)\), so \(d_E(A,B)=\sqrt{1^2+2^2}=\sqrt{5}\), approximately \(2.236\).

For \(A,C\), the differences are \((4,2)\), so \(d_E(A,C)=\sqrt{4^2+2^2}=\sqrt{20}=2\sqrt{5}\), approximately \(4.472\).

For \(A,D\), the differences are \((4,6)\), so \(d_E(A,D)=\sqrt{4^2+6^2}=\sqrt{52}=2\sqrt{13}\), approximately \(7.211\).

For \(B,C\), the differences are \((3,0)\), so \(d_E(B,C)=\sqrt{3^2+0^2}=3\). For \(B,D\), the differences are \((3,4)\), so \(d_E(B,D)=\sqrt{3^2+4^2}=5\). For \(C,D\), the differences are \((0,4)\), so \(d_E(C,D)=\sqrt{0^2+4^2}=4\).

In the order \(A,B,C,D\), the matrix is

$$ D_E= \begin{pmatrix} 0&\sqrt{5}&2\sqrt{5}&2\sqrt{13}\\ \sqrt{5}&0&3&5\\ 2\sqrt{5}&3&0&4\\ 2\sqrt{13}&5&4&0 \end{pmatrix} \approx \begin{pmatrix} 0&2.236&4.472&7.211\\ 2.236&0&3&5\\ 4.472&3&0&4\\ 7.211&5&4&0 \end{pmatrix}. $$

Check. The six distinct pairs appear in the matrix: \(AB\), \(AC\), \(AD\), \(BC\), \(BD\), and \(CD\). Each value is mirrored across the diagonal, and the diagonal entries are zero. All decimal approximations are rounded to three places only after the exact calculations.

Common Mistake: Reporting Squared Euclidean Distance

The sum of squared coordinate differences is not Euclidean distance; it is squared Euclidean distance. The square root is part of the distance formula. For a coordinate difference of \((3,4)\), incorrectly stopping at \(3^2+4^2=25\) reports \(25\), not the Euclidean distance. The correct calculation is \(\sqrt{25}=5\).

Squared Euclidean values may appear in some calculations, but they are on a different scale from the Euclidean distances. When asked for Euclidean distance or a matrix of Euclidean distances, take the square root for every pair. Do not take a square root for Manhattan distance: its defining calculation is the sum of absolute differences.

These choices matter because a distance matrix records the dissimilarities supplied to a clustering procedure. Two matrices based on the same observations can differ in their numerical entries and in how strongly they reflect concentrated versus spread-out coordinate differences. The calculation should therefore be explicit, and the matrix should be checked for symmetry, a zero diagonal, and correct pairwise entries.

Key takeaway: Euclidean distance squares coordinate differences, sums them, and takes a square root; Manhattan distance sums their absolute values. Calculate each distinct pair once, then use symmetry and the zero diagonal to check the dissimilarity matrix.

Check Your Understanding

For each problem, calculate the requested distances carefully and check the coordinate differences before combining them.

  1. Two observations have feature vectors \(R=(2,1)\) and \(S=(8,9)\). Find their Euclidean and Manhattan distances. (Answer: Euclidean \(10\); Manhattan \(14\))
  2. For \(P=(2,5,1)\) and \(Q=(5,1,3)\), calculate both distances and give the Euclidean distance to three decimal places. (Answer: Euclidean \(\sqrt{29}\approx5.385\); Manhattan \(9\))
  3. Let \(A=(0,1)\), \(B=(2,4)\), and \(C=(5,1)\). Find the Euclidean and Manhattan distances for all three pairs. (Answer: \(AB=(\sqrt{13},5)\), \(AC=(5,5)\), \(BC=(3\sqrt{2},6)\), with each ordered as Euclidean then Manhattan)
  4. From \(O=(0,0)\), compare \(U=(5,0)\) and \(V=(2,3)\). Determine whether their Manhattan distances match and whether their Euclidean distances match. (Answer: Manhattan distances are both \(5\); Euclidean distances are \(5\) and \(\sqrt{13}\approx3.606\), respectively)
  5. For \(P=(0,0)\), \(Q=(1,3)\), and \(R=(4,0)\), write the Manhattan dissimilarity matrix in the order \(P,Q,R\). (Answer: \(\begin{pmatrix}0&4&4\\4&0&6\\4&6&0\end{pmatrix}\))
Clinical Biostats