﻿ SquareMahalanobis.FromCovarianceMatrix Method   # SquareMahalanobisAddLanguageSpecificTextSet("LSTF93ADE14_0?cpp=::|nu=.");FromCovarianceMatrix Method

Creates a new Square-Mahalanobis distance from a covariance matrix.

Namespace:  Accord.Math.Distances
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0 Syntax
```public static SquareMahalanobis FromCovarianceMatrix(
double[,] covariance
)```

#### Parameters

covariance
Type: SystemDouble
A covariance matrix.

#### Return Value

Type: SquareMahalanobis
A square Mahalanobis distance using the SingularValueDecomposition of the given covariance matrix. Examples
```// Let's say we would like to compute the Squared
// Mahalanobis distance between the vectors 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 = SquareMahalanobis.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 SquareMahalanobis(new CholeskyDecomposition(covariance));
var fromSVD = new SquareMahalanobis(new SingularValueDecomposition(covariance));
var fromPrecision1 = new SquareMahalanobis(covariance.Inverse());
var fromPrecision2 = SquareMahalanobis.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