Package mvpa :: Package misc :: Module stats
[hide private]
[frames] | no frames]

Source Code for Module mvpa.misc.stats

 1  #emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- 
 2  #ex: set sts=4 ts=4 sw=4 et: 
 3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
 4  # 
 5  #   See COPYING file distributed along with the PyMVPA package for the 
 6  #   copyright and license terms. 
 7  # 
 8  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
 9  """Little statistics helper""" 
10   
11  __docformat__ = 'restructuredtext' 
12   
13  from mvpa.base import externals 
14  externals.exists('scipy', raiseException=True) 
15   
16  import scipy.stats as stats 
17  import numpy as N 
18   
19 -def chisquare(obs, exp=None):
20 """Compute the chisquare value of a contingency table with arbitrary 21 dimensions. 22 23 If no expected frequencies are supplied, the total N is assumed to be 24 equally distributed across all cells. 25 26 Returns: chisquare-stats, associated p-value (upper tail) 27 """ 28 obs = N.array(obs) 29 30 # get total number of observations 31 nobs = N.sum(obs) 32 33 # if no expected value are supplied assume equal distribution 34 if exp == None: 35 exp = N.ones(obs.shape) * nobs / N.prod(obs.shape) 36 37 # make sure to have floating point data 38 exp = exp.astype(float) 39 40 # compute chisquare value 41 chisq = N.sum((obs - exp )**2 / exp) 42 43 # return chisq and probability (upper tail) 44 return chisq, stats.chisqprob(chisq, N.prod(obs.shape) - 1)
45