Skip to content

CAssociation · Guide test 085

Distance covariance independence test

Tests general dependence between random vectors, including nonlinear relationships.

Runs in CodeSplashPython (calls R) example not runR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

In Test for any dependence (ξ, Hoeffding, distance correlation, HSIC), choose Dependence measure = Distance correlation (dCor).

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Distance covariance independence test

30

Shape of the link

0.80
0.050

Your data

Simulated with a known link

If H₀ were true

T = 2.36
  1. The reference: how T varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .009).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

T=M−1 R∗1−R∗2,M=n(n−3)2T=\sqrt{M-1}\,\dfrac{R^*}{\sqrt{1-R^{*2}}},\quad M=\tfrac{n(n-3)}{2}

With your numbers

R∗=0.12,T=2.36,dCor=0.45R^* = 0.12,\qquad T = 2.36,\qquad \text{dCor} = 0.45
R∗R^*
bias-corrected distance correlation (U-centered distances)
TT
Székely–Rizzo t on M − 1 df

The verdict

t(404) = 2.36, p = .009, R* = 0.12

p ≤ α = .050: reject H₀

Effect size: bias-corrected distance correlation R* = 0.117

Distance correlation is zero only under independence, whatever the shape. The bias-corrected version has a t reference.

Statistic
2.365
p-value
0.0093
Distance covariance independence test on 30 (x, y) pairs: t(404) = 2.36, p = .009, R* = 0.12. The p-value is below α = .050, so the data are hard to reconcile with H0.

Distance covariance independence test on 30 (x, y) pairs: t(404) = 2.36, p = .009, R* = 0.12. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: random vectors X,Y are independent.

Assumptions

Independent paired vectors and finite first moments for the usual Euclidean distance covariance; appropriate exchangeability for permutation.

Formula

A=HaH, B=HbHVn2=n−2∑ijAijBijH=I−11T/n\begin{gathered}A=HaH,\ B=HbH\\[5pt] \mathcal V_n^2=n^{-2}\sum_{ij}A_{ij}B_{ij}\\[5pt] H=I-\mathbf1\mathbf1^T/n\end{gathered}

Parameters and notation

aij=‖Xi−Xj‖, bij=‖Yi−Yj‖. A,B are double centered distance matrices; unbiased U-centered versions also exist.

Structure of the method

Construct pairwise distance matrices for the two observed vectors, center them according to the chosen biased or unbiased estimator, and aggregate their elementwise products. The ordinary independence test compares this statistic with its distribution under permutations of one sample’s row identities.

Estimated and nuisance parameters

Distances and centered distance matrices are estimated from the paired observations. There is no nuisance Gaussian correlation model. A standardized distance-correlation coefficient is distinct from raw distance covariance; some packages report n times the squared covariance as their test statistic.

Settings and design choices

Specify metric, coordinate standardization, distance exponent, biased versus U-centered statistic, permutation count, and seed. For Euclidean distance powers, broad independence characterization requires the appropriate exponent range; using squared distances is not a harmless interchangeable choice for omnibus detection.

Fitting and adaptation

Recalculate distances when feature scaling or data change. For ordinary independent paired rows, a permutation can reorder a precomputed centered matrix consistently in both dimensions. Repeated, clustered, or serial observations need a justified dependence-preserving scheme. Fine-tuning does not apply to the test itself.

Sensitivity and robustness

High dimensions and irrelevant features can reduce power by concentrating distances. Scale choices may privilege a subset of coordinates. The null is independence, not merely zero covariance; finite first moments support the usual theory. Small permutation budgets yield coarse p-values and simulation uncertainty.

How to use and calibrate

Permute one vector’s labels, recompute the statistic, and use its upper tail. State biased versus unbiased statistic.

Example application

Test dependence between multivariate measurements and outcomes.

Interpretation and pitfalls

Watch out

Ordinary independent permutations are invalid for clustered or serial data. Distance metric and feature scaling affect power.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

Permutation test of independence; R is the number of random permutations. Exchangeability is required.

Python · invokes R

Not run — Executed end to end through Rscript · needs Python standard library + Rscript; R packages: energy

# Python launcher for the complete R implementation below.
# Requires Rscript on PATH and the listed R packages.
import shutil
import subprocess
rscript = shutil.which("Rscript")
if rscript is None:
    raise RuntimeError("Install R and make Rscript available on PATH.")
r_code = r"""
set.seed(2026)
x<-rnorm(60);y<-x^2+rnorm(60)
print(energy::dcov.test(x,y,R=999))
"""
# No shell interpolation. A failed R execution raises an error in Python.
result = subprocess.run([rscript, "--vanilla", "-"], input=r_code,
                        text=True, capture_output=True)
print(result.stdout)
if result.stderr:
    print(result.stderr)
result.check_returncode()
R

Validated — Executed successfully in the validation environment · needs energy

set.seed(2026)
x<-rnorm(60);y<-x^2+rnorm(60)
print(energy::dcov.test(x,y,R=999))
Output
	dCov independence test (permutation test)

data:  index 1, replicates 999
nV^2 = 5.4496, p-value = 0.005
sample estimates:
     dCov 
0.3013738

Sources

Statistics guide v4 (historical):
Python: Executed end to end through Rscript · R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Python-to-R bridge; Rscript is unavailable. R code was not executed. Python: Not run in the reference's review: Python-to-R bridge; Rscript is unavailable. R code was not executed. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 085, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT085.