Skip to contents

Computes the weighted Hilbert–Schmidt Independence Criterion (HSIC) between two pre-computed kernel matrices, $$\frac{1}{W^2} \sum_{i,j} w_i w_j (K_x^c)_{ij} (K_y^c)_{ij},$$ where K_x^c and K_y^c are weight-centred and \(W = \sum_i w_i\). This is the exact statistic bd_hsic_test() accumulates over its permutation null, exposed so that downstream methods – for example the mechanism-consistency test – can build a bd-HSIC-compatible statistic against a custom reference distribution without re-implementing the engine.

Usage

weighted_hsic_stat(Kx, Ky, w = rep(1, nrow(Kx)))

Arguments

Kx

Numeric n by n kernel matrix for the first variable.

Ky

Numeric n by n kernel matrix for the second variable, with the same dimensions as Kx.

w

Non-negative numeric weight vector of length n. Defaults to uniform weights, giving the unweighted HSIC.

Value

A single numeric value: the weighted HSIC statistic.

Details

With uniform weights this reduces to the (biased) HSIC estimator; supplying density-ratio weights \(w(t, z)\) yields the backdoor-adjusted statistic used for causal testing.

See also

bd_hsic_test() for the full causal test that uses this statistic; resolve_bandwidth() and kernel_matrix() for building the kernel inputs.

Other kernel primitives: kernel_matrix(), kernel_spec(), resolve_bandwidth(), select_bandwidth()

Examples

set.seed(1)
x <- matrix(rnorm(40), ncol = 2)
y <- matrix(rnorm(40), ncol = 2)
Kx <- kernel_matrix(x, kernel = resolve_bandwidth(kernel_spec(), x))
Ky <- kernel_matrix(y, kernel = resolve_bandwidth(kernel_spec(), y))
weighted_hsic_stat(Kx, Ky)
#> [1] 0.007846936