Notes

Mahalanobis Distance

Mahalanobis distance is a way to measure how “unusual” a data point is, but it does so with more common sense than plain Euclidean distance. It notices when variables move together and adjusts the distance accordingly.

What it is (and what makes it different)
In many datasets, features have different scales and are correlated (e.g., house size and number of bedrooms). Mahalanobis distance measures distance after accounting for the dataset’s covariance structure. Technically, for a point x, a mean vector μ, and covariance matrix Σ, it is:

d(x, μ) = sqrt( (x - μ)^T Σ^{-1} (x - μ) )

The key is Σ-1: it down-weights directions where the data naturally varies a lot, and up-weights directions where variation is rare. If features are uncorrelated and equally scaled, Mahalanobis distance behaves like standardized Euclidean distance.

Intuition: distance in “standard deviation units”
Think of data forming an elongated cloud. Moving along the long axis of the cloud is “normal,” so it shouldn’t count as much distance. Moving across the short axis is surprising, so it counts more. Mahalanobis distance essentially measures how many multivariate standard deviations away a point is from the center.

Practical examples
You’ll see it used for:

  • Outlier detection: flagging unusual patient lab panels where several measurements jointly look abnormal.
  • Quality control: detecting defective products when multiple sensor readings are correlated.
  • Similarity with correlated features: comparing customers using spending categories that tend to rise together.

Why it matters in AI/ML
Many ML problems rely on “distance.” If you ignore covariance, you can misjudge similarity and miss true anomalies. Mahalanobis distance appears in Gaussian models, Linear Discriminant Analysis (LDA), and anomaly scoring (often assuming roughly elliptical/normal feature distributions). In practice, estimating Σ well (sometimes with shrinkage or robust covariance) is crucial, especially in high dimensions.

Mahalanobis Distance measures how far a point is from a distribution while accounting for feature scale and correlations, using the inverse covariance matrix. Unlike Euclidean distance, it downweights directions with high variance and handles correlated variables appropriately. It is important in AI/ML for multivariate outlier detection, clustering, and Gaussian-based classification. Example: flagging anomalous sensor readings by computing each reading’s distance from the normal operating mean.

Imagine you’re checking how “unusual” a person is in a crowd, but you don’t just look at one thing like height—you look at height and weight together. A tall person isn’t that unusual if they’re also heavier, because those two traits often move together.

Mahalanobis Distance is a way to measure how far a data point is from the “typical” point in a dataset while accounting for how features vary and how they relate to each other (their shared patterns). In AI and machine learning, it’s often used to spot outliers, detect anomalies, or decide how similar a new example is to a known group.