Skip to content

CAssociation · Guide test 086

Hilbert–Schmidt independence criterion test

Uses kernels to test independence between variables or structured observations.

Runs in CodeSplashPython example validatedR 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 = HSIC (Gaussian kernels).

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 · Hilbert–Schmidt independence criterion test

30

Shape of the link

0.80
0.050

Your data

Simulated with a known link

If H₀ were true

HSIC = 0.02
  1. The reference: how HSIC varies across samples when H₀ is true, 999 permutations of y (permutation null on the shared Wichmann–Hill stream).
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .008).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

HSIC=1(n−1)2tr⁡(KHLH)\mathrm{HSIC}=\dfrac{1}{(n-1)^2}\operatorname{tr}(KHLH)

With your numbers

HSIC=0.02,σx=1.02, σy=1.01\mathrm{HSIC} = 0.02,\qquad \sigma_x = 1.02,\ \sigma_y = 1.01
K, LK,\ L
Gaussian kernel matrices of x and y
σ\sigma
median-distance bandwidths
HH
the centering matrix

The verdict

HSIC = 0.022, p = .008

p ≤ α = .050: reject H₀

A kernel measure of dependence: zero when the joint distribution factorizes. The permutation p redraws y's order on the shared stream.

Statistic
0.022
p-value
0.0080
Hilbert–Schmidt independence criterion test on 30 (x, y) pairs: HSIC = 0.022, p = .008. The p-value is below α = .050, so the data are hard to reconcile with H0.

Hilbert–Schmidt independence criterion test on 30 (x, y) pairs: HSIC = 0.022, p = .008. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: X and Y are independent.

Assumptions

Independent paired observations; bounded or sufficiently integrable kernels. Characteristic kernels are needed for broad consistency against all dependence.

Formula

HSIC^=(n−1)−2tr⁡(KHLH)\widehat{HSIC}=(n-1)^{-2}\operatorname{tr}(KHLH)

Parameters and notation

Kij=k(Xi,Xj), Lij=ℓ(Yi,Yj), H=I−11ᵀ/n. Gaussian bandwidths and other kernel parameters must be stated.

Structure of the method

Compute one Gram matrix per variable, center both, and form their normalized trace product. Under independence the ordinary HSIC null is degenerate, so permutation calibration or a validated null approximation is required rather than an unqualified normal z reference.

Estimated and nuisance parameters

The centered empirical kernel cross-covariance is estimated. Kernel bandwidths may be fixed by design or selected by a rule; they are not ordinary regression coefficients. Characteristic kernels support broad detection of dependence, while a linear kernel targets much more limited second-order structure.

Settings and design choices

Prespecify kernel families, bandwidth selection, normalization, statistic variant, and calibration budget. A bandwidth chosen from each marginal sample alone can be permutation-invariant; a bandwidth optimized using observed pairwise dependence must be reselected inside permutations or chosen on independent data.

Fitting and adaptation

For new data, rebuild kernels and rerun the selected calibration. With fixed or permutation-invariant kernels, one centered Gram matrix can be permuted in both row and column indices. End-to-end learned features require sample splitting or full selection-aware recalibration; neural fine-tuning is not part of vanilla HSIC.

Sensitivity and robustness

Extreme bandwidths make kernels nearly constant or nearly diagonal and can destroy power. Ties, small samples, high dimensions, and dependence among rows affect behavior. A finite Monte Carlo p-value should use a valid counting convention and report its resolution rather than zero.

How to use and calibrate

Use a permutation upper tail or a documented null approximation. Unbiased HSIC uses a different U-statistic formula.

Example application

Detect nonlinear dependence between two sets of features.

Interpretation and pitfalls

Watch out

Kernel tuning on the same data can alter calibration; linear kernels primarily detect covariance. HSIC has a degenerate null distribution, not an ordinary normal law.

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

Biased nonnegative HSIC statistic with Gaussian kernel bandwidth one, fixed in advance; label-permutation calibration is valid under independent paired observations.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
x=rng.normal(size=60);y=x*x+rng.normal(size=60)
K=np.exp(-(x[:,None]-x[None,:])**2/2);L=np.exp(-(y[:,None]-y[None,:])**2/2)
n=len(x);H=np.eye(n)-np.ones((n,n))/n;Kc=H@K@H;Lc=H@L@H
t=np.sum(Kc*Lc)/(n-1)**2;B=999;null=[]
for _ in range(B):
    j=rng.permutation(n);null.append(np.sum(Kc*Lc[np.ix_(j,j)])/(n-1)**2)
print(t,(1+np.sum(np.array(null)>=t))/(B+1))
Output
0.00957146057264246 0.016
R

Validated — Executed successfully in the validation environment · needs Base/standard library

set.seed(2026)
x<-rnorm(60);y<-x^2+rnorm(60)
rbf<-function(x) exp(-outer(x,x,"-")^2/2)
K<-rbf(x);L<-rbf(y);n<-length(x);H<-diag(n)-matrix(1/n,n,n)
Kc<-H%*%K%*%H;Lc<-H%*%L%*%H
T<-sum(Kc*Lc)/(n-1)^2;B<-999
null<-replicate(B,{j<-sample.int(n);sum(Kc*Lc[j,j])/(n-1)^2})
print(c(HSIC=T,p=(1+sum(null>=T))/(B+1)))
Output
       HSIC           p 
0.008962704 0.022000000

Sources

Statistics guide v4 (historical):
R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

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