Tutorials › Exam SRM - Statistics for Risk Modeling › Choosing a Distance Measure for the Data

Exam SRM · Hierarchical Clustering · Tutorial 747 of 926

Choosing a Distance Measure for the Data

Choose a distance measure by matching its treatment of scale, correlation, and large differences to the meaning of similarity in the data.

Advanced 11 min read

What You'll Learn

  • Define a metric and distinguish it from squared Euclidean distance.
  • Compare Euclidean, Manhattan, Minkowski, and Chebyshev distances.
  • Explain how feature units and scaling affect distance comparisons.
  • Calculate standardized Euclidean and Mahalanobis distances.
  • Relate outliers, correlation, and the clustering objective to distance choice.
  • Recognize common calculation and interpretation traps.

Why the Distance Measure Matters

Hierarchical clustering starts by comparing observations. The distance measure determines what counts as similar before any linkage rule is applied: if one feature is measured in dollars and another in days, or if a large difference in one feature should count more than several small differences, the choice of measure can change which observations are nearest. A useful distance is therefore not simply a formula selected by convention; it expresses a choice about the meaning of similarity for the task.

This tutorial compares common distances for numeric observations and explains how units, correlation, and unusual values affect their interpretation. The discussion is aligned with the SOA Exam SRM (Statistics for Risk Modeling) syllabus.

Definition: A distance function \(d(x,y)\) is a metric if, for every pair of observations \(x,y\) and every third observation \(z\), it satisfies (1) \(d(x,y)\geq 0\), with \(d(x,y)=0\) if and only if \(x=y\); (2) symmetry, \(d(x,y)=d(y,x)\); and (3) the triangle inequality, \(d(x,z)\leq d(x,y)+d(y,z)\). Not every numerical dissimilarity is a metric.

Common Distances for Numeric Observations

Let \(x=(x_1,\ldots,x_p)\) and \(y=(y_1,\ldots,y_p)\) be two observations on \(p\) numeric features. The difference in feature \(j\) is \(x_j-y_j\). The most familiar measures aggregate these coordinate differences in different ways.

Definitions: The Euclidean distance is \(d_E(x,y)=\sqrt{\sum_{j=1}^p(x_j-y_j)^2}\). The Manhattan distance is \(d_1(x,y)=\sum_{j=1}^p|x_j-y_j|\). More generally, the Minkowski distance of order \(r\geq1\) is \(d_r(x,y)=\left(\sum_{j=1}^p|x_j-y_j|^r\right)^{1/r}\). Euclidean distance is the case \(r=2\); Manhattan distance is the case \(r=1\). The limiting case \(r\to\infty\), called Chebyshev distance, is \(d_\infty(x,y)=\max_j|x_j-y_j|\).

Euclidean distance gives greater weight to a coordinate difference when that difference is large, because it is squared before the sum is taken. Manhattan distance adds absolute differences, so a given difference contributes in direct proportion to its size. Chebyshev distance keeps only the largest coordinate difference. These are distinct definitions of separation, and can rank pairs differently.

Squared Euclidean dissimilarity, \(\sum_j(x_j-y_j)^2\), is sometimes used in clustering calculations. It is not a metric: for points \(0,1,2\) on a line, the squared distance from \(0\) to \(2\) is \(4\), while the sum of the squared distances from \(0\) to \(1\) and \(1\) to \(2\) is \(1+1=2\). Thus the triangle inequality fails. Do not report a squared distance as if it were an ordinary Euclidean distance.

Worked Example: One Large Difference or Several Moderate Ones?

A hypothetical insurer compares two pairs of branch profiles using two numeric service indicators. In pair A the feature differences are \((6,0)\); in pair B they are \((4,4)\). Calculate Euclidean and Manhattan distances and determine which pair is closer under each measure.

Method. Apply each distance definition to the difference coordinates. The example isolates how the aggregation rule changes the meaning of “closer.”

For pair A, the Euclidean distance is \(\sqrt{6^2+0^2}=\sqrt{36}=6\). Its Manhattan distance is \(|6|+|0|=6\).

For pair B, the Euclidean distance is \(\sqrt{4^2+4^2}=\sqrt{16+16}=\sqrt{32}\approx5.6569\). Its Manhattan distance is \(|4|+|4|=8\).

Under Euclidean distance, pair B is closer because \(5.6569<6\). Under Manhattan distance, pair A is closer because \(6<8\). The Euclidean calculation gives relatively greater influence to concentrating the difference in one coordinate; the Manhattan calculation counts the total absolute difference across coordinates.

Check. Both computations use the same coordinate differences. The reversed ranking is not an arithmetic error; it follows from choosing different definitions of similarity. A common mistake is to declare one pair closer without naming the distance measure.

Units, Scaling, and Standardized Euclidean Distance

Ordinary Euclidean and Manhattan distances use the recorded units directly. If one feature is recorded in large numerical units, its differences can dominate the total even when the feature is not more important to the clustering task. Changing a feature from dollars to cents multiplies its differences by 100, which can substantially change the distances.

One response is to scale each feature by a positive reference scale \(s_j\), often its standard deviation in a specified reference data set. The resulting standardized Euclidean distance is

$$ d_{\mathrm{std}}(x,y)=\sqrt{\sum_{j=1}^{p}\left(\frac{x_j-y_j}{s_j}\right)^2}. $$

This puts differences in units of their feature-specific scales. It does not mean that all features are equally important in the substantive problem; it means that a one-scale-unit difference in each feature contributes equally to this particular distance. The scales should be selected and justified for the task. If every feature already has a meaningful common unit and intended weight, standardization may remove information rather than improve comparability.

The formula follows directly from transforming the coordinates. Define \(z_j(x)=(x_j-c_j)/s_j\), where \(c_j\) is any common center for feature \(j\). Then the difference between transformed observations is \(z_j(x)-z_j(y)=(x_j-y_j)/s_j\). Applying ordinary Euclidean distance to the transformed observations gives the standardized formula. The center cancels; the scale does not.

Worked Example: Comparing Features Recorded on Different Scales

A hypothetical service team compares two branches using claim frequency, recorded as a proportion, and average handling time, recorded in hours. Relative to the reference branch, branch A differs by \(0.04\) in frequency and \(3\) hours in handling time. For this example, the reference standard deviations are \(0.02\) and \(1.5\), respectively. Calculate raw and standardized Euclidean distances.

Method. First calculate the distance from the recorded coordinates, then divide each difference by its stated reference scale. The trap to avoid is treating the raw distance as meaningful without considering that the coordinates use different units and scales.

The raw difference vector is \((0.04,3)\). The raw Euclidean distance is \[ \sqrt{0.04^2+3^2}=\sqrt{0.0016+9}=\sqrt{9.0016}\approx3.0003. \]

The standardized frequency difference is \(0.04/0.02=2\). The standardized handling-time difference is \(3/1.5=2\). Therefore, \[ d_{\mathrm{std}}=\sqrt{2^2+2^2}=\sqrt{8}\approx2.8284. \]

The raw number is expressed on the mixture of the original scales and has no single natural unit. The standardized distance says that the branch differs by two reference standard deviations on each feature. That interpretation depends on the specified reference scales; it does not establish that standardization is automatically appropriate for every clustering objective.

Check. Both standardized coordinate differences equal \(2\), so their squared contributions are \(4\) and \(4\), giving a squared distance of \(8\) and distance \(\sqrt{8}\). Do not divide the final distance by the number of features unless a different, explicitly defined measure is intended.

Mahalanobis Distance and Correlated Features

Standardized Euclidean distance adjusts for feature scales but treats the features as uncorrelated. When numeric features are correlated, a difference in a direction that commonly varies together may be less unusual than a difference of the same Euclidean length in a direction rarely observed. Mahalanobis distance accounts for both scales and linear correlation using a covariance matrix.

Definition: Given a positive-definite covariance matrix \(\Sigma\) for the numeric features, the Mahalanobis distance between \(x\) and \(y\) is \(d_M(x,y)=\sqrt{(x-y)^\mathsf{T}\Sigma^{-1}(x-y)}\). The covariance matrix must describe an appropriate reference population or data set. If it is not invertible, this formula cannot be applied as written.

The inverse covariance matrix weights differences according to their covariance structure. To see why, suppose \(\Sigma=LL^\mathsf{T}\) is a Cholesky factorization, and set \(w=L^{-1}(x-y)\). Then \[ w^\mathsf{T}w=(x-y)^\mathsf{T}(L^{-1})^\mathsf{T}L^{-1}(x-y) =(x-y)^\mathsf{T}\Sigma^{-1}(x-y). \] Thus Mahalanobis distance is Euclidean distance after transforming differences to remove covariance and scale. The covariance estimate matters: if it is poorly estimated or distorted by extreme observations, the resulting distances can be misleading.

Worked Example: Equal Euclidean Lengths, Different Covariance-Adjusted Distances

Suppose two numeric service measures have reference covariance matrix \(\Sigma=\begin{pmatrix}4&2\\2&4\end{pmatrix}\). Compare a branch whose difference from a reference branch is \(\delta_1=(2,2)\) with one whose difference is \(\delta_2=(2,-2)\). Calculate Euclidean and Mahalanobis distances and interpret the contrast.

Method. Calculate Euclidean lengths directly, then invert the stated covariance matrix and evaluate the quadratic form. The measures are described as numeric service measures, not standardized variables: the diagonal entries \(4\) are variances, not unit variances.

Both Euclidean distances are \(\sqrt{2^2+2^2}=\sqrt{8}\approx2.8284\). The determinant of \(\Sigma\) is \(4(4)-2(2)=12\), so \[ \Sigma^{-1}=\frac{1}{12}\begin{pmatrix}4&-2\\-2&4\end{pmatrix} =\begin{pmatrix}1/3&-1/6\\-1/6&1/3\end{pmatrix}. \]

For \(\delta_1=(2,2)\), multiplication gives \(\Sigma^{-1}\delta_1=(1/3,1/3)\). The squared Mahalanobis distance is \(\delta_1^\mathsf{T}\Sigma^{-1}\delta_1=2(1/3)+2(1/3)=4/3\), so \(d_M=\sqrt{4/3}\approx1.1547\).

For \(\delta_2=(2,-2)\), multiplication gives \(\Sigma^{-1}\delta_2=(1,-1)\). The squared distance is \(\delta_2^\mathsf{T}\Sigma^{-1}\delta_2=2(1)+(-2)(-1)=4\), so \(d_M=2\).

The covariance is positive definite because its leading diagonal entry is positive and its determinant is \(12>0\). The common-direction difference \((2,2)\) is less unusual relative to this positively correlated reference variation than the opposing-direction difference \((2,-2)\), so its Mahalanobis distance is smaller. Check: both ordinary Euclidean distances remain equal; only the covariance adjustment distinguishes the directions.

Outliers and Choosing a Measure for the Task

A large coordinate difference can strongly affect Euclidean distance because it is squared. Manhattan distance still responds to a large difference, but its contribution grows linearly rather than quadratically. Neither choice makes the data immune to outliers: an extreme observation can still be far from many other observations and can affect the cluster structure. In Mahalanobis distance, an extreme value can also affect the estimated covariance matrix, which may then change distances for the whole data set.

Choosing robust scales or a robust covariance estimate can reduce sensitivity to unusual observations, but those choices require justification and do not repair a mismatch between the features and the task. Consider whether the unusual values are errors, rare but valid cases, or the very cases the analysis needs to distinguish. Removing or downweighting them without that assessment can erase relevant structure.

A practical choice starts by stating what “similar” means in context. Use the feature units directly when they are comparable and meaningful for the objective. Consider standardized Euclidean distance when features have different scales and equal importance is intended per reference-scale unit. Consider Mahalanobis distance when covariance-adjusted differences are the desired notion of unusualness and a suitable covariance estimate is available. Manhattan distance may be useful when a sum of absolute coordinate differences is meaningful. These are guides, not automatic rules; the distance and its inputs should be reported together.

Common exam trap: For differences \((3,4)\), writing the Euclidean distance as \(3^2+4^2=25\) confuses squared Euclidean dissimilarity with Euclidean distance. The correction is \(d_E=\sqrt{25}=5\). Also check whether a question supplies raw or standardized variables: never call features standardized if their stated variances are not one.

Check Your Understanding

For each problem, identify the requested distance carefully and state the comparison or calculation.

  1. Two observations differ by \((1,-2,2)\). Find their Euclidean and Manhattan distances. (Answer: Euclidean \(=3\); Manhattan \(=5\).)
  2. Two features differ by \(6\) and \(2\), with reference standard deviations \(3\) and \(4\). Find standardized Euclidean distance. (Answer: \(\sqrt{4.25}\approx2.0616\).)
  3. Let \(\Sigma=\begin{pmatrix}9&0\\0&4\end{pmatrix}\) and let the difference vector be \((3,2)\). Find the Mahalanobis distance. (Answer: \(\sqrt{2}\approx1.4142\).)
  4. Find the Minkowski distance of order \(3\) for difference vector \((1,2,2)\). (Answer: \(\sqrt[3]{17}\approx2.5713\).)
  5. Show why squared Euclidean distance is not a metric using the points \(0,1,2\) on a line. (Answer: \(d^2(0,2)=4\), but \(d^2(0,1)+d^2(1,2)=1+1=2\), violating the triangle inequality.)
Clinical Biostats