{
  "id": "mahalanobis-distance",
  "title": "Mahalanobis Distance",
  "lang": "en",
  "source_lang": "en",
  "summary": "Mahalanobis distance measures separation between vectors relative to a covariance structure, accounting for differences in scale and correlations among variables.",
  "category": "mathematics",
  "subcategory": "probability-statistics",
  "type": "concept",
  "aliases": [
    "Mahalanobis metric",
    "Generalized statistical distance"
  ],
  "generated_at": "2026-11-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/mahalanobis-distance/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/mahalanobis-distance/",
      "json": "https://aiwiki.page/zh/mahalanobis-distance.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Mahalanobis distance is a measure of separation between vectors that accounts for the scales and relationships of their components. Unlike ordinary Euclidean distance, it measures displacement relative to a specified covariance matrix: differences along highly variable directions contribute less, while differences along tightly concentrated directions contribute more. Introduced by the Indian statistician Prasanta Chandra Mahalanobis in 1936, it is widely used in multivariate statistics, pattern recognition, and anomaly detection."
    },
    {
      "heading": "Definition",
      "text": "Let \\(x,y\\in\\mathbb{R}^{p}\\), and let \\(\\Sigma\\) be a symmetric positive-definite matrix. The Mahalanobis distance between \\(x\\) and \\(y\\), relative to \\(\\Sigma\\), is\n\n\\[d_{\\Sigma}(x,y) = \\sqrt{(x-y)^{\\mathsf T}\\Sigma^{-1}(x-y)},\\]\n\nwhere \\({}^{\\mathsf T}\\) denotes transpose and \\(\\Sigma^{-1}\\) is the inverse of \\(\\Sigma\\).\n\nFrequently, the reference point is the mean vector \\(\\mu\\) of a probability distribution. The distance of an observation \\(x\\) from that mean is then\n\n\\[D(x)=\\sqrt{(x-\\mu)^{\\mathsf T}\\Sigma^{-1}(x-\\mu)}.\\]\n\nThe quantity\n\n\\[D^{2}(x)=(x-\\mu)^{\\mathsf T}\\Sigma^{-1}(x-\\mu)\\]\n\nis the squared Mahalanobis distance. Software and statistical literature sometimes report this squared quantity simply as “Mahalanobis distance,” so the convention must be checked when interpreting results.\n\nThe matrix \\(\\Sigma\\) is typically a population covariance matrix or an estimate obtained from reference data. Its inverse is often called the precision matrix. The distance is not a property of the two points alone: changing the reference covariance changes their separation."
    },
    {
      "heading": "Scale and correlation",
      "text": "Standardization by variability\n\nFor a single variable with mean \\(\\mu\\) and standard deviation \\(\\sigma>0\\),\n\n\\[D(x)=\\frac{|x-\\mu|}{\\sigma}.\\]\n\nThus, one-dimensional Mahalanobis distance is the absolute standardized deviation from the mean.\n\nIf the covariance matrix is diagonal,\n\n\\[\\Sigma=\\operatorname{diag}(\\sigma_1^2,\\ldots,\\sigma_p^2),\\]\n\nthen\n\n\\[d_{\\Sigma}^{2}(x,y) = \\sum_{j=1}^{p}\\frac{(x_j-y_j)^2}{\\sigma_j^2}.\\]\n\nThis is Euclidean distance after each coordinate has been divided by its standard deviation. If \\(\\Sigma\\) is the identity matrix, Mahalanobis distance reduces exactly to Euclidean distance.\n\nAdjustment for correlated variables\n\nWhen variables are correlated, standardizing each coordinate separately is insufficient. Mahalanobis distance also accounts for their correlation.\n\nConsider two variables with unit variances and correlation \\(\\rho\\), where \\(|\\rho|<1\\). For a displacement \\(\\Delta=(a,b)^{\\mathsf T}\\),\n\n\\[d_{\\Sigma}^{2} = \\frac{a^2-2\\rho ab+b^2}{1-\\rho^2}.\\]\n\nIf \\(\\rho=0.9\\), then the displacements \\((1,1)\\) and \\((1,-1)\\) have the same Euclidean length, but their squared Mahalanobis distances are\n\n\\[d_{\\Sigma}^{2}(1,1)=\\frac{2}{1+0.9}\\approx1.053,\\]\n\nand\n\n\\[d_{\\Sigma}^{2}(1,-1)=\\frac{2}{1-0.9}=20.\\]\n\nThe first displacement follows the usual joint variation of positively correlated variables. The second runs against that pattern and is therefore much more unusual relative to the reference covariance."
    },
    {
      "heading": "Geometric interpretation",
      "text": "Ellipsoidal contours\n\nPoints at a fixed distance \\(c\\) from \\(\\mu\\) satisfy\n\n\\[(x-\\mu)^{\\mathsf T}\\Sigma^{-1}(x-\\mu)=c^2.\\]\n\nThese sets are ellipsoids; in two dimensions they are ellipses.\n\nWriting the spectral decomposition as\n\n\\[\\Sigma=Q\\Lambda Q^{\\mathsf T},\\]\n\nthe columns of \\(Q\\) are orthonormal eigenvectors, and\n\n\\[\\Lambda=\\operatorname{diag}(\\lambda_1,\\ldots,\\lambda_p)\\]\n\ncontains positive eigenvalues. If \\(u=Q^{\\mathsf T}(x-\\mu)\\), then\n\n\\[D^{2}(x)=\\sum_{j=1}^{p}\\frac{u_j^2}{\\lambda_j}.\\]\n\nThe ellipsoid’s axes follow the eigenvectors, and its semiaxis lengths are \\(c\\sqrt{\\lambda_j}\\). Directions with larger variance permit larger displacements at the same Mahalanobis distance. This decomposition also connects the distance to principal component analysis.\n\nWhitening\n\nDefine transformed coordinates by\n\n\\[z=\\Sigma^{-1/2}(x-\\mu).\\]\n\nThen\n\n\\[D(x)=\\|z\\|_2.\\]\n\nThis transformation, called whitening, turns covariance-adjusted distance into ordinary Euclidean distance. If a random vector has covariance \\(\\Sigma\\), its whitened version has identity covariance. Whitening removes linear correlations, but it does not generally establish statistical independence; that stronger conclusion holds for jointly normal variables.\n\nMetric properties\n\nFor a fixed positive-definite \\(\\Sigma\\),\n\n\\[\\langle u,v\\rangle_{\\Sigma}=u^{\\mathsf T}\\Sigma^{-1}v\\]\n\ndefines an inner product, and Mahalanobis distance is the associated distance. It therefore defines a metric: it is nonnegative, symmetric, zero only for identical points, and satisfies the triangle inequality.\n\nThe squared distance is not itself a metric because it generally fails the triangle inequality. Likewise, if a different covariance matrix is chosen separately for each pair of points, the resulting rule need not be a metric."
    },
    {
      "heading": "Invariance under changes of coordinates",
      "text": "Mahalanobis distance is invariant under invertible affine transformations, provided the covariance is transformed consistently.\n\nLet\n\n\\[x'=Ax+b,\\qquad y'=Ay+b,\\]\n\nwhere \\(A\\) is invertible. The transformed covariance is\n\n\\[\\Sigma'=A\\Sigma A^{\\mathsf T}.\\]\n\nThen\n\n\\[d_{\\Sigma'}(x',y')=d_{\\Sigma}(x,y).\\]\n\nConsequently, changing measurement units, rotating coordinates, or applying an invertible linear mixing does not change the distance when the covariance is updated accordingly.\n\nThis invariance does not generally extend to transformations that discard information, such as projection onto fewer dimensions."
    },
    {
      "heading": "Relationship to probability distributions",
      "text": "Multivariate normal distributions\n\nFor a multivariate normal distribution with mean \\(\\mu\\) and positive-definite covariance \\(\\Sigma\\), the probability density function is\n\n\\[f(x)= \\frac{1}{(2\\pi)^{p/2}|\\Sigma|^{1/2}} \\exp\\left(-\\frac{1}{2}D^{2}(x)\\right).\\]\n\nThus, equal-density contours are also equal-Mahalanobis-distance ellipsoids. For a fixed Gaussian distribution, density decreases as distance from its mean increases.\n\nIf\n\n\\[X\\sim N_p(\\mu,\\Sigma),\\]\n\nwith the true parameters used in the distance, then\n\n\\[D^{2}(X)\\sim\\chi_p^2,\\]\n\na chi-squared distribution with \\(p\\) degrees of freedom. This gives probability regions of the form\n\n\\[D^{2}(x)\\leq \\chi^{2}_{p,\\,1-\\alpha},\\]\n\nwhich contain probability \\(1-\\alpha\\).\n\nThis chi-squared result requires multivariate normality and known population parameters. It does not automatically hold when the mean and covariance are estimated from the same observations being assessed.\n\nBeyond normality\n\nMahalanobis distance remains mathematically meaningful for nonnormal distributions with a finite, positive-definite covariance matrix. Its interpretation as a direct measure of rarity is then more limited.\n\nFor any random vector \\(X\\) with mean \\(\\mu\\) and nonsingular covariance \\(\\Sigma\\),\n\n\\[\\operatorname{E}[D^{2}(X)]=p,\\]\n\nregardless of normality. However, the full distribution of \\(D^{2}(X)\\) depends on the distribution of \\(X\\).\n\nCovariance alone cannot describe multimodality, curved dependence, or all aspects of tail behavior. A point close to the overall mean can therefore lie in a low-density region, especially when the data contain separated clusters."
    },
    {
      "heading": "Estimation from data",
      "text": "For observations \\(x_1,\\ldots,x_n\\), a common estimate uses the sample mean\n\n\\[\\bar{x}=\\frac{1}{n}\\sum_{i=1}^{n}x_i\\]\n\nand sample covariance\n\n\\[S=\\frac{1}{n-1} \\sum_{i=1}^{n}(x_i-\\bar{x})(x_i-\\bar{x})^{\\mathsf T}.\\]\n\nThe estimated squared distance is\n\n\\[\\widehat{D}^{2}(x) = (x-\\bar{x})^{\\mathsf T}S^{-1}(x-\\bar{x}).\\]\n\nIts statistical behavior depends on whether \\(x\\) belongs to the estimation sample or is an independent new observation.\n\nA related statistic is Hotelling’s \\(T^2\\), used in hypothesis testing about a multivariate population mean. For independent observations from a multivariate normal population,\n\n\\[T^2=n(\\bar{x}-\\mu_0)^{\\mathsf T}S^{-1}(\\bar{x}-\\mu_0).\\]\n\nUnder the null hypothesis that the population mean is \\(\\mu_0\\), and for \\(n>p\\),\n\n\\[\\frac{n-p}{p(n-1)}T^2\\sim F_{p,n-p}.\\]\n\nThis concerns the estimated mean, not the distance of an individual observation."
    },
    {
      "heading": "Applications",
      "text": "Anomaly detection. Large distances identify observations far from a reference center relative to its covariance structure. This can reveal unusual combinations of measurements that would not appear extreme when examined separately.\n\nClassification. A common covariance matrix produces a shared geometry for comparing observations with class centers. Under Gaussian class models with equal covariance and equal prior probabilities, choosing the nearest center by Mahalanobis distance is equivalent to choosing the most probable class. With unequal covariances, Gaussian classification also requires covariance-determinant terms and, where applicable, class-prior terms.\n\nClustering and similarity search. In cluster analysis and pattern matching, covariance-adjusted distances account for elongated groups and correlated features. Cluster-specific covariance matrices describe different local shapes, although they do not necessarily define a single global metric.\n\nMultivariate monitoring. Distances combine several correlated measurements into a single statistic for detecting changes in an industrial or observational process.\n\nMetric learning. In machine learning, a generalized squared distance is often written\n\n\\[d_M^2(x,y)=(x-y)^{\\mathsf T}M(x-y),\\]\n\nwhere \\(M\\) is learned from data rather than necessarily chosen as an inverse covariance. A positive-definite \\(M\\) gives a metric; a positive-semidefinite \\(M\\) may give only a pseudometric."
    },
    {
      "heading": "Limitations and numerical computation",
      "text": "Singular and poorly estimated covariance\n\nThe sample covariance has rank at most \\(n-1\\), so it is necessarily singular when \\(p\\geq n\\). Exact linear dependencies among variables can also make it singular.\n\nA generalized inverse can replace the ordinary inverse, but then nonzero displacements in the covariance null space receive zero distance. The result is a pseudometric on the full space, although it can be a genuine metric on the retained subspace.\n\nNear-singularity is also problematic. Small estimated eigenvalues receive large inverse weights, potentially magnifying noise and making distances unstable.\n\nRegularization can stabilize the calculation. One example is\n\n\\[S_{\\lambda}=S+\\lambda I,\\qquad \\lambda>0.\\]\n\nShrinkage toward a diagonal or otherwise structured covariance matrix is another approach. These modifications change the geometry and generally sacrifice exact affine invariance.\n\nSensitivity to outliers\n\nOrdinary means and covariance estimates are sensitive to extreme observations. An outlier can shift the estimated center and inflate variance in its own direction, reducing its apparent distance—an effect called masking. Robust location and covariance estimates are used to reduce such effects, although their calibration and performance depend on the estimation method and assumptions.\n\nStable calculation\n\nExplicitly forming \\(S^{-1}\\) is usually unnecessary. If a Cholesky decomposition gives\n\n\\[S=LL^{\\mathsf T},\\]\n\nsolve\n\n\\[Lz=x-\\bar{x}\\]\n\nand compute\n\n\\[\\widehat{D}^{2}(x)=z^{\\mathsf T}z.\\]\n\nThis avoids constructing an inverse and generally improves numerical stability. When the covariance is singular or nearly singular, eigenvalue methods or singular-value decomposition can make the treatment of small-variance directions explicit."
    }
  ],
  "links": [
    "euclidean-distance",
    "covariance-matrix",
    "statistics",
    "positive-definite-matrix",
    "inverse-matrix",
    "probability-distribution",
    "standard-deviation",
    "identity-matrix",
    "correlation",
    "eigenvalues-and-eigenvectors",
    "principal-component-analysis",
    "statistical-independence",
    "inner-product",
    "metric-space",
    "affine-map",
    "multivariate-normal-distribution",
    "probability-density-function",
    "chi-squared-distribution",
    "sample-mean",
    "statistical-hypothesis-testing",
    "cluster-analysis",
    "machine-learning",
    "regularization",
    "cholesky-decomposition",
    "numerical-stability"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}