MMD Utilities

polygraph.utils.mmd_utils.mmd_from_gram(kxx, kyy, kxy, variant)

Computes MMD statistic from kernel matrices.

Computes the Maximum Mean Discrepancy between two samples using pre-computed kernel matrices. Three estimator variants are available:

  • 'biased': Standard biased V-statistic
  • 'umve': Unbiased minimum variance estimator
  • 'ustat': Unbiased U-statistic (requires equal sample sizes)
Parameters:
  • kxx (ndarray) –

    Kernel matrix between first sample points (n×n)

  • kyy (ndarray) –

    Kernel matrix between second sample points (m×m)

  • kxy (ndarray) –

    Kernel matrix between first and second samples (n×m)

  • variant (Literal['biased', 'umve', 'ustat']) –

    Which MMD estimator to use

Returns:
  • Union[float, ndarray]

    MMD value(s). If kernel matrices have an extra dimension for multiple kernels,

  • Union[float, ndarray]

    returns one MMD value per kernel.

Raises:
  • RuntimeError

    If variant='ustat' but sample sizes are different

  • ValueError

    If variant is not one of the supported options

polygraph.utils.mmd_utils.full_gram_from_blocks(kxx, kxy, kyy)

Combines kernel block matrices into a single kernel matrix.

Takes separate kernel matrices for within-sample and between-sample comparisons and combines them into a single symmetric kernel matrix for all points. Useful for computing MMDs in permutation tests.

Parameters:
  • kxx (ndarray) –

    Kernel matrix between first sample points (n×n)

  • kxy (ndarray) –

    Kernel matrix between first and second samples (n×m)

  • kyy (ndarray) –

    Kernel matrix between second sample points (m×m)

Returns:
  • ndarray

    Combined kernel matrix of shape ((n+m)×(n+m))

Note

Input matrices (and output matrix) can have an extra dimension for multiple kernels.