1
2
3
4
5
6
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
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
31 nobs = N.sum(obs)
32
33
34 if exp == None:
35 exp = N.ones(obs.shape) * nobs / N.prod(obs.shape)
36
37
38 exp = exp.astype(float)
39
40
41 chisq = N.sum((obs - exp )**2 / exp)
42
43
44 return chisq, stats.chisqprob(chisq, N.prod(obs.shape) - 1)
45