Source code for spm1d.stats._ci


'''
Confidence intervals.
'''

# Copyright (C) 2026  Todd Pataky


from .. _results.ci import (CIOneSample0D, CIOneSample1D, CIPairedSample0D, CIPairedSample1D,
    CITwoSample0D, CITwoSample1D, CIPairedDifference0D, CIPairedDifference1D,
    CITwoSampleDifference0D, CITwoSampleDifference1D)
from . _t import ttest,ttest2
from . _dec import checkargs, docdims


[docs] @checkargs @docdims('univariate') def ci_onesample(y, alpha=0.05, mu=None): ''' One-sample confidence interval. :Parameters: - *y* --- (J,) array for 0D data or (J,Q) array for 1D data - *alpha* --- Type I error rate. Default 0.05 - *mu* --- Datum as a scalar for 0D data or a scalar or (Q,) array for 1D data :Returns: - A **CIOneSample0D** or **CIOneSample1D** object :Example: The 0D example uses the "morley" dataset from R (100 measurements of the speed of light). >>> import spm1d >>> import jikudata as jd >>> y = jd.R_Morley().y # (100,) array >>> ci = spm1d.stats.ci_onesample( y, 0.05 ) >>> print( ci ) 0D Confidence Interval (95.0%) kind : One sample datum : mean (852.40000) Probability alpha : 0.050 df : (1, 99) zc : 1.98422 Confidence interval hc : 15.67741 ci : (836.72259, 868.07741) The 1D example uses ten simulated responses from Pataky et al. (2015). >>> import spm1d >>> import jikudata as jd >>> from matplotlib import pyplot as plt >>> y = jd.SimulatedPataky2015a().y # (10,101) array >>> ci = spm1d.stats.ci_onesample( y, 0.05 ) >>> ci.plot() >>> plt.show() The example datasets require the jiku-data package: ``pip install "spm1d[data]"``. :Note: - For 1D data this is a confidence region based on the random field theory threshold (not a pointwise interval). ''' _mu = 0 if (mu is None) else mu spmi = ttest(y, _mu).inference(alpha, dirn=0) mean = spmi.fit.b.flatten() mean += _mu s = spmi.fit.mse ** 0.5 hc = spmi.zc * s / y.shape[0]**0.5 # ci height CIClass = CIOneSample1D if spmi.dim==1 else CIOneSample0D return CIClass(spmi, mean, hc, mu)
[docs] @checkargs @docdims('univariate') def ci_pairedsample(yA, yB, alpha=0.05, datum='difference', mu=None): ''' Paired-sample confidence interval. :Parameters: - *yA* --- Condition A data as a (J,) array for 0D data or a (J,Q) array for 1D data - *yB* --- Condition B data as a (J,) array for 0D data or a (J,Q) array for 1D data. Rows must match condition A. - *alpha* --- Type I error rate. Default 0.05 - *datum* --- Interval center: ``difference`` (default) or ``meanA`` - *mu* --- Hypothesized mean difference for ``datum='difference'`` as a scalar for 0D data or a scalar or (Q,) array for 1D data (default 0). Criterion ``'meanB'`` (default) or ``'tailB'`` for ``datum='meanA'``. :Returns: - Confidence interval object :Example: The 0D example uses the "sleep" dataset from R (10 participants and two conditions). >>> import spm1d >>> import jikudata as jd >>> dataset = jd.R_SleepPairedTest() >>> y, A = dataset.y, dataset.x # (20,) arrays of responses and condition labels >>> yA, yB = y[ A == 0 ], y[ A == 1 ] # same participants in the same order >>> ci = spm1d.stats.ci_pairedsample( yA, yB, 0.05 ) >>> print( ci ) 0D Confidence Interval (95.0%) kind : Paired difference datum : mean (-1.58000) Probability alpha : 0.050 df : (1, 9) zc : 2.26216 Confidence interval hc : 0.87989 ci : (-2.45989, -0.70011) The 1D example uses plantar arch angle from Caravaggi et al. (2010) (ten participants and two conditions). >>> import spm1d >>> import jikudata as jd >>> from matplotlib import pyplot as plt >>> dataset = jd.PlantarArchAngle() >>> y, A = dataset.y, dataset.x # (20,101) array and (20,) array >>> yA, yB = y[ A == 0 ], y[ A == 1 ] >>> ci = spm1d.stats.ci_pairedsample( yA, yB, 0.05 ) >>> ci.plot() >>> plt.show() The example datasets require the jiku-data package: ``pip install "spm1d[data]"``. ''' muvalue = 0 if datum=='difference': muvalue = 0 if (mu is None) else mu spmi = ttest(yA-yB, muvalue).inference(alpha, dirn=0) mA,mB = yA.mean(axis=0), yB.mean(axis=0) s = spmi.fit.mse ** 0.5 hc = spmi.zc * s / yA.shape[0]**0.5 # ci height if datum=='difference': CIClass = CIPairedDifference1D if spmi.dim==1 else CIPairedDifference0D ci = CIClass(spmi, mA-mB, hc, mu) else: # "mu" selects the comparison, "meanB" (the default) or "tailB". criterion = 'meanB' if (mu is None) else mu CIClass = CIPairedSample1D if spmi.dim==1 else CIPairedSample0D ci = CIClass(spmi, mA, mB, hc, criterion) return ci
[docs] @checkargs @docdims('univariate') def ci_twosample(yA, yB, alpha=0.05, datum='difference', mu=None): ''' Two-sample confidence interval. :Parameters: - *yA* --- Group A data as a (JA,) array for 0D data or a (JA,Q) array for 1D data - *yB* --- Group B data as a (JB,) array for 0D data or a (JB,Q) array for 1D data. - *alpha* --- Type I error rate. Default 0.05 - *datum* --- ``difference`` (default) or ``meanA`` - *mu* --- Hypothesized mean difference for ``datum='difference'`` as a scalar for 0D data or a scalar or (Q,) array for 1D data (default 0). Criterion ``'meanB'`` (default) or ``'tailB'`` for ``datum='meanA'``. :Returns: - Confidence interval object :Example: The 0D example uses the "sleep" dataset from R (two groups of 10). >>> import spm1d >>> import jikudata as jd >>> dataset = jd.R_Sleep2Test() >>> y, A = dataset.y, dataset.x # (20,) arrays of responses and group labels >>> yA, yB = y[ A == 0 ], y[ A == 1 ] >>> ci = spm1d.stats.ci_twosample( yA, yB, 0.05 ) >>> print( ci ) 0D Confidence Interval (95.0%) kind : Two-sample difference datum : mean (-1.58000) Probability alpha : 0.050 df : (1, 18) zc : 2.10092 Confidence interval hc : 1.78387 ci : (-3.36387, 0.20387) The 1D example uses two simulated groups of six responses. >>> import spm1d >>> import jikudata as jd >>> from matplotlib import pyplot as plt >>> dataset = jd.SimulatedTwoLocalMax() >>> y, A = dataset.y, dataset.x # (12,101) array and (12,) array >>> yA, yB = y[ A == 0 ], y[ A == 1 ] >>> ci = spm1d.stats.ci_twosample( yA, yB, 0.05 ) >>> ci.plot() >>> plt.show() The example datasets require the jiku-data package: ``pip install "spm1d[data]"``. ''' muvalue = 0 if datum=='difference': muvalue = 0 if mu is None else mu spmi = ttest2(yA-muvalue, yB, cov_model='iid').inference(alpha, dirn=0) # pooled variance JA,JB = yA.shape[0], yB.shape[0] mA,mB = spmi.fit.b mA += muvalue s = spmi.fit.mse ** 0.5 hc = spmi.zc * s * (1/JA + 1/JB)**0.5 # ci height if datum == 'difference': CIClass = CITwoSampleDifference1D if spmi.dim==1 else CITwoSampleDifference0D ci = CIClass(spmi, mA-mB, hc, mu) else: criterion = 'meanB' if (mu is None) else mu CIClass = CITwoSample1D if spmi.dim==1 else CITwoSample0D ci = CIClass(spmi, mA, mB, hc, criterion) return ci