java.lang.Object
imagingbook.common.math.MahalanobisDistance
This class implements the Mahalanobis distance using the Apache Commons Math library. No statistical bias correction is used. See the Appendix G (Sections G.2-G.3) of [1] for additional details and examples.
[1] W. Burger, M.J. Burge, Digital Image Processing – An Algorithmic Introduction, 3rd ed, Springer (2022).
- Version:
- 2023/01/01
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final double
Tolerance used for validating positvity in the Cholesky decomposition.static final double
The default minimum diagonal value used to condition the covariance matrix.static final double
Tolerance used for validating symmetry in the Cholesky decomposition. -
Constructor Summary
ConstructorsConstructorDescriptionMahalanobisDistance
(double[][] cov) Constructor, creates a newMahalanobisDistance
instance from the m x m covariance matrix and the m-dimensional mean vector of a distribution of m-dimensional data. -
Method Summary
Modifier and TypeMethodDescriptiondouble
distance
(double[] a, double[] b) Returns the Mahalanobis distance between the given points.double
distance2
(double[] a, double[] b) Returns the squared Mahalanobis distance between the given points a, b: d^2(a,b) = (a-b)^T .static MahalanobisDistance
fromSamples
(double[][] samples) Creates a newMahalanobisDistance
instance from an array of m-dimensional samples, e.g., samples = {{x1,y1}, {x2,y2},...,{xn,yn}} for a vector of n two-dimensional samples.static MahalanobisDistance
fromSamples
(double[][] samples, double minDiagVal) Creates a newMahalanobisDistance
instance from an array of m-dimensional samples, e.g.double[][]
Returns the covariance matrix used for distance calculations.int
Returns the sample dimension (M) for this instance.double[][]
Returns the inverse of the conditioned covariance matrix.double[][]
Returns the 'root' (U) of the inverse covariance matrix S^{-1}, such that S^{-1} = U^T .double[][]
getWhiteningTransformation
(double relativeSymmetryThreshold, double absolutePositivityThreshold) Returns the 'root' (U) of the inverse covariance matrix S^{-1}, such that S^{-1} = U^T .
-
Field Details
-
DefaultMinimumDiagonalValue
The default minimum diagonal value used to condition the covariance matrix.- See Also:
-
DefaultRelativeSymmetryThreshold
Tolerance used for validating symmetry in the Cholesky decomposition. SeeCholeskyDecomposition.DEFAULT_RELATIVE_SYMMETRY_THRESHOLD
(1.0E-15), which seems too small).- See Also:
-
DefaultAbsolutePositivityThreshold
Tolerance used for validating positvity in the Cholesky decomposition. SeeCholeskyDecomposition.DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD
.- See Also:
-
-
Constructor Details
-
MahalanobisDistance
Constructor, creates a newMahalanobisDistance
instance from the m x m covariance matrix and the m-dimensional mean vector of a distribution of m-dimensional data. The covariance matrix is assumed to be properly conditioned, i.e., has all-positive diagonal values.- Parameters:
cov
- the covariance matrix of size m x m
-
-
Method Details
-
fromSamples
Creates a newMahalanobisDistance
instance from an array of m-dimensional samples, e.g., samples = {{x1,y1}, {x2,y2},...,{xn,yn}} for a vector of n two-dimensional samples. UsesDefaultMinimumDiagonalValue
to condition the covariance matrix.- Parameters:
samples
- a sequence of m-dimensional samples, i.e., samples[k][i] represents the i-th component of the k-th sample- Returns:
- a
MahalanobisDistance
instance
-
fromSamples
Creates a newMahalanobisDistance
instance from an array of m-dimensional samples, e.g. samples = {{x1,y1}, {x2,y2},...,{xn,yn}} for a vector of two-dimensional samples (n = 2).- Parameters:
samples
- a vector of length n with m-dimensional samples, i.e., samples[k][i] represents the i-th component of the k-th sampleminDiagVal
- the minimum diagonal value used to condition the covariance matrix to avoid singularity (seeDefaultMinimumDiagonalValue
)- Returns:
- a
MahalanobisDistance
instance
-
getDimension
Returns the sample dimension (M) for this instance.- Returns:
- the sample dimension
-
getCovarianceMatrix
Returns the covariance matrix used for distance calculations.- Returns:
- the conditioned covariance matrix
-
getInverseCovarianceMatrix
Returns the inverse of the conditioned covariance matrix.- Returns:
- the inverse of the conditioned covariance matrix
-
getWhiteningTransformation
Returns the 'root' (U) of the inverse covariance matrix S^{-1}, such that S^{-1} = U^T . U This matrix can be used to pre-transform the original sample vectors X (by X → U . X) to a space where distance (in the Mahalanobis sense) can be measured with the usual Euclidean norm. The matrix U is invertible in case the reverse transformation is required.- Returns:
- the m x m matrix for pre-transforming the original (m-dimensional) sample vectors
-
getWhiteningTransformation
public double[][] getWhiteningTransformation(double relativeSymmetryThreshold, double absolutePositivityThreshold) Returns the 'root' (U) of the inverse covariance matrix S^{-1}, such that S^{-1} = U^T . U This matrix can be * used to pre-transform the original sample vectors X (by X → U . X) to a space where distance (in the * Mahalanobis sense) can be measured with the usual Euclidean norm. The matrix U is invertible in case the reverse * transformation is required.- Parameters:
relativeSymmetryThreshold
- maximum deviation from symmetry (seeDefaultRelativeSymmetryThreshold
)absolutePositivityThreshold
- maximum deviation from positivity (seeDefaultAbsolutePositivityThreshold
)- Returns:
-
distance
Returns the Mahalanobis distance between the given points.- Parameters:
a
- first pointb
- second point- Returns:
- the Mahalanobis distance
-
distance2
Returns the squared Mahalanobis distance between the given points a, b: d^2(a,b) = (a-b)^T . S^{-1} . (a-b)- Parameters:
a
- first pointb
- second point- Returns:
- the squared Mahalanobis distance
-