You are on page 1of 6

(http://people.revoledu.

com/kardi/)

MENU

Mahalanobis Distance

(index.html)

< Previous (Correlation.html) | Next (Normalization.html) | Content (index.html) >

Mahalanobis Distance
Mahalanobis distance is also called quadratic distance . It measures the separation of two groups
of objects. Suppose we have two groups with means and , Mahalanobis distance is given by
the following

Formula

The data of the two groups must have the same number of variables (the same number of
columns) but not necessarily to have the same number of data (each group may have different
number of rows).

In Matlab, the code is as follow:


function d=MahalanobisDistance(A, B)
% Return mahalanobis distance of two data matrices
% A and B (row = object, column = feature)
% @author: Kardi Teknomo
% http://people.revoledu.com/kardi/index.html
[n1, k1]=size(A);
[n2, k2]=size(B);
n=n1+n2;
if(k1~=k2)
disp('number of columns of A and B must be the same')
else
xDiff=mean(A)-mean(B); % mean diff row vector
cA=Covariance(A);
cB=Covariance(B);
pC=n1/n*cA+n2/n*cB; % pooled covariance matrix
d=sqrt(xDiff*inv(pC)*xDiff'); % mahalanobis distance
end

The code above requires computation of Covariance matrix, which code is given below
function C=Covariance(X)
% Return covariance given data matrix X (row = object, column = feature)
% @author: Kardi Teknomo
% http://people.revoledu.com/kardi/index.html
[n,k]=size(X);
Xc=X-repmat(mean(X),n,1); % centered data
C=Xc'*Xc/n; % covariance

Example
Suppose we have two groups of data, each of group consists of two variables (x, y). The scattered

plot of data is shown below.

First, we center the data on the arithmetic mean of each variable.

Covariance matrix of group is computed using centered data matrix

It produces covariance matrices for group 1 and 2 as follow


The pooled covariance matrix of the two groups is computed as weighted average of the
covariance matrices. The weighted average takes this form

The pooled covariance is computed using weighted average (10/15)*Covariance group 1 +


(5/15)*Covariance group 2 yields

The Mahalanobis distance is simply quadratic multiplication of mean difference and inverse of
pooled covariance matrix.

To perform the quadratic multiplication, check again the formula of Mahalanobis distance above.
When you get mean difference, transpose it, and multiply it by inverse pooled covariance. After
that, multiply the result with the mean difference again and you take the square root. The final
result of Mahalanobis distance is

Spreadsheet example (MS Excel) of this Mahalanobis computation can be downloaded here
(../../download/download.php?file=Mahalanobis) .

Basics of Titration - Free Titration Handbook


This comprehensive guide tells you everything about the basics of titration.
mt.com/Titration/Handbook

Use the interactive program below to compute Mahalanobis distance. If you like this program,
please recommend it to your friends.
How to use the program:
Input are two matrices name matrix A and matrix B that represent features coordinates of two
objects. The columns indicate the features, and the rows are the observations. The number of
features of the two objects must be equal (i.e. columns of matrix A = columns of matrix B). Each
matrix should have at least 2 rows and 1 column.

A matrix is sequence of numbers in a tabular format, inputted using the following format:

each number in a row is separated by a comma or a space


each row is separated by semicolon ;

Validate your input before running the program. The initial input values are the example.
Refresh your browser to get back the example.

This program is presented by Kardi Teknomo (http://people.revoledu.com/kardi/)

Input Matrix A
2, 2; 2, 5; 6, 5; 7, 3; 4, 7;
6, 4; 5, 3; 4, 6; 2, 5; 1, 3;

Input Matrix B
6, 5; 7, 4; 8, 7; 5, 6; 5, 4;

Clear Input
Validate Input
Get Mahalanobis Distance

Get a $300 free trial credit to get


started with any GCP product. TRY IT FREE
Samples of Applications of Mahalanobis Distance

Mahalanobis distances in habitat selection (http://www.ncbi.nlm.nih.gov/pubmed/18409444)


studies
Mahalanobis distance for skin color range for face detection
(http://ieeexplore.ieee.org/xpl/articleDetails.jsp?reload=true&arnumber=5480001)
Mahalanobis distance for observational epidemiology, health promotion and social
determinants (http://www.scielo.br/scielo.php?pid=S1415-
790X2014000300668&script=sci_arttext)
Mahalanobis Distance for Leadership and Education Science (http://eric.ed.gov/?
id=ED528876)
Mahalanobis Distance for fuzzy classifier (http://perso.telecom-
paristech.fr/~bloch/P6/PRREC/deer.pdf)
Mahalanobis Distance for Classifiers (http://www.mathematik.uni-
stuttgart.de/fak8/ians/publications/files/HP08b.pdf)
Mahalanobis distance for classifying species
(http://adsabs.harvard.edu/full/2001CRABS..54i..79N)
Mahalanobis distance for computer vision
(http://lrs.icg.tugraz.at/pubs/roth_springer_prid_2014.pdf)
Mahalanobis distance in robotic (http://www.scientific.net/MSF.773-774.759)

< Previous (Correlation.html) | Next (Normalization.html) | Content (index.html) >

Rate this tutorial (../Rating/RateTutorial.php?TutorialName=Similarity)

This tutorial is copyrighted.

Preferable reference for this tutorial is

Teknomo, Kardi (2015) Similarity Measurement. http:\people.revoledu.comkardi


tutorialSimilarity

Copyright 2017 Kardi Teknomo


Revoledu Design

You might also like