Two-sample variance test

Contents

Two-sample variance test#

spm1d.stats.eqvartest tests whether two groups of 1D data have equal variance. The test statistic is the ratio of the two sample variances at each domain node (an F statistic). Its critical threshold is calculated using random field theory. This test was reported by Kowalski et al. (2021).

The test_eqvar method of test statistic objects generalizes this test to more than two groups (see Unequal variances).


Example#

The example below compares vertical ground reaction forces at two of the three walking speeds of the SpeedGRF dataset. There are 20 trials at each speed. The default alt='unequal' uses the ratio of the larger to the smaller sample variance at each node. It requires equal sample sizes.

import numpy as np
import spm1d
import jikudata as jd

dataset = jd.SpeedGRFcategorical()
y, A    = dataset.y, dataset.x  # (60,101) array and (60,) array of speed labels 1, 2 and 3
y2, y3  = y[ A == 2 ], y[ A == 3 ]
Fi      = spm1d.stats.eqvartest( y2, y3 ).inference( 0.05 )

The figure below depicts the two groups (left) and the variance ratio with its critical threshold (right).

from matplotlib import pyplot as plt

fig, (ax0, ax1) = plt.subplots( 1, 2, figsize=(10, 4), tight_layout=True )
ax0.plot( y2.T, color='k', lw=0.8 )
ax0.plot( y3.T, color='#2eb0ce', lw=0.8 )
ax0.plot( [], [], color='k', label='Speed 2' )
ax0.plot( [], [], color='#2eb0ce', label='Speed 3' )
ax0.legend( frameon=False )
ax0.set_title( 'SpeedGRF (categorical)' )
x = np.arange( Fi.z.size )
ax1.plot( x, Fi.z, color='k', lw=2 )                                                         # variance ratio
ax1.fill_between( x, Fi.zc, Fi.z, where=Fi.z > Fi.zc, interpolate=True, color='0.8' )        # suprathreshold region
ax1.axhline( Fi.zc, color='k', ls='--', lw=1 )                                              # critical threshold
ax1.axhline( 1, color='k', ls=':', lw=1 )                                                   # equal variances
ax1.text( 40, Fi.zc + 0.4, rf'$\alpha$={Fi.alpha}:  $F_c$={Fi.zc:.3f}', color='#d0453a' )
ax1.set_ylabel( 'F' )
ax1.set_title( 'spm1d.stats.eqvartest' )
plt.show()
GRF eqvartest GRF eqvartest

The alt='greater' option tests whether the first argument’s variance is greater than the second argument’s variance:

Fi = spm1d.stats.eqvartest( y2, y3, alt='greater' ).inference( 0.05 )
print( np.around([Fi.zc, Fi.zmax],3), Fi.h0reject )
[4.631 1.472] False