# Distance Correlation

I am interested in calculating the distance correlation between a pair of vectors. To do it I use CorrelationDistance function, but I get a distance greater than 1. One of the of the properties of this distance that is limited between 0 and 1. I am using a correct function, or I did miss something?

CorrelationDistance[RandomReal[5, 100], RandomReal[5, 100]]

• Where did you get that the distance should be between $0$ and $1$? Recall that the correlation distance is effectively computed as $1-\cos(\text{something})$, which can take values in $[0, 2]$. – J. M.'s technical difficulties Sep 24 '18 at 19:14
• @J.M. Please take a look for the first property in the Wikipedia link – Kiril Danilchenko Sep 24 '18 at 19:17
• The Wikipedia article is about distance correlation, not about correlation distance... – Henrik Schumacher Sep 24 '18 at 19:20
• Ok... anyway I am interested in calculate distance covariance. If it is possible to use one of WL distances. – Kiril Danilchenko Sep 24 '18 at 19:27

If I am not mistaken, the distance correlation can be computed as follows:

DistanceCorrelation[X_, Y_] := Module[{A, B, n, w},
n = Length[X];
w = ConstantArray[1./n, {n}];
Module[{x, y},
A = DistanceMatrix[X];
x = A.w;
y = Subtract[0.5 (x.w), x];
A += (Outer[Plus, y, y]);
B = DistanceMatrix[Y];
x = B.w;
y = Subtract[0.5 (x.w), x];
B += (Outer[Plus, y, y]);
];

Sqrt[
Total[A B, 2]/Sqrt[Total[A^2, 2] Total[B^2, 2]]
]
]


Example:

X = RandomReal[5, 100];
Y = RandomReal[5, 100];
DistanceCorrelation[X, Y]


# Edit

The Wikipedia article is very confusing. I am not sure whether the overall square root over the result should be there or not.

• Hmm, why not use e.g. x = Mean[A], since it computes means of columns directly? – J. M.'s technical difficulties Sep 24 '18 at 19:47
• Good point. I changed it. – Henrik Schumacher Sep 24 '18 at 19:56
• In the meantime, I replaced Mean by a Dot for performance reasons. – Henrik Schumacher Sep 24 '18 at 21:33
• Oh yeah, that is nicer... – J. M.'s technical difficulties Sep 25 '18 at 2:49