Source code for spm1d.stats._mv.hotellings

'''
Hotelling's multivariate one-, paired- and two-sample tests.
'''

# Copyright (C) 2026  Todd Pataky


import numpy as np

from .. _glm.cov import resolve_cov_model
from .. _dec import appendargs, checkargs, docdims
from . _mvfit import run_glm


def _assemble(design, y, roi=None, _fwhm_method='taylor2008'):
    '''
    Hotelling's T2 test through the multivariate GLM engine.
    '''
    return run_glm( design, y, design.contrasts[0].c, roi=roi, _fwhm_method=_fwhm_method, stat='T2' )


[docs] @appendargs @checkargs @docdims('multivariate') def hotellings(y, mu=None, roi=None, _fwhm_method='taylor2008'): ''' One-sample Hotelling's T2 test. :Parameters: - *y* --- (J,I) array for 0D data or (J,Q,I) array for 1D data - *mu* --- Datum as a scalar or (I,) array for 0D data or a scalar or (Q,I) array for 1D data (default zero) - *roi* --- DEPRECATED (use ``inference``) :Returns: - **SPM0D** or **SPM1D** object :Example: The 0D example uses the setosa flowers of the "iris" dataset from R (50 observations and four components). >>> import spm1d >>> import jikudata as jd >>> y = jd.R_IrisSetosa().y # (50,4) array >>> mu = [5.0, 3.4, 1.5, 0.2] >>> spmi = spm1d.stats.hotellings( y, mu ).inference( 0.05 ) >>> print( spmi ) SPM{T2} (0D) inference fn_name : hotellings STAT : T2 z : 17.17196 df : (4, 49) Inference results: method : param isparametric : True alpha : 0.05 zc : 10.96763 h0reject : True p : 0.00695 The example datasets require the jiku-data package: ``pip install "spm1d[data]"``. ''' from .. _glm.design.designs_mv import HOTELLINGS design = HOTELLINGS( _nobs(y) ) y = design.validate( y ) if mu is not None: y = y - mu return _assemble( design, y, roi, _fwhm_method )
[docs] @appendargs @checkargs @docdims('multivariate') def hotellings_paired(y0, y1, roi=None, _fwhm_method='taylor2008'): ''' Paired Hotelling's T2 test. :Parameters: - *y0* --- Condition A data as a (J,I) array for 0D data or a (J,Q,I) array for 1D data - *y1* --- Condition B data as a (J,I) array for 0D data or a (J,Q,I) array for 1D data. Rows must match condition A. - *roi* --- DEPRECATED (use ``inference``) :Returns: - **SPM0D** or **SPM1D** object :Example: The 0D example uses a generated paired dataset (20 pairs and three components). >>> import spm1d >>> import jikudata as jd >>> dataset = jd.Gen0D_hotellings_paired() >>> y, A = dataset.y, dataset.x # (40,3) array and (40,) array >>> y0, y1 = y[ A == 0 ], y[ A == 1 ] # same participants in the same order >>> spmi = spm1d.stats.hotellings_paired( y0, y1 ).inference( 0.05 ) >>> print( spmi ) SPM{T2} (0D) inference fn_name : hotellings_paired STAT : T2 z : 8.15846 df : (3, 19) Inference results: method : param isparametric : True alpha : 0.05 zc : 10.71860 h0reject : False p : 0.10040 The 1D example uses center-of-pressure trajectories from Pataky et al. (2014) (ten participants, two conditions and two components). >>> import spm1d >>> import jikudata as jd >>> from matplotlib import pyplot as plt >>> dataset = jd.Pataky2014cop() >>> y, A = dataset.y, dataset.x # (20,101,2) array and (20,) array >>> y0, y1 = y[ A == 0 ], y[ A == 1 ] >>> spmi = spm1d.stats.hotellings_paired( y0, y1 ).inference( 0.05 ) >>> print( spmi.h0reject ) True >>> spmi.plot() >>> plt.show() The example datasets require the jiku-data package: ``pip install "spm1d[data]"``. ''' from .. _glm.design.designs_mv import HOTELLINGS_PAIRED from ... exceptions import SPM1DError y0, y1 = np.asarray(y0), np.asarray(y1) if y0.shape != y1.shape: raise SPM1DError( f'"hotellings_paired" needs the two samples to have the ' f'same shape; the given samples have shapes {y0.shape} and {y1.shape}.' ) design = HOTELLINGS_PAIRED( _nobs(y0) ) y = design.validate( y0 - y1 ) return _assemble( design, y, roi, _fwhm_method )
[docs] @appendargs @checkargs @docdims('multivariate') def hotellings2(y0, y1, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2008'): ''' Two-sample Hotelling's T2 test. :Parameters: - *y0* --- Group A data as a (J0,I) array for 0D data or a (J0,Q,I) array for 1D data - *y1* --- Group B data as a (J1,I) array for 0D data or a (J1,Q,I) array for 1D data. - *roi* --- DEPRECATED (use ``inference``) - *cov_model* --- ``iid`` only (the default) - *equal_var* --- DEPRECATED (use ``cov_model``) :Returns: - **SPM0D** or **SPM1D** object :Example: The 0D example uses the "crabs" dataset from the R package MASS (two groups of 100 and five components). >>> import spm1d >>> import jikudata as jd >>> import warnings >>> warnings.filterwarnings( "ignore", category=spm1d.exceptions.SPM1DDimensionWarning ) >>> dataset = jd.MASS_Crabs() >>> y, A = dataset.y, dataset.x # (200,5) array and (200,) array >>> y0, y1 = y[ A == 0 ], y[ A == 1 ] >>> spmi = spm1d.stats.hotellings2( y0, y1 ).inference( 0.05 ) >>> print( spmi ) SPM{T2} (0D) inference fn_name : hotellings2 STAT : T2 z : 1363.65777 df : (5, 198) Inference results: method : param isparametric : True alpha : 0.05 zc : 11.53629 h0reject : True p : <0.00001 The 1D example uses joint angles during walking from Fukuchi et al. (2018) (two groups and three components). >>> import spm1d >>> import jikudata as jd >>> from matplotlib import pyplot as plt >>> dataset = jd.Fukuchi2018walking() >>> y, A = dataset.y, dataset.x # (51,101,3) array and (51,) array >>> y0, y1 = y[ A == 0 ], y[ A == 1 ] >>> spmi = spm1d.stats.hotellings2( y0, y1 ).inference( 0.05 ) >>> print( spmi.h0reject ) False >>> spmi.plot() >>> plt.show() The example datasets require the jiku-data package: ``pip install "spm1d[data]"``. :Note: - Covariance is pooled across samples. Requires ``cov_model='iid'``. - Post hoc component tests and loadings are signed first minus second (y0 - y1). ''' from .. _glm.design.designs_mv import HOTELLINGS2 from ... exceptions import SPM1DError cov_model = resolve_cov_model( cov_model, equal_var, default='iid' ) if cov_model != 'iid': raise SPM1DError( 'The non-sphericity correction is not implemented for ' '"hotellings2". Set ' 'cov_model="iid".' ) y0, y1 = np.asarray(y0), np.asarray(y1) design = HOTELLINGS2( _nobs(y0), _nobs(y1) ) y = design.validate( np.concatenate([y0, y1], axis=0) ) return _assemble( design, y, roi, _fwhm_method )
def _nobs(y): a = np.asarray(y) return a.shape[0] if a.ndim else 0