Skip to content

n_permutations="all" in the cluster permutation tests does not run the full exact test #14479

Closed

Description

@ChenziqiAdam

Description of the problem

For "all" the number of permutations is set to the number of non-identity sign flips and then the code draws that many minus one of them at random, so one flip pattern is left out and the surrogate distribution is one entry short. Passing an integer larger than the number of possible flips enumerates all of them correctly.

Steps to reproduce

import numpy as np
from mne.stats import permutation_cluster_1samp_test as test

X = np.random.RandomState(0).randn(8, 1) + 0.4   # 8 subjects, one time point
kw = dict(tail=0, threshold=0.5, out_type="mask", seed=0, verbose=False)

_, _, p_all, h0_all = test(X, n_permutations="all", **kw)
_, _, p_int, h0_int = test(X, n_permutations=10000, **kw)
print(len(h0_all), p_all)   # 127 [0.02362205]
print(len(h0_int), p_int)   # 128 [0.0234375]

Link to data

No response

Expected results

For a two-tailed test on 8 subjects there are 2**(8 - 1) = 128 distinct sign patterns, so the exact test has 128 surrogate values and p-values that are multiples of 1/128 (here 3/128 = 0.0234375).

Actual results

n_permutations="all" returns 127 surrogate values and 3/127 = 0.02362. The result is not the exact permutation distribution the documentation promises.

Additional information

I used AI to help me locate this may be due to mne/stats/cluster_level.py, _get_1samp_orders:

max_perms = 2 ** (n_samples - (tail == 0)) - 1
if isinstance(n_permutations, str):
    ...
    n_permutations = max_perms        # "all"
n_permutations = int(n_permutations)
if max_perms < n_permutations:        # False for "all"
    orders = bin_perm_rep(n_samples)[1 : max_perms + 1]   # exact enumeration
elif n_samples <= 20:
    orders = rng.choice(max_perms, n_permutations - 1, replace=False)  # random draw, one pattern fewer

Activity

  1. larsoner commented on Nov 2, 2026

    @larsoner
    Member

    I'll take a look, this sounds like we need to include the original permutation in H0 but not actually recompute it...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions