andrey.core.ci_kernel.KCI#
- class andrey.core.ci_kernel.KCI(data, *, ridge=0.001, rank=None)[source]#
Kernel conditional-independence test (KCI) with the gamma approximation of its null.
Each query compares Gaussian-kernel features of
xandyafter kernel ridge regression on the conditioning set removes what it explains. The statistic is matched to a gamma law through its null mean and variance, which gives the p-value in closed form.- Parameters:
data (
ndarray) – Observed data, one row per sample. Each column is standardized to unit variance.ridge (
float) – Regularization of the kernel ridge regression on the conditioning set. Smaller values fit the conditioning set more closely.rank (
int|None) – With a conditioning set, replace each kernel matrix by a pivoted incomplete Cholesky factorization of at most this many columns, so a query’s time grows with the square of the number of rows instead of its cube. The p-value then approximates the exact one.Nonecomputes the exact kernels.
- Raises:
ValueError – If
datais not 2-D, has fewer than 2 rows, contains NaN or infinity, or has values too large or too small to standardize,ridgeis not positive and finite, orrankis below 1.
Notes
How it works. With
Kxthe centered Gaussian kernel matrix of a variable andnrows, an empty conditioning set givesT = tr(Kx Ky) / n, with null meantr(Kx) tr(Ky) / n^2and variance2 tr(Kx^2) tr(Ky^2) / n^4: the gamma HSIC test ofhsic_test(). A conditioning setSjoinsxandSinto one variablexsand residualizes withR = ridge (Ks + ridge I)^-1:A = R Kxs R,B = R Ky R,T = tr(A B) / n. Under the null,Tis a weighted sum of chi-squared variables whose meansum_t A_tt B_tt / nand variance2 sum_st (A_st B_st)^2 / n^2come from the elementwise product ofAandB. The gamma law with that mean and variance gives the p-value [1].Every kernel is Gaussian,
exp(-|a - b|^2 / (2 w^2)), on standardized columns. Without a conditioning set,wis 0.8 below 200 rows, 0.5 below 1,200, and 0.3 beyond. Withkconditioning variables, the kernels ofxs,y, andSsharew = c sqrt(k),c1.2 below 200 rows, 0.7 below 1,200, and 0.4 beyond, andSentersxsat half scale.xjoins the conditioning set andydoes not, so of the two variables the one with the lowertie_ranks()rank, a rank computed from each column’s own data, takes the role ofx, and the conditioning set’s columns enter its kernels in rank order: the p-value depends on neither the argument order nor the column order.With
rank, each kernel of a conditional query is a pivoted incomplete Cholesky factorLwithK ~ L L'[2], centered, andR = I - Ls (Ls' Ls + ridge I)^-1 Ls', soAandBare products ofn x rankfactors and non x nsystem is solved. The factorization stops beforerankcolumns whenL L'reproduces the kernel to rounding.An exact query with a conditioning set runs on a CUDA GPU, in float64, under the
cudabackend, or underautofromANDREY_KCI_GPU_THRESHOLDrows squared. Its p-value then matches the CPU’s to rounding.Limitations. Measured in PC on simulated data from sparse random graphs (mean degree 2), each variable a weighted sum of
z + sin(z), or ofcos(z), over its parents plus Gaussian noise, with 20 datasets per size on 4 cores. From 5 to 20 variables with 500 to 2,000 rows, and at 5 variables with 5,000 rows, its median errors (SHD) stayed below the empty graph’s at every size. The median fit took 4 to 18 seconds at 500 rows, 37 seconds to 11 minutes at 2,000 rows, and 13 minutes at 5 variables with 5,000 rows; at 20 variables and 2,000 rows, on shared nodes, 4 of 40 fits passed a half-hour cap. Each query with a conditioning set costs two products ofn x nmatrices, plus an eigendecomposition for a conditioning set not seen before and two more products for a newywith it; the test keeps up to 256 MB of each kind of matrix, at least one. So time grows as the cube of the number of rows and memory as its square: thousands of rows are practical, tens of thousands are not. The kernel widths follow the row count and the size of the conditioning set, not the data. On simulated draws with up to three conditioning variables and 500 rows, it rejected a true independence at rates betweenalphaand twicealpha; a test that rejects too often makes PC and FCI keep edges.References