Source code for spm1d.prob._general


'''
General probability utilities.
'''

# Copyright (C) 2026  Todd Pataky

import numpy as np


[docs] def p_corrected_bonf(p, n): ''' Bonferroni-corrected *p* value min(1, n * p). :Parameters: - *p* --- probability value computed from one of multiple tests - *n* --- number of tests :Returns: - Bonferroni-corrected *p* value :Example: >>> p = spm1d.prob.p_corrected_bonf(0.03, 8) # yields p = 0.24 ''' if p<=0: return 0.0 elif p>=1: return 1.0 else: return float( min(1.0, n * p) )
[docs] def p_corrected_dirn(p, dirn): ''' Corrected p values based on dirn (t-like tests only). :Parameters: - *p* --- probability value computed from one of multiple tests - *dirn* --- test direction: 0 (two-tailed), 1 or -1 (one-tailed) :Returns: - Corrected *p* value(s) :Example: >>> p = spm1d.prob.p_corrected_dirn(0.03, 0) # yields p = 0.06 ''' if isinstance(p, float): p = float( min(1, 2*p) if (dirn==0) else p ) else: p = np.minimum(2*p, 1) if (dirn==0) else p return p
[docs] def p_critical_bonf(alpha, n): ''' Bonferroni-corrected critical Type I error rate, alpha / n. :Parameters: - *alpha* --- original Type I error rate (usually 0.05) - *n* --- number of tests :Returns: - Bonferroni-corrected critical *p* value :Example: >>> p = spm1d.prob.p_critical_bonf(0.05, 20) # yields p = 0.0025 ''' if alpha<=0: return 0.0 elif alpha>=1: return 1.0 else: return float( alpha / n )
def z_corrected_dirn(z, dirn): ''' Test statistic values sign-corrected according to dirn (t-like tests only). ''' if dirn==0: z = np.abs(z) elif dirn==-1: z = -z return z def p_point_in_region(p, z): ''' Point-level p values restricted to the search region. ''' if np.ma.is_masked(z): return np.ma.array( np.asarray( np.ma.getdata(p) ), mask=np.ma.getmaskarray(z) ) return p