[11] Tests of Independence#

pysht.independence provides permutation tests for paired observations of random vectors. Every input block uses rows for the same observational units; different blocks may have different feature dimensions.

Exact marginal-permutation inference is invariant to Euclidean isometries. Fixed-seed Monte Carlo replay also holds when metric canonicalization completes within its fixed work budget and distance ranks are separated from their floating equality boundary. A boundary case or symmetric geometry that exhausts this budget can select a different, but still uniformly valid, permutation plan after a rotation; use exact mode for transformation replay when its orbit is tractable.

Function

Null hypothesis

Primary statistic

distance_covariance

\(X\) and \(Y\) are independent

\(nV_n^2\) in raw distance units

hsic

\(X\) and \(Y\) are independent

biased \(\mathrm{HSIC}_b\)

dhsic

all supplied blocks are mutually independent

\(n\widehat{\mathrm{dHSIC}}_n\)

distance_multivariance

all supplied blocks are mutually independent

normalized total \(n\overline M_n^2\)

Pairwise independence is not mutual independence. In particular, dhsic and distance_multivariance can detect higher-order alternatives such as \(Z=X\mathbin{\mathrm{xor}}Y\), even though all three pairs are independent.

Kernel methods accept only built-in RBF and Laplacian kernels. hsic exposes separate kernel_x, kernel_y, bandwidth_x, and bandwidth_y controls. dhsic accepts either one value broadcast to every block or a tuple with one value per block. All Gram matrices and median bandwidths are fixed before permuting marginal rows.

import numpy as np
from pysht import independence

rng = np.random.default_rng(2026)
x = rng.normal(size=(40, 2))
y = x**2 + 0.25 * rng.normal(size=(40, 2))

result = independence.hsic(x, y, n_resamples=999, rng=17)
result.pvalue

See the independence validation ledger for formula reductions, randomization semantics, numerical normalization, and calibration evidence.

Functions#

Nonparametric tests of pairwise and mutual independence.

pysht.independence.dhsic(*samples, kernel='rbf', bandwidth='median', calibration='permutation', n_resamples=9_999, rng=None)[source]#

Test mutual independence of two or more random vectors using dHSIC.

Following the estimator’s defining finite-sample regime, n >= 2*d is required for d marginals. For two marginals, this function reports n times the hsic() statistic; that positive scaling gives exactly the same permutation ordering, exceedance count, and p-value.

References

Pfister, N., Bühlmann, P., Schölkopf, B. and Peters, J. (2018). Kernel-based tests for joint independence. Journal of the Royal Statistical Society B, 80, 5–31. https://doi.org/10.1111/rssb.12235

Parameters:
  • samples (ArrayLike)

  • kernel (_KernelControls)

  • bandwidth (_BandwidthControls)

  • calibration (Literal['permutation', 'exact', 'monte-carlo'])

  • n_resamples (int)

  • rng (int | integer | Generator | None)

Return type:

ResamplingTestResult

pysht.independence.distance_covariance(x, y, *, calibration='permutation', n_resamples=9_999, rng=None)[source]#

Test pairwise independence using distance covariance.

Rows of x and y are paired observations; the feature dimensions may differ. The reported statistic is n V_n^2 in the original distance units. Calibration uses the algebraically equivalent statistic obtained by dividing each doubly centered distance matrix by its mean interpoint distance. This positive normalization improves numerical stability without changing permutation ordering. Distance covariance, distance correlation, and both distance variances are also returned when representable.

References

Székely, G. J., Rizzo, M. L. and Bakirov, N. K. (2007). Measuring and testing dependence by correlation of distances. Annals of Statistics, 35, 2769–2794. https://doi.org/10.1214/009053607000000505

Parameters:
  • x (ArrayLike)

  • y (ArrayLike)

  • calibration (Literal['permutation', 'exact', 'monte-carlo'])

  • n_resamples (int)

  • rng (int | integer | Generator | None)

Return type:

ResamplingTestResult

pysht.independence.distance_multivariance(*samples, calibration='permutation', n_resamples=9_999, rng=None)[source]#

Test mutual independence using normalized total distance multivariance.

Unlike a collection of pairwise tests, total distance multivariance can detect higher-order dependence whose every pair is independent. The statistic is the normalized total version in Eq. 4.29 of the primary paper, using Euclidean distances and a separate mean-distance normalization for every marginal.

References

Böttcher, B., Keller-Ressel, M. and Schilling, R. L. (2019). Distance multivariance: New dependence measures for random vectors. Annals of Statistics, 47, 2757–2789. https://doi.org/10.1214/18-AOS1764

Parameters:
  • samples (ArrayLike)

  • calibration (Literal['permutation', 'exact', 'monte-carlo'])

  • n_resamples (int)

  • rng (int | integer | Generator | None)

Return type:

ResamplingTestResult

pysht.independence.hsic(x, y, *, kernel_x='rbf', kernel_y='rbf', bandwidth_x='median', bandwidth_y='median', calibration='permutation', n_resamples=9_999, rng=None)[source]#

Test pairwise independence with the biased empirical HSIC.

kernel_x and kernel_y independently select a fixed RBF or Laplacian Gram matrix. A median bandwidth is estimated within the corresponding marginal before any permutation; alternatively, each marginal accepts its own explicit positive scalar bandwidth.

References

Gretton, A., Fukumizu, K., Teo, C. H., Song, L., Schölkopf, B. and Smola, A. (2008). A kernel statistical test of independence. Advances in Neural Information Processing Systems 20, 585–592.

Parameters:
  • x (ArrayLike)

  • y (ArrayLike)

  • kernel_x (_KernelName)

  • kernel_y (_KernelName)

  • bandwidth_x (_BandwidthControl)

  • bandwidth_y (_BandwidthControl)

  • calibration (Literal['permutation', 'exact', 'monte-carlo'])

  • n_resamples (int)

  • rng (int | integer | Generator | None)

Return type:

ResamplingTestResult