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

Exam SRM · Nearest-Neighbor Learning · Tutorial 787 of 926

Minkowski, Euclidean, and Manhattan Distances

Calculate Minkowski, Euclidean, and Manhattan distances and compare how their different treatments of coordinate differences affect nearest-neighbor rankings.

Intermediate 10 min read

What You'll Learn

  • Define Minkowski distance for numeric predictor vectors and identify its metric parameter.
  • Calculate Manhattan and Euclidean distances from coordinate differences.
  • Recognize how the Minkowski parameter changes the contribution of large coordinate differences.
  • Compare nearest-neighbor rankings under different distance metrics, including ties.
  • Avoid confusing Manhattan distance, Euclidean distance, and squared Euclidean distance.

Why Distance Choice Matters

A nearest-neighbor method compares a query observation with observations in a data set. The distance calculation determines which observations are regarded as close, so changing the distance metric can change the neighbor ranking even when the predictor values remain exactly the same. This tutorial focuses on three metrics for numeric predictors: Minkowski distance, of which Manhattan and Euclidean distance are special cases.

The calculations below assume that each observation is represented by a vector with the same number of numeric coordinates. They use the coordinate values as supplied; they do not alter or standardize the predictors. A distance is a measure of separation, not a prediction: the earlier material on the K-nearest-neighbor prediction rule describes how selected neighbors are then used.

Definition: For two observations \(x=(x_1,\ldots,x_d)\) and \(z=(z_1,\ldots,z_d)\), the Minkowski distance of order \(r\), where \(r\geq 1\), is $$ D_r(x,z)=\left(\sum_{j=1}^{d}|x_j-z_j|^r\right)^{1/r}. $$ The absolute coordinate differences \(|x_j-z_j|\) are the inputs to the calculation. Setting \(r=1\) gives Manhattan distance, and setting \(r=2\) gives Euclidean distance.
$$ D_1(x,z)=\sum_{j=1}^{d}|x_j-z_j|, \qquad D_2(x,z)=\sqrt{\sum_{j=1}^{d}(x_j-z_j)^2}. $$

The order \(r\) controls how the coordinate differences combine. Manhattan distance adds their absolute values. Euclidean distance squares them, adds the squares, and takes a square root. For \(r>1\), a relatively large coordinate difference has more influence on the sum before the final root is taken. The final root is part of the distance formula; omitting it gives a different quantity.

How the Formulas Measure Separation

For two coordinates, Euclidean distance is the length of the straight line between the points. If their horizontal and vertical separations are \(a\) and \(b\), the Pythagorean theorem gives the square of that length as \(a^2+b^2\). Taking the square root therefore gives \(\sqrt{a^2+b^2}\), which is exactly the Euclidean formula. In more dimensions, the squared separations across all coordinates are added in the same way.

Manhattan distance has a different geometric interpretation. If movement is restricted to changing one coordinate at a time, moving from one point to another requires a total of \(|x_1-z_1|\) units in the first coordinate, \(|x_2-z_2|\) in the second, and so on. Adding those movements gives \(\sum_j|x_j-z_j|\). This explains why a diagonal separation can have a different Manhattan distance from its Euclidean distance.

More generally, Minkowski distance combines the coordinate differences by raising their absolute values to a common power and then taking the matching root. It is a metric for \(r\geq1\): it is nonnegative, is zero exactly when the two vectors are identical, is symmetric, and satisfies the triangle inequality. The triangle inequality says that going directly from \(x\) to \(z\) cannot be longer than going from \(x\) to a third point \(w\) and then from \(w\) to \(z\):

$$ D_r(x,z)\leq D_r(x,w)+D_r(w,z). $$

For this topic, the practical task is usually to calculate distances and rank candidate observations. The ranking—not just the numerical distance for one candidate—matters when the goal is to identify nearest neighbors.

Worked Example: Calculating All Three Distances

A query observation has predictors \(q=(2,1)\). Four observations have predictors \(A=(5,5)\), \(B=(4,1)\), \(C=(2,4)\), and \(D=(0,0)\). Calculate the Manhattan, Euclidean, and order-3 Minkowski distances from the query to each observation, then rank them under Manhattan and Euclidean distance.

Method. First calculate the absolute coordinate differences. Then apply the selected distance formula to those differences.

ObservationAbsolute differences from \(q\)Manhattan \(D_1\)Euclidean \(D_2\)Order-3 \(D_3\)
A(3, 4)3 + 4 = 7\(\sqrt{3^2+4^2}=5\)\(\sqrt[3]{3^3+4^3}=\sqrt[3]{91}\approx4.498\)
B(2, 0)2 + 0 = 2\(\sqrt{2^2+0^2}=2\)\(\sqrt[3]{2^3+0^3}=2\)
C(0, 3)0 + 3 = 3\(\sqrt{0^2+3^2}=3\)\(\sqrt[3]{0^3+3^3}=3\)
D(2, 1)2 + 1 = 3\(\sqrt{2^2+1^2}=\sqrt{5}\approx2.236\)\(\sqrt[3]{2^3+1^3}=\sqrt[3]{9}\approx2.080\)

Under Manhattan distance, the ordering from nearest to farthest is \(B\), then \(C\) and \(D\) tied, then \(A\). Under Euclidean distance, it is \(B\), \(D\), \(C\), then \(A\), since \(\sqrt{5}<3\). The tie between \(C\) and \(D\) under Manhattan distance disappears under Euclidean distance.

For the order-3 distances, the displayed approximations are rounded to three decimal places. Their unrounded values give the ordering \(B\), \(D\), \(C\), \(A\). The check is consistent with the computed values: \(2<\sqrt[3]{9}<3<\sqrt[3]{91}\). This example shows why a candidate’s relative position can depend on the metric.

Common mistake avoided: ranking \(C\) ahead of \(D\) under Manhattan distance because \(C\) has one zero coordinate. Manhattan distance adds all absolute differences; it does not select or average coordinates.

Changing the Metric Can Change the Nearest Observation

A particularly important comparison is between adding absolute differences and combining squared differences. A point can have a smaller total absolute difference but a larger Euclidean distance, or the reverse. A change in nearest neighbor is not an arithmetic error if each metric has been calculated correctly.

Worked Example: A Different Nearest Neighbor Under Each Metric

For a query \(q=(0,0)\), compare observations \(A=(0,4)\), \(B=(3,2)\), and \(C=(5,0)\). Identify the nearest observation under Manhattan distance and under Euclidean distance.

For \(A\), the absolute differences are \((0,4)\). Thus \(D_1(q,A)=0+4=4\) and \(D_2(q,A)=\sqrt{0^2+4^2}=4\).

For \(B\), the absolute differences are \((3,2)\). Thus \(D_1(q,B)=3+2=5\), while \(D_2(q,B)=\sqrt{3^2+2^2}=\sqrt{13}\approx3.606\).

For \(C\), the absolute differences are \((5,0)\). Thus \(D_1(q,C)=5+0=5\), while \(D_2(q,C)=\sqrt{5^2+0^2}=5\).

The Manhattan distances are \(4,5,5\), so \(A\) is the unique nearest observation under that metric. The Euclidean distances are \(4,\sqrt{13},5\); because \(\sqrt{13}\approx3.606<4<5\), \(B\) is the unique nearest under Euclidean distance. The nearest observation changes because the coordinate differences combine differently, not because either calculation is inconsistent.

Method and trap avoided: calculate both metrics for every candidate before ranking. Comparing only the largest coordinate difference, or assuming that the same point must be nearest under every metric, would give an unsupported conclusion.

The Minkowski Order and Large Differences

The Minkowski order \(r\) lets us compare how different rules treat the same coordinate differences. For example, with differences \((4,0)\), the order-1, order-2, and order-3 distances are all \(4\). With differences \((3,3)\), they are \(6\), \(\sqrt{18}\), and \(\sqrt[3]{54}\). A change in \(r\) can therefore change the ranking between these two patterns.

Worked Example: Changing the Minkowski Order

A query has predictors \(q=(0,0)\). Candidate \(A=(4,0)\) differs by \((4,0)\), while candidate \(B=(3,3)\) differs by \((3,3)\). Compare the two candidates using \(r=1\), \(r=2\), and \(r=3\).

For \(r=1\): \(D_1(q,A)=|4|+|0|=4\). For \(B\), \(D_1(q,B)=|3|+|3|=6\). Candidate \(A\) is nearer.

For \(r=2\): \(D_2(q,A)=\sqrt{4^2+0^2}=4\). For \(B\), \(D_2(q,B)=\sqrt{3^2+3^2}=\sqrt{18}\approx4.243\). Candidate \(A\) is nearer.

For \(r=3\): \(D_3(q,A)=\sqrt[3]{4^3+0^3}=4\). For \(B\), \(D_3(q,B)=\sqrt[3]{3^3+3^3}=\sqrt[3]{54}\approx3.780\). Candidate \(B\) is now nearer.

The last approximation is rounded to three decimal places. The switch is verified by \(54<4^3=64\), so \(\sqrt[3]{54}<4\). The order-3 metric ranks the candidate with two moderate differences ahead of the candidate with one larger difference, unlike orders 1 and 2 in this comparison. The lesson is to calculate the requested order rather than infer a ranking from a different metric.

Common Calculation Traps

A frequent error is to use the right coordinate differences but the wrong combination rule. For example, for query \(q=(0,0)\) and candidate \(B=(3,2)\), the calculation \(3+2=5\) is the Manhattan distance. It is not the Euclidean distance. The correct Euclidean calculation is

$$ D_2(q,B)=\sqrt{3^2+2^2}=\sqrt{13}\approx3.606. $$

The reverse mix-up is also possible: calculating \(\sqrt{13}\) and reporting it as Manhattan distance. Label the formula before substituting values, especially when a question asks for more than one metric.

Another trap is to report the sum of squared differences, \(13\), as Euclidean distance. That sum is squared Euclidean distance; Euclidean distance is its square root, \(\sqrt{13}\). For ranking candidates using Euclidean distance alone, comparing sums of squared differences gives the same ordering because the square-root function is increasing on nonnegative values. However, the reported distances are not equal, and the squared value is not the requested Euclidean distance.

Finally, preserve ties unless the calculated distances resolve them. If two observations have the same distance under the stated metric, the distance calculation alone does not rank one ahead of the other. Do not break a tie using another metric unless the question explicitly asks for that comparison.

Key takeaway: For numeric predictor vectors, Manhattan distance adds absolute coordinate differences, Euclidean distance takes the square root of the sum of squared differences, and general Minkowski distance uses an order \(r\geq1\). Calculate distances using the specified metric, then rank the resulting values; different metrics can produce different nearest neighbors and ties.

Check Your Understanding

For each problem, calculate the requested distances before deciding which observations are nearest.

  1. A query is \(q=(1,2)\), and candidates are \(A=(4,6)\) and \(B=(4,2)\). Calculate the Manhattan and Euclidean distances to both candidates and identify the nearest under each metric. (Answer: \(A\): Manhattan \(7\), Euclidean \(5\); \(B\): Manhattan \(3\), Euclidean \(3\); \(B\) is nearest under both.)
  2. For query \(q=(0,0)\), candidates are \(A=(0,4)\), \(B=(3,2)\), and \(C=(5,0)\). Find the nearest candidate under each of Manhattan and Euclidean distance. (Answer: Manhattan: \(A\), at distance \(4\); Euclidean: \(B\), at distance \(\sqrt{13}\approx3.606\).)
  3. For query \(q=(0,0)\), candidate \(A=(4,0)\) and candidate \(B=(3,3)\). Which candidate is nearer for \(r=1\), \(r=2\), and \(r=3\)? Give the distances for each order. (Answer: \(r=1\): \(A=4,B=6\), so \(A\); \(r=2\): \(A=4,B=\sqrt{18}\approx4.243\), so \(A\); \(r=3\): \(A=4,B=\sqrt[3]{54}\approx3.780\), so \(B\).)
  4. A query is \(q=(1,2,0)\). Candidate \(P=(3,5,4)\) and candidate \(Q=(4,2,1)\). Calculate their Manhattan and Euclidean distances and identify the nearest under each metric. (Answer: \(P\): Manhattan \(9\), Euclidean \(\sqrt{29}\approx5.385\); \(Q\): Manhattan \(4\), Euclidean \(\sqrt{10}\approx3.162\); \(Q\) is nearest under both.)
  5. For query \(q=(0,0)\), candidates are \(A=(1,4)\), \(B=(3,0)\), \(C=(0,3)\), and \(D=(2,2)\). Identify the two closest observations under Manhattan distance and under Euclidean distance, noting any boundary tie. (Answer: Manhattan: \(B\) and \(C\) tie at distance \(3\); Euclidean: \(D\) is first at \(\sqrt{8}\), and \(B\) and \(C\) tie at distance \(3\) for the next position.)
Clinical Biostats