How many permutations?

How many permutations?#

Answer: In most cases the default number of permutations is appropriate. By default spm1d uses all possible permutations when there are 10,000 or fewer and 10,000 randomly selected permutations otherwise. Fewer permutations (e.g., nperm=1000) execute faster and can be used for preliminary analyses. Final results should generally use at least 10,000 permutations or all possible permutations (nperm=-1). More generally, it is good practice to vary both nperm and the random number generator state via np.random.seed and to examine the numerical stability of the results. If changes to nperm and np.random.seed do not qualitatively affect results then the results may be regarded as sufficiently stable.

Justification: The precision of a permutation p-value is approximately \(\sqrt{p(1-p)/n}\), where n is the number of permutations. For p = 0.05 this corresponds to a standard error of about 0.002 for 10,000 permutations and about 0.007 for 1,000 permutations.

The example below uses plantar arch angles from Caravaggi et al. (2010) that were measured on ten participants during normal-speed and fast walking. For a paired t test with ten participants there are 1024 possible permutations. The exact permutation threshold can therefore also be calculated using all of them (nperm=-1).

Refer to Continuum-level inference for more details.

import numpy as np
from matplotlib import pyplot as plt
import spm1d
import jikudata as jd
plt.rc( 'font', size=18 )             # minimum font size for all figure text

dataset = jd.PlantarArchAngle()      # 1D dataset:  two conditions, ten participants
y, A    = dataset.y, dataset.x       # (20,101) array of observations, (20,) array of condition labels
t       = spm1d.stats.ttest_paired( y[A == 1], y[A == 0] )
ti_all  = t.inference( 0.05, method='perm', nperm=-1 )
zc_all  = ti_all.zc  # exact threshold, using all 1024 permutations
print( ti_all )

np.random.seed( 0 )                  # the permutations are random;  seeding makes the figure reproducible
NPERM   = [50, 100, 200, 500, 1000]
zc      = np.array( [[t.inference( 0.05, method='perm', nperm=n ).zc  for _ in range( 5 )]  for n in NPERM] )   # five repetitions for each nperm value
SnPM{t} (1D) inference
    fn_name : ttest_paired
    STAT    : T
    z       : (101,) array
    df      : (1, 9)
Smoothness estimates:
    fwhm    : 23.819
    lkc     : 6.991
    resels  : (1, 4.19835)
Inference results:
    method       : perm
    isparametric : False
    alpha        : 0.05
    dirn         : 0
    zc           : 3.59797
    zmax         : 6.552
    h0reject     : True
    p_zmax       : 0.004
    p_set        : 0.029
    p_cluster    : [0.035]
    p_point      : (101,) array
    nperm        : all (1024)
    nuperm       : 1024
    Z            : (1024,) array
    Z2           : (1024,) array


The figure below depicts the critical thresholds from five repetitions at several nperm values, their means, and the exact threshold calculated from all permutations. Generally larger nperm values yield more stable results.

plt.figure( figsize=(6.4, 4.5), tight_layout=True )
ax = plt.gca()
for i, n in enumerate( NPERM ):
    h0 = ax.plot( [n] * zc.shape[1], zc[i], 'o', color='0.6', ms=8 )[0]
h1 = ax.plot( NPERM, zc.mean( axis=1 ), '-o', color='#0e6e84', lw=3, ms=8 )[0]
h2 = ax.axhline( zc_all, color='C3', lw=2.5, ls='--' )
ax.set_xscale( 'log' )
ax.set_xlabel( 'nperm', fontsize=20 )
ax.set_ylabel( 'Critical threshold', fontsize=20 )
ax.legend( [h1, h2], ['Mean', 'All permutations'], fontsize=20, ncol=2, loc='lower center', bbox_to_anchor=(0.5, 1), handlelength=1.5 )   # above the axes
nperm
nperm


Attempting to use more permutations than exist results in an SPM1DError. For small samples the number of unique permutations is small and the permutation distribution is therefore coarse. For example, a two-sample test with three observations per group has only 20 unique permutations and the smallest possible p-value is therefore 0.05.

SPM1DError: Number of specified permutations (5000) exceeds the total
number of possible permutations (1024).