Skip to content

Conditional independence tests

Two tests are bundled. Both implement the CITest protocol, so you can also write your own and pass it as ci_test= to CDANs.

Picking a test

Test Cost Assumes Use when
FisherZ O(n) linear, Gaussian data is approximately linear-Gaussian
KCITest O(n³) none dependencies may be nonlinear; surrogate-dependence check

Pick by name (ci_test="fisherz" or "kci") or by passing an instance for custom configuration.

CITest

Bases: Protocol

Protocol that every conditional-independence test must implement.

pvalue

pvalue(x: ndarray, y: ndarray, z: ndarray | None = None) -> float

Return the p-value of testing X ⊥ Y | Z.

Parameters:

Name Type Description Default
x ndarray

(n_samples,) or (n_samples, d_x) array.

required
y ndarray

(n_samples,) or (n_samples, d_y) array.

required
z ndarray | None

(n_samples, d_z) array of conditioning variables, or None for an unconditional test.

None

Returns:

Type Description
float

p-value in [0, 1]. Larger values mean stronger evidence for independence.

FisherZ

Fisher's Z conditional independence test (partial correlation based).

KCITest

KCITest(
    width_heuristic: str = "empirical",
    width: float | None = None,
    epsilon: float = 0.001,
)

Kernel-based Conditional Independence test (Zhang et al., 2011).

Suitable for both linear and nonlinear, Gaussian and non-Gaussian relationships. Substantially slower than :class:FisherZO(n^3) per call — so prefer FisherZ for linear-Gaussian benchmarks and reach for KCI when the residual structure is plausibly nonlinear.

Parameters:

Name Type Description Default
width_heuristic str

How to set the Gaussian kernel bandwidth. "empirical" (default) uses the sample-size-dependent constants from the reference KCI implementation. "median" uses the median pairwise-distance heuristic. "manual" requires width to be passed.

'empirical'
width float | None

Manual precision parameter 1 / sigma^2. Used only when width_heuristic="manual".

None
epsilon float

Regularization for the conditional residualization step. 1e-3 matches the reference.

0.001

get_ci_test

get_ci_test(name: str | CITest, **kwargs) -> CITest

Resolve a CI test by name, or return one already given.

Parameters:

Name Type Description Default
name str | CITest

Either a string ("fisherz" or "kci") or an object that already implements the :class:CITest protocol.

required
**kwargs

Forwarded to the test constructor when name is a string.

{}

Returns:

Type Description
CITest

A ready-to-use CI test instance.