Click or drag to resize
Accord.NET (logo)

Mahalanobis Structure

Mahalanobis distance.

Namespace:  Accord.Math.Distances
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public struct Mahalanobis : IMetric<double[]>, 
	IDistance<double[]>, IDistance<double[], double[]>
Request Example View Source

The Mahalanobis type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleMahalanobis(Double)
Initializes a new instance of the Mahalanobis class.
Public methodCode exampleMahalanobis(CholeskyDecomposition)
Initializes a new instance of the Mahalanobis class.
Public methodCode exampleMahalanobis(SingularValueDecomposition)
Initializes a new instance of the Mahalanobis class.
Top
Methods
  NameDescription
Public methodCode exampleDistance
Computes the distance d(x,y) between points x and y.
Public methodEquals
Indicates whether this instance and a specified object are equal.
(Inherited from ValueType.)
Public methodStatic memberCode exampleFromCovarianceMatrix
Creates a new Mahalanobis distance from a covariance matrix.
Public methodStatic memberCode exampleFromPrecisionMatrix
Creates a new Mahalanobis distance from a precision matrix.
Public methodGetHashCode
Returns the hash code for this instance.
(Inherited from ValueType.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodToString
Returns the fully qualified type name of this instance.
(Inherited from ValueType.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Examples
// Let's say we would like to compute the Mahalanobis
// distance between the two vectors x and y below:
double[] x = { 2, 5, 1 };
double[] y = { 4, 2, 2 };

// Using the covariance
double[,] covariance =
{
    { 4, 3, 0 },
    { 3, 5, 2 },
    { 0, 2, 6 }
};

// There are multiple ways to create a Mahalanobis 
// distance. The easiest method by far is by using:
var mahalanobis = Mahalanobis.FromCovarianceMatrix(covariance);

// Then, you can compute the distance using:
double distance = mahalanobis.Distance(x, y);

// However, if you need more control over how the covariance matrix
// should be inverted, or if you have the precision matrix instead of
// the covariance, you can use any of the alternative methods:

var fromCholesky = new Mahalanobis(new CholeskyDecomposition(covariance));
var fromSVD = new Mahalanobis(new SingularValueDecomposition(covariance));
var fromPrecision1 = new Mahalanobis(covariance.Inverse());
var fromPrecision2 = Mahalanobis.FromPrecisionMatrix(covariance.Inverse());

// They all should produce equivalent results:
double a = fromCholesky.Distance(x, y);
double b = fromSVD.Distance(x, y);
double c = fromPrecision1.Distance(x, y);
double d = fromPrecision2.Distance(x, y);
See Also