mlpack
Public Member Functions | List of all members
mlpack::metric::MahalanobisDistance< TakeRoot > Class Template Reference

The Mahalanobis distance, which is essentially a stretched Euclidean distance. More...

#include <mahalanobis_distance.hpp>

Public Member Functions

 MahalanobisDistance ()
 Initialize the Mahalanobis distance with the empty matrix as covariance. More...
 
 MahalanobisDistance (const size_t dimensionality)
 Initialize the Mahalanobis distance with the identity matrix of the given dimensionality. More...
 
 MahalanobisDistance (arma::mat covariance)
 Initialize the Mahalanobis distance with the given covariance matrix. More...
 
template<typename VecTypeA , typename VecTypeB >
double Evaluate (const VecTypeA &a, const VecTypeB &b)
 Evaluate the distance between the two given points using this Mahalanobis distance. More...
 
const arma::mat & Covariance () const
 Access the covariance matrix. More...
 
arma::mat & Covariance ()
 Modify the covariance matrix. More...
 
template<typename Archive >
void serialize (Archive &ar, const uint32_t version)
 Serialize the Mahalanobis distance.
 
template<>
double Evaluate (const VecTypeA &a, const VecTypeB &b)
 Specialization for non-rooted case.
 
template<>
double Evaluate (const VecTypeA &a, const VecTypeB &b)
 Specialization for rooted case. More...
 

Detailed Description

template<bool TakeRoot = true>
class mlpack::metric::MahalanobisDistance< TakeRoot >

The Mahalanobis distance, which is essentially a stretched Euclidean distance.

Given a square covariance matrix \( Q \) of size \( d \) x \( d \), where \( d \) is the dimensionality of the points it will be evaluating, and given two vectors \( x \) and \( y \) also of dimensionality \( d \),

\[ d(x, y) = \sqrt{(x - y)^T Q (x - y)} \]

where Q is the covariance matrix.

Because each evaluation multiplies (x_1 - x_2) by the covariance matrix, it is typically much quicker to use an LMetric and simply stretch the actual dataset itself before performing any evaluations. However, this class is provided for convenience.

If you wish to use the KNN class or other tree-based algorithms with this distance, it is recommended to instead stretch the dataset first, by decomposing Q = L^T L (perhaps via a Cholesky decomposition), and then multiply the data by L. If you still wish to use the KNN class with a custom distance anyway, you will need to use a different tree type than the default KDTree, which only works with the LMetric class.

Similar to the LMetric class, this offers a template parameter TakeRoot which, when set to false, will instead evaluate the distance

\[ d(x, y) = (x - y)^T Q (x - y) \]

which is faster to evaluate.

Template Parameters
TakeRootIf true, takes the root of the output. It is slightly faster to leave this at the default of false, but this means the metric may not satisfy the triangle inequality and may not be usable for methods that expect a true metric.

Constructor & Destructor Documentation

◆ MahalanobisDistance() [1/3]

template<bool TakeRoot = true>
mlpack::metric::MahalanobisDistance< TakeRoot >::MahalanobisDistance ( )
inline

Initialize the Mahalanobis distance with the empty matrix as covariance.

Don't call Evaluate() until you set the covariance with Covariance()!

◆ MahalanobisDistance() [2/3]

template<bool TakeRoot = true>
mlpack::metric::MahalanobisDistance< TakeRoot >::MahalanobisDistance ( const size_t  dimensionality)
inline

Initialize the Mahalanobis distance with the identity matrix of the given dimensionality.

Parameters
dimensionalityDimesnsionality of the covariance matrix.

◆ MahalanobisDistance() [3/3]

template<bool TakeRoot = true>
mlpack::metric::MahalanobisDistance< TakeRoot >::MahalanobisDistance ( arma::mat  covariance)
inline

Initialize the Mahalanobis distance with the given covariance matrix.

The given covariance matrix will be copied (this is not optimal).

Parameters
covarianceThe covariance matrix to use for this distance.

Member Function Documentation

◆ Covariance() [1/2]

template<bool TakeRoot = true>
const arma::mat& mlpack::metric::MahalanobisDistance< TakeRoot >::Covariance ( ) const
inline

Access the covariance matrix.

Returns
Constant reference to the covariance matrix.

◆ Covariance() [2/2]

template<bool TakeRoot = true>
arma::mat& mlpack::metric::MahalanobisDistance< TakeRoot >::Covariance ( )
inline

Modify the covariance matrix.

Returns
Reference to the covariance matrix.

◆ Evaluate() [1/2]

template<>
double mlpack::metric::MahalanobisDistance< true >::Evaluate ( const VecTypeA &  a,
const VecTypeB &  b 
)

Specialization for rooted case.

This requires one extra evaluation of sqrt().

◆ Evaluate() [2/2]

template<bool TakeRoot = true>
template<typename VecTypeA , typename VecTypeB >
double mlpack::metric::MahalanobisDistance< TakeRoot >::Evaluate ( const VecTypeA &  a,
const VecTypeB &  b 
)

Evaluate the distance between the two given points using this Mahalanobis distance.

If the covariance matrix has not been set (i.e. if you used the empty constructor and did not later modify the covariance matrix), calling this method will probably result in a crash.

Parameters
aFirst vector.
bSecond vector.

The documentation for this class was generated from the following files: