1
2
3
4
5
6
7
8
9 """Little statistics helper"""
10
11 __docformat__ = 'restructuredtext'
12
13 from mvpa.base import externals
14
15 if externals.exists('scipy', raiseException=True):
16 import scipy.stats as stats
17
18 import numpy as N
19 import copy
20
22 """Compute the chisquare value of a contingency table with arbitrary
23 dimensions.
24
25 If no expected frequencies are supplied, the total N is assumed to be
26 equally distributed across all cells.
27
28 Returns: chisquare-stats, associated p-value (upper tail)
29 """
30 obs = N.array(obs)
31
32
33 nobs = N.sum(obs)
34
35
36 if exp == None:
37 exp = N.ones(obs.shape) * nobs / N.prod(obs.shape)
38
39
40 exp = exp.astype(float)
41
42
43 chisq = N.sum((obs - exp )**2 / exp)
44
45
46 return chisq, stats.chisqprob(chisq, N.prod(obs.shape) - 1)
47
48
50 """DSMatrix allows for the creation of dissilimarity matrices using
51 arbitrary distance metrics.
52 """
53
54
55 - def __init__(self, data_vectors, metric='spearman'):
56 """Initialize DSMatrix
57
58 :Parameters:
59 data_vectors : ndarray
60 m x n collection of vectors, where m is the number of exemplars
61 and n is the number of features per exemplar
62 metric : string
63 Distance metric to use (e.g., 'euclidean', 'spearman', 'pearson',
64 'confusion')
65 """
66
67 self.full_matrix = []
68 self.u_triangle = None
69 self.vector_form = None
70
71
72 self.metric = metric
73
74
75 num_exem = N.shape(data_vectors)[0]
76 flag_1d = False
77
78
79 if (not(num_exem == N.size(data_vectors))):
80 num_features = N.shape(data_vectors)[1]
81 else:
82 flag_1d = True
83 num_features = 1
84
85
86 dsmatrix = N.mat(N.zeros((num_exem, num_exem)))
87
88 if (metric == 'euclidean'):
89
90
91 for i in range(num_exem):
92
93 for j in range(num_exem):
94 if (not(flag_1d)):
95 dsmatrix[i,j] = N.linalg.norm(data_vectors[i,:] - data_vectors[j,:])
96 else:
97 dsmatrix[i,j] = N.linalg.norm(data_vectors[i] - data_vectors[j])
98
99 elif (metric == 'spearman'):
100
101
102 for i in range(num_exem):
103
104 for j in range(num_exem):
105 dsmatrix[i,j] = 1 - stats.spearmanr(data_vectors[i,:],data_vectors[j,:])[0]
106
107 elif (metric == 'pearson'):
108
109
110 for i in range(num_exem):
111
112 for j in range(num_exem):
113 dsmatrix[i, j] = 1 - stats.pearsonr(
114 data_vectors[i,:],data_vectors[j,:])[0]
115
116 elif (metric == 'confusion'):
117
118
119 for i in range(num_exem):
120
121 for j in range(num_exem):
122 if (not(flag_1d)):
123 dsmatrix[i, j] = 1 - int(
124 N.floor(N.sum((
125 data_vectors[i, :] == data_vectors[j, :]
126 ).astype(N.int32)) / num_features))
127 else:
128 dsmatrix[i, j] = 1 - int(
129 data_vectors[i] == data_vectors[j])
130
131 self.full_matrix = dsmatrix
132
134
135 if (self.u_triangle == None):
136 self.u_triangle = N.triu(self.full_matrix)
137
138 return self.u_triangle
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
175
176
177
179 return self.full_matrix
180
183