Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

two_sample

two_sample

Two-sample permutation test implementations.

This module provides CPU-parallel and GPU-batched implementations of the two-sample permutation test (group permutation test).

Methods:

NameDescription
two_sample_permutation_testTwo-sample permutation test using group label shuffling.

Classes

Methods

two_sample_permutation_test

two_sample_permutation_test(data1: np.ndarray, data2: np.ndarray, *, n_permute: int = 5000, tail: int | str = 2, return_null: bool = False, device: str | None = 'cpu', n_jobs: int = -1, max_gpu_memory_gb: float | None = None, random_state: int | None = None, progress_bar: bool = False) -> dict

Two-sample permutation test using group label shuffling.

Tests whether two independent groups have different means by randomly permuting group labels. This is the permutation test equivalent of an independent samples t-test.

Assumption: Exchangeability under the null hypothesis (group assignments are arbitrary). Valid for independent samples from similar distributions.

Parameters:

NameTypeDescriptionDefault
data1ndarrayGroup 1 data - shape (n_samples1,) for single feature - shape (n_samples1, n_features) for multi-feature (voxel-wise)required
data2ndarrayGroup 2 data - shape (n_samples2,) for single feature - shape (n_samples2, n_features) for multi-feature (voxel-wise)required
n_permuteintNumber of permutations (default: 5000)5000
tailint | strTest type — 2‘two’ (two-tailed, default) or 1
return_nullboolIf True, return full null distribution (default: False)False
devicestrParallelization method (default: ‘cpu’) - None: Single-threaded NumPy (for debugging/small problems) - ‘cpu’: CPU parallelization via joblib (default, 4-8× speedup) - ‘gpu’: GPU acceleration via PyTorch (fastest for large problems)‘cpu’
n_jobsintNumber of CPU cores for parallelization (default: -1 = all cores) Only used when device=‘cpu’-1
max_gpu_memory_gbfloatExplicit GPU memory budget in GB. None (default) measures the device’s available memory. Controls automatic batching to prevent OOM errors. Only used with device=‘gpu’. Larger values allow more permutations per batch but risk OOM on smaller GPUs.None
random_stateintRandom seed for reproducibilityNone

Returns:

NameTypeDescription
dictdictDictionary with keys: - ‘mean_diff’ (float or np.ndarray): Observed mean difference (data1 - data2) - ‘p’ (float or np.ndarray): P-value(s) - ‘null_dist’ (np.ndarray): Null distribution (if return_null=True) - ‘device’ (str): Parallelization method used

Examples:

>>> # Single feature (default CPU parallelization)
>>> data1 = np.random.randn(20)  # Group 1: 20 subjects
>>> data2 = np.random.randn(25)  # Group 2: 25 subjects
>>> result = two_sample_permutation_test(data1, data2, n_permute=5000)
>>> result['p']
0.45
>>> # Voxel-wise test with GPU
>>> data1 = np.random.randn(20, 10000)  # 20 subjects, 10K voxels
>>> data2 = np.random.randn(25, 10000)  # 25 subjects, 10K voxels
>>> result = two_sample_permutation_test(data1, data2, n_permute=5000, device='gpu')
>>> result['mean_diff'].shape
(10000,)
>>> result['p'].shape
(10000,)
>>> # Single-threaded (for debugging)
>>> result = two_sample_permutation_test(data1, data2, n_permute=5000, device=None)
Notes
  • Default (device=‘cpu’): CPU parallelization with joblib (4-8× speedup)

  • GPU parallelization (‘gpu’): Fastest for large problems with automatic batching

  • Single-threaded (device=None): Use for small problems or debugging

  • For voxel-wise tests, each voxel tested independently

  • Group sizes can be unequal