/usr/share/pyshared/mvpa2/misc/stats.py is in python-mvpa2 2.2.0-4ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the PyMVPA package for the
# copyright and license terms.
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Little statistics helper"""
__docformat__ = 'restructuredtext'
from mvpa2.base import externals
if externals.exists('scipy', raise_=True):
import scipy.stats as st
# evaluate once the fact of life
__scipy_prior0101 = externals.versions['scipy'] < '0.10.1'
import numpy as np
import copy
def chisquare(obs, exp='uniform'):
"""Compute the chisquare value of a contingency table with arbitrary
dimensions.
Parameters
----------
obs : array
Observations matrix
exp : ('uniform', 'indep_rows') or array, optional
Matrix of expected values of the same size as `obs`. If no
array is given, then for 'uniform' -- evenly distributes all
observations. In 'indep_rows' case contingency table takes into
account frequencies relative across different columns, so, if
the contingency table is predictions vs targets, it would
account for dis-balance among different targets. Although
'uniform' is the default, for confusion matrices 'indep_rows' is
preferable.
Returns
-------
tuple
chisquare-stats, associated p-value (upper tail)
"""
obs = np.array(obs)
# get total number of observations
nobs = np.sum(obs)
# if no expected value are supplied assume equal distribution
if not isinstance(exp, np.ndarray):
ones = np.ones(obs.shape, dtype=float)
if exp == 'indep_rows':
# multiply each column
exp = np.sum(obs, axis=0)[None, :] * ones / obs.shape[0]
elif exp == 'indep_cols':
# multiply each row
exp = np.sum(obs, axis=1)[:, None] * ones / obs.shape[1]
elif exp == 'uniform':
# just evenly distribute
exp = nobs * np.ones(obs.shape, dtype=float) / np.prod(obs.shape)
else:
raise ValueError, \
"Unknown specification of expected values exp=%r" % (exp,)
else:
assert(exp.shape == obs.shape)
# make sure to have floating point data
exp = exp.astype(float)
# compute chisquare value
exp_zeros = exp == 0
exp_nonzeros = np.logical_not(exp_zeros)
if np.sum(exp_zeros) !=0 and (obs[exp_zeros] != 0).any():
raise ValueError, \
"chisquare: Expected values have 0-values, but there are actual" \
" observations -- chi^2 cannot be computed"
chisq = np.sum(((obs - exp )**2)[exp_nonzeros] / exp[exp_nonzeros])
# return chisq and probability (upper tail)
# taking only the elements with something expected
return chisq, st.chisqprob(chisq, np.sum(exp_nonzeros) - 1)
class DSMatrix(object):
"""DSMatrix allows for the creation of dissilimarity matrices using
arbitrary distance metrics.
"""
# metric is a string
def __init__(self, data_vectors, metric='spearman'):
"""Initialize DSMatrix
Parameters
----------
data_vectors : ndarray
m x n collection of vectors, where m is the number of exemplars
and n is the number of features per exemplar
metric : string
Distance metric to use (e.g., 'euclidean', 'spearman', 'pearson',
'confusion')
"""
# init members
self.full_matrix = []
self.u_triangle = None
self.vector_form = None
# this one we know straight away, so set it
self.metric = metric
# size of dataset (checking if we're dealing with a column vector only)
num_exem = np.shape(data_vectors)[0]
flag_1d = False
# changed 4/26/09 to new way of figuring out if array is 1-D
#if (isinstance(data_vectors, np.ndarray)):
if (not(num_exem == np.size(data_vectors))):
num_features = np.shape(data_vectors)[1]
else:
flag_1d = True
num_features = 1
# generate output (dissimilarity) matrix
dsmatrix = np.mat(np.zeros((num_exem, num_exem)))
if (metric == 'euclidean'):
#print 'Using Euclidean distance metric...'
# down rows
for i in range(num_exem):
# across columns
for j in range(num_exem):
if (not(flag_1d)):
dsmatrix[i, j] = np.linalg.norm(
data_vectors[i, :] - data_vectors[j, :])
else:
dsmatrix[i, j] = np.linalg.norm(
data_vectors[i] - data_vectors[j])
elif (metric == 'spearman'):
#print 'Using Spearman rank-correlation metric...'
# down rows
for i in range(num_exem):
# across columns
for j in range(num_exem):
dsmatrix[i, j] = 1 - st.spearmanr(
data_vectors[i,:], data_vectors[j,:])[0]
elif (metric == 'pearson'):
dsmatrix = np.corrcoef(data_vectors)
elif (metric == 'confusion'):
#print 'Using confusion correlation metric...'
# down rows
for i in range(num_exem):
# across columns
for j in range(num_exem):
if (not(flag_1d)):
dsmatrix[i, j] = 1 - int(
np.floor(np.sum((
data_vectors[i, :] == data_vectors[j, :]
).astype(np.int32)) / num_features))
else:
dsmatrix[i, j] = 1 - int(
data_vectors[i] == data_vectors[j])
self.full_matrix = dsmatrix
##REF: Name was automagically refactored
def get_triangle(self):
# if we need to create the u_triangle representation, do so
if (self.u_triangle is None):
self.u_triangle = np.triu(self.full_matrix)
return self.u_triangle
# create the dissimilarity matrix on the (upper) triangle of the two
# two dissimilarity matrices; we can just reuse the same dissimilarity
# matrix code, but since it will return a matrix, we need to pick out
# either dsm[0,1] or dsm[1,0]
# note: this is a bit of a kludge right now, but it's the only way to solve
# certain problems:
# 1. Set all 0-valued elements in the original matrix to -1 (an impossible
# value for a dissimilarity matrix)
# 2. Find the upper triangle of the matrix
# 3. Create a vector from the upper triangle, but only with the
# elements whose absolute value is greater than 0 -- this
# will keep everything from the original matrix that wasn't
# part of the zero'ed-out portion when we took the upper
# triangle
# 4. Set all the -1-valued elements in the vector to 0 (their
# original value)
# 5. Cast to numpy array
##REF: Name was automagically refactored
def get_vector_form(self):
if (self.vector_form is not None):
return self.vector_form
orig_dsmatrix = copy.deepcopy(self.get_full_matrix())
orig_dsmatrix[orig_dsmatrix == 0] = -1
orig_tri = np.triu(orig_dsmatrix)
vector_form = orig_tri[abs(orig_tri) > 0]
vector_form[vector_form == -1] = 0
self.vector_form = np.asarray(vector_form)
return self.vector_form
# XXX is there any reason to have these get* methods
# instead of plain access to full_matrix and method?
##REF: Name was automagically refactored
def get_full_matrix(self):
return self.full_matrix
##REF: Name was automagically refactored
def get_metric(self):
return self.metric
def _chk_asanyarray(a, axis):
a = np.asanyarray(a)
if axis is None:
a = a.ravel()
outaxis = 0
else:
outaxis = axis
return a, outaxis
def ttest_1samp(a, popmean=0, axis=0, mask=None, alternative='two-sided'):
"""
Calculates the T-test for the mean of ONE group of scores `a`.
This is a refinement for the :func:`scipy.stats.ttest_1samp` for
the null hypothesis testing that the expected value (mean) of a
sample of independent observations is equal to the given
population mean, `popmean`. It adds ability to test carry single
tailed test as well as operate on samples with varying number of
active measurements, as specified by `mask` argument.
Since it is only a refinement and otherwise it should perform the
same way as the original ttest_1samp -- the name was overloaded.
Note
----
Initially it was coded before discovering scipy.mstats which
should work with masked arrays. But ATM (scipy 0.10.1) its
ttest_1samp does not support axis argument making it of limited
use anyways.
Parameters
----------
a : array_like
sample observations
popmean : float or array_like
expected value in null hypothesis, if array_like than it must have the
same shape as `a` excluding the axis dimension
axis : int, optional, (default axis=0)
Axis can equal None (ravel array first), or an integer (the axis
over which to operate on a).
mask : array_like, bool
bool array to specify which measurements should participate in the test
alternative : ('two-sided', 'greater', 'less')
alternative two test
Returns
-------
t : float or array
t-statistic
prob : float or array
p-value
Examples
--------
TODO
"""
# would also flatten if no axis specified
a, axis = _chk_asanyarray(a, axis)
if isinstance(a, np.ma.core.MaskedArray):
if mask is not None:
raise ValueError(
"Provided array is already masked, so no additional "
"mask should be provided")
n = a.count(axis=axis)
elif mask is not None:
# Create masked array
a = np.ma.masked_array(a, mask=~np.asanyarray(mask))
n = a.count(axis=axis)
else:
# why bother doing anything?
n = a.shape[axis]
df = n - 1
d = np.mean(a, axis) - popmean
# yoh: there is a bug in old (e.g. 1.4.1) numpy's while operating on
# masked arrays -- for some reason refuses to compute var
# correctly whenever only 2 elements are available and it is
# multi-dimensional:
# (Pydb) print np.var(a[:, 9:11], axis, ddof=1)
# [540.0 --]
# (Pydb) print np.var(a[:, 10:11], axis, ddof=1)
# [--]
# (Pydb) print np.var(a[:, 10], axis, ddof=1)
# 648.0
v = np.var(a, axis, ddof=1)
denom = np.sqrt(v / n)
t = np.divide(d, denom)
# t, prob might be full arrays if no masking was actually done
def _filled(a):
if isinstance(a, np.ma.core.MaskedArray):
return a.filled(np.nan)
else:
return a
t, prob = _ttest_finish(_filled(df), _filled(t), alternative=alternative)
return t, prob
def _ttest_finish(df, t, alternative):
"""Common code between all 3 t-test functions."""
dist_gen = st.distributions.t
if alternative == 'two-sided':
prob = dist_gen.sf(np.abs(t), df) * 2 # use np.abs to get upper alternative
elif alternative == 'greater':
prob = dist_gen.sf(t, df)
elif alternative == 'less':
prob = dist_gen.cdf(t, df)
else:
raise ValueError("Unknown alternative %r" % alternative)
t_isnan = np.isnan(t)
if np.any(t_isnan) and __scipy_prior0101:
# older scipy's would return 0 for nan values of the argument
# which is incorrect
prob[t_isnan] = np.nan
if t.ndim == 0:
t = np.asscalar(t)
return t, prob
|