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]]
  • 2
    $\begingroup$ 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]$. $\endgroup$
    – J. M.'s torpor
    Sep 24 '18 at 19:14
  • $\begingroup$ @J.M. Please take a look for the first property in the Wikipedia link $\endgroup$ Sep 24 '18 at 19:17
  • 2
    $\begingroup$ The Wikipedia article is about distance correlation, not about correlation distance... $\endgroup$ Sep 24 '18 at 19:20
  • $\begingroup$ Ok... anyway I am interested in calculate distance covariance. If it is possible to use one of WL distances. $\endgroup$ 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]);

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


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


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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.