Usage of distance correlation#

Example that shows the usage of distance correlation.

import timeit
import traceback

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats

import dcor

Distance correlation is a measure of dependence between distributions, analogous to the classical Pearson’s correlation coefficient. However, Pearson’s correlation can be 0 even when there is a nonlinear dependence, while distance correlation is 0 only for independent distributions.

As an example, consider the following data sampled from two dependent distributions:

n_samples = 1000
random_state = np.random.default_rng(123456)

x = random_state.uniform(-1, 1, size=n_samples)
y = x**2 + random_state.normal(0, 0.01, size=n_samples)

plt.scatter(x, y, s=1)
plot dcor usage

The data from the first distribution comes from a uniform distribution. However, the second distribution is a noisy function of the first one: \(y \approx x^2\). It is clear then that there is a nonlinear dependence between the distributions. The standard Pearson’s correlation coefficient is not able to detect this kind of dependence:

scipy.stats.pearsonr(x, y).statistic

Note that Pearson’s correlation takes values in \([-1, 1]\), with values near the extremes indicating a high degree of linear correlation. In this case the value is near 0, as the correlation is not linear, and thus is ignored by this method.

However, distance correlation correctly identifies the nonlinear dependence:


As an additional advantage distance correlation can be applied between samples of arbitrary dimensional random vectors, even with different dimensions between x and y.

n_features_x2 = 2

x2 = random_state.uniform(-1, 1, size=(n_samples, 2))
y2 = x2[:, 0]**2 + x2[:, 1]**2


fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter(x2[:, 0], x2[:, 1], y2, s=1)

dcor.distance_correlation(x2, y2)
plot dcor usage
(1000, 2)


As with the Pearson’s correlation, distance correlation is computed from a related covariance measure, called distance covariance:


The standard naive algorithm for computing distance covariance and correlation requires the computation of the distance matrices between the observations in x and between the observations in y. This has a computational cost in both time and memory of \(O(n^2)\), with \(n\) the number of observations:

n_calls = 100

    lambda: dcor.distance_correlation(x, y, method="naive"),

When both x and y are one-dimensional, there are alternative algorithms with a computational cost of \(O(n\log(n))\), one based on the theory of the AVL balanced trees and one based on the popular sorting algorithm mergesort:

    lambda: dcor.distance_correlation(x, y, method="avl"),
    lambda: dcor.distance_correlation(x, y, method="mergesort"),

By default, these fast algorithms are used when possible (this is what the default value for the method, “auto”, means):

    lambda: dcor.distance_correlation(x, y, method="auto"),

Note that explicitly trying to use the fast algorithms with multidimensional random vectors will produce an error.

    dcor.distance_correlation(x2, y2, method="avl")
except Exception:
Traceback (most recent call last):
  File "/home/docs/checkouts/", line 129, in <module>
    dcor.distance_correlation(x2, y2, method="avl")
  File "/home/docs/checkouts/", line 1033, in distance_correlation
  File "/home/docs/checkouts/", line 911, in distance_correlation_sqr
    return method.value.stats_sqr(
  File "/home/docs/checkouts/", line 260, in stats_sqr
    ) = self.terms(
  File "/home/docs/checkouts/", line 533, in _distance_covariance_sqr_terms_avl
    x, y = _transform_to_1d(x, y)
  File "/home/docs/checkouts/", line 125, in _transform_to_1d
    assert array.shape[1] == 1

Total running time of the script: (0 minutes 2.028 seconds)

Gallery generated by Sphinx-Gallery