Source code for spm1d.prob._param


'''
Parametric (0D) probabilities.
'''

# Copyright (C) 2026  Todd Pataky

import numpy as np
from . _results import InferenceResults0D
from . _general import p_corrected_dirn


def isf_sf_t(z, df, alpha=0.05, dirn=0):
    import rft1d
    a      = 0.5 * alpha if (dirn==0) else alpha
    zc     = rft1d.t.isf0d( a, df )
    zz     = np.abs(z) if (dirn==0) else dirn*z
    p      = rft1d.t.sf0d( zz, df )
    p      = p_corrected_dirn(p, dirn)
    return zc,p


[docs] def param(STAT, z, df, alpha=0.05, dirn=0, zc=None): ''' Parametric inference on a 0D test statistic. :Parameters: - *STAT* --- test statistic: ``'T'``, ``'F'``, ``'T2'`` or ``'X2'`` - *z* --- test statistic value - *df* --- degrees of freedom as a two-element tuple - *alpha* --- Type I error rate (default 0.05) - *dirn* --- inference direction: 0 (two-tailed), +1 or -1 (one-tailed) - *zc* --- critical value replacing the ``alpha``-derived value :Returns: - **InferenceResults0D** object :Example: Parametric inference on the t statistic of a paired t test on the "sleep" dataset from R. >>> import spm1d >>> import jikudata as jd >>> dataset = jd.R_SleepPairedTest() >>> y, A = dataset.y, dataset.x # (20,) arrays of responses and condition labels >>> t = spm1d.stats.ttest_paired( y[ A == 0 ], y[ A == 1 ] ) >>> results = spm1d.prob.param( 'T', t.z, t.df, alpha=0.05, dirn=0 ) >>> print( round( results.zc, 3 ), round( results.p, 3 ) ) 2.262 0.003 The example datasets require the jiku-data package: ``pip install "spm1d[data]"``. ''' if isinstance(dirn, bool) or (dirn not in (-1, 0, 1)): from .. exceptions import SPM1DError raise SPM1DError( f'"dirn" must be -1, 0 or 1, not {dirn!r}.' ) if STAT != 'T': dirn = 1 import rft1d if STAT=='T': v = df[1] if isinstance(df, (list,tuple,np.ndarray)) else df zc0,p = isf_sf_t(z, v, alpha, dirn=dirn) elif STAT=='F': zc0 = rft1d.f.isf0d( alpha, df ) p = rft1d.f.sf0d( z, df ) elif STAT=='T2': zc0 = rft1d.T2.isf0d( alpha, df ) p = rft1d.T2.sf0d( z, df ) elif STAT=='X2': v = df[1] if isinstance(df, (list,tuple,np.ndarray)) else df zc0 = rft1d.chi2.isf0d( alpha, v ) p = rft1d.chi2.sf0d( z, v ) else: from .. exceptions import SPM1DError raise SPM1DError( f'Unknown statistic: {STAT}. Must be one of: ["T", "F", "T2", "X2"]' ) zc = zc0 if (zc is None) else float(zc) results = InferenceResults0D( 'param', STAT, z, alpha, df, dirn, zc, p, isparametric=True ) return results