[][src]Module smartcore::math::distance::mahalanobis

The Mahalanobis distance is the distance between two points in multivariate space.

Mahalanobis Distance

The Mahalanobis distance (MD) is the distance between two points in multivariate space. In a regular Euclidean space the distance between any two points can be measured with Euclidean distance. For uncorrelated variables, the Euclidean distance equals the MD. However, if two or more variables are correlated the measurements become impossible with Euclidean distance because the axes are no longer at right angles to each other. MD on the other hand, is scale-invariant, it takes into account the covariance matrix of the dataset when calculating distance between 2 points that belong to the same space as the dataset.

MD between two vectors \( x \in ℝ^n \) and \( y \in ℝ^n \) is defined as \[ d(x, y) = \sqrt{(x - y)^TS^{-1}(x - y)}\]

where \( S \) is the covariance matrix of the dataset.

Example:

use smartcore::linalg::naive::dense_matrix::*;
use smartcore::math::distance::Distance;
use smartcore::math::distance::mahalanobis::Mahalanobis;

let data = DenseMatrix::from_2d_array(&[
                  &[64., 580., 29.],
                  &[66., 570., 33.],
                  &[68., 590., 37.],
                  &[69., 660., 46.],
                  &[73., 600., 55.],
]);

let a = data.column_mean();
let b = vec![66., 640., 44.];

let mahalanobis = Mahalanobis::new(&data);

mahalanobis.distance(&a, &b);

References

Structs

Mahalanobis

Mahalanobis distance.