-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdist_test.py
More file actions
389 lines (326 loc) · 14.7 KB
/
dist_test.py
File metadata and controls
389 lines (326 loc) · 14.7 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import ieeg.viz
import numpy as np
import matplotlib.pyplot as plt
from ieeg.calc.fast import mean_diff, _md
from ieeg.arrays._api import get_namespace
from ieeg.arrays.reshape import windower
import inspect
from itertools import combinations
from scipy.special import comb
AxisError: type[Exception]
def permutation_test(data, statistic, *, permutation_type='independent',
vectorized=None, n_resamples=9999, batch=None,
alternative="two-sided", axis=0, rng=None):
args = _permutation_test_iv(data, statistic, permutation_type, vectorized,
n_resamples, batch, alternative, axis,
rng)
(data, statistic, permutation_type, vectorized, n_resamples, batch,
alternative, axis, rng) = args
observed = statistic(*data, axis=-1)
null_calculators = {"independent": _calculate_null_both}
null_calculator_args = (data, statistic, n_resamples,
batch, rng)
calculate_null = null_calculators[permutation_type]
null_distribution, n_resamples, exact_test = (
calculate_null(*null_calculator_args))
# See References [2] and [3]
adjustment = 0 if exact_test else 1
# relative tolerance for detecting numerically distinct but
# theoretically equal values in the null distribution
eps = (0 if not np.issubdtype(observed.dtype, np.inexact)
else np.finfo(observed.dtype).eps*100)
gamma = np.abs(eps * observed)
def less(null_distribution, observed):
cmps = null_distribution <= observed + gamma
pvalues = (cmps.sum(axis=0) + adjustment) / (n_resamples + adjustment)
return pvalues
def greater(null_distribution, observed):
cmps = null_distribution >= observed - gamma
pvalues = (cmps.sum(axis=0) + adjustment) / (n_resamples + adjustment)
return pvalues
def two_sided(null_distribution, observed):
pvalues_less = less(null_distribution, observed)
pvalues_greater = greater(null_distribution, observed)
pvalues = np.minimum(pvalues_less, pvalues_greater) * 2
return pvalues
compare = {"less": less,
"greater": greater,
"two-sided": two_sided}
pvalues = compare[alternative](null_distribution, observed)
pvalues = np.clip(pvalues, 0, 1)
return observed, pvalues, null_distribution
def _permutation_test_iv(data, statistic, permutation_type, vectorized,
n_resamples, batch, alternative, axis, rng):
"""Input validation for `permutation_test`."""
axis_int = int(axis)
if axis != axis_int:
raise ValueError("`axis` must be an integer.")
permutation_types = {'samples', 'pairings', 'independent'}
permutation_type = permutation_type.lower()
if permutation_type not in permutation_types:
raise ValueError(f"`permutation_type` must be in {permutation_types}.")
if vectorized not in {True, False, None}:
raise ValueError("`vectorized` must be `True`, `False`, or `None`.")
if vectorized is None:
vectorized = 'axis' in inspect.signature(statistic).parameters
if not vectorized:
statistic = _vectorize_statistic(statistic)
message = "`data` must be a tuple containing at least two samples"
try:
if len(data) < 2 and permutation_type == 'independent':
raise ValueError(message)
except TypeError:
raise TypeError(message)
data = _broadcast_arrays(data, axis)
data_iv = []
for sample in data:
sample = np.atleast_1d(sample)
if sample.shape[axis] <= 1:
raise ValueError("each sample in `data` must contain two or more "
"observations along `axis`.")
sample = np.moveaxis(sample, axis_int, -1)
data_iv.append(sample)
n_resamples_int = (int(n_resamples) if not np.isinf(n_resamples)
else np.inf)
if n_resamples != n_resamples_int or n_resamples_int <= 0:
raise ValueError("`n_resamples` must be a positive integer.")
if batch is None:
batch_iv = batch
else:
batch_iv = int(batch)
if batch != batch_iv or batch_iv <= 0:
raise ValueError("`batch` must be a positive integer or None.")
alternatives = {'two-sided', 'greater', 'less'}
alternative = alternative.lower()
if alternative not in alternatives:
raise ValueError(f"`alternative` must be in {alternatives}")
if rng is None or isinstance(rng, int):
rng = np.random.RandomState(rng)
elif not hasattr(rng, 'permutation'):
raise ValueError("`rng` must have a `permutation`"
" method or be an integer.")
return (data_iv, statistic, permutation_type, vectorized, n_resamples_int,
batch_iv, alternative, axis_int, rng)
def _batch_generator(iterable, batch):
"""A generator that yields batches of elements from an iterable"""
iterator = iter(iterable)
if batch <= 0:
raise ValueError("`batch` must be positive.")
z = [item for i, item in zip(range(batch), iterator)]
while z: # we don't want StopIteration without yielding an empty list
yield z
z = [item for i, item in zip(range(batch), iterator)]
def _vectorize_statistic(statistic):
"""Vectorize an n-sample statistic"""
# This is a little cleaner than np.nditer at the expense of some data
# copying: concatenate samples together, then use np.apply_along_axis
def stat_nd(*data, axis=0):
lengths = [sample.shape[axis] for sample in data]
split_indices = np.cumsum(lengths)[:-1]
z = _broadcast_concatenate(data, axis)
# move working axis to position 0 so that new dimensions in the output
# of `statistic` are _prepended_. ("This axis is removed, and replaced
# with new dimensions...")
z = np.moveaxis(z, axis, 0)
def stat_1d(z):
data = np.split(z, split_indices)
return statistic(*data)
return np.apply_along_axis(stat_1d, 0, z)[()]
return stat_nd
def _broadcast_concatenate(arrays, axis, paired=False):
"""Concatenate arrays along an axis with broadcasting."""
arrays = _broadcast_arrays(arrays, axis if not paired else None)
res = np.concatenate(arrays, axis=axis)
return res
def _broadcast_arrays(arrays, axis=None, xp=None):
"""
Broadcast shapes of arrays, ignoring incompatibility of specified axes
"""
if not arrays:
return arrays
xp, is_comp = get_namespace(*arrays) if xp is None else xp
xp = np if not is_comp else xp
arrays = [xp.asarray(arr) for arr in arrays]
shapes = [arr.shape for arr in arrays]
new_shapes = _broadcast_shapes(shapes, axis)
if axis is None:
new_shapes = [new_shapes]*len(arrays)
return [xp.broadcast_to(array, new_shape)
for array, new_shape in zip(arrays, new_shapes)]
def _broadcast_shapes(shapes, axis=None):
"""
Broadcast shapes, ignoring incompatibility of specified axes
"""
if not shapes:
return shapes
# input validation
if axis is not None:
axis = np.atleast_1d(axis)
message = '`axis` must be an integer, a tuple of integers, or `None`.'
try:
with np.errstate(invalid='ignore'):
axis_int = axis.astype(int)
except ValueError as e:
raise AxisError(message) from e
if not np.array_equal(axis_int, axis):
raise AxisError(message)
axis = axis_int
# First, ensure all shapes have same number of dimensions by prepending 1s.
n_dims = max([len(shape) for shape in shapes])
new_shapes = np.ones((len(shapes), n_dims), dtype=int)
for row, shape in zip(new_shapes, shapes):
row[len(row)-len(shape):] = shape # can't use negative indices (-0:)
# Remove the shape elements of the axes to be ignored, but remember them.
if axis is not None:
axis[axis < 0] = n_dims + axis[axis < 0]
axis = np.sort(axis)
if axis[-1] >= n_dims or axis[0] < 0:
message = (f"`axis` is out of bounds "
f"for array of dimension {n_dims}")
raise AxisError(message)
if len(np.unique(axis)) != len(axis):
raise AxisError("`axis` must contain only distinct elements")
removed_shapes = new_shapes[:, axis]
new_shapes = np.delete(new_shapes, axis, axis=1)
# If arrays are broadcastable, shape elements that are 1 may be replaced
# with a corresponding non-1 shape element. Assuming arrays are
# broadcastable, that final shape element can be found with:
new_shape = np.max(new_shapes, axis=0)
# except in case of an empty array:
new_shape *= new_shapes.all(axis=0)
# Among all arrays, there can only be one unique non-1 shape element.
# Therefore, if any non-1 shape element does not match what we found
# above, the arrays must not be broadcastable after all.
if np.any(~((new_shapes == 1) | (new_shapes == new_shape))):
raise ValueError("Array shapes are incompatible for broadcasting.")
if axis is not None:
# Add back the shape elements that were ignored
new_axis = axis - np.arange(len(axis))
new_shapes = [tuple(np.insert(new_shape, new_axis, removed_shape))
for removed_shape in removed_shapes]
return new_shapes
else:
return tuple(new_shape)
def _calculate_null_both(data, statistic, n_permutations, batch,
rng=None):
"""
Calculate null distribution for independent sample tests.
"""
n_samples = len(data)
# compute number of permutations
# (distinct partitions of data into samples of these sizes)
n_obs_i = [sample.shape[-1] for sample in data] # observations per sample
n_obs_ic = np.cumsum(n_obs_i)
n_obs = n_obs_ic[-1] # total number of observations
n_max = np.prod([comb(n_obs_ic[i], n_obs_ic[i-1])
for i in range(n_samples-1, 0, -1)])
# perm_generator is an iterator that produces permutations of indices
# from 0 to n_obs. We'll concatenate the samples, use these indices to
# permute the data, then split the samples apart again.
if n_permutations >= n_max:
exact_test = True
n_permutations = n_max
perm_generator = _all_partitions_concatenated(n_obs_i)
else:
exact_test = False
# Neither RandomState.permutation nor Generator.permutation
# can permute axis-slices independently. If this feature is
# added in the future, batches of the desired size should be
# generated in a single call.
# idx = np.arange(n_obs)
# probs = np.concatenate([(np.ones(n_obs_i[i])/n_obs_i[i])
# for i in range(n_samples)]) / n_samples
# perm_generator = rng.choice(idx, (n_permutations, n_obs), p=probs)
# perm_generator = (rng.permutation(n_obs)
# for i in range(n_permutations))
tot = min(n_obs_i)
perm_generator = rng.permuted(np.stack([np.concatenate([
rng.choice(n_obs_i[i], tot, shuffle=False, replace=False) +
(n_obs_ic[i-1] if i > 0 else 0)
for i in range(n_samples)])
for _ in range(n_permutations)]), axis=1)
batch = batch or int(n_permutations)
batch_generator = windower(perm_generator, batch, 0, 1)[::batch]
null_distribution = []
# First, concatenate all the samples. In batches, permute samples with
# indices produced by the `perm_generator`, split them into new samples of
# the original sizes, compute the statistic for each batch, and add these
# statistic values to the null distribution.
data = np.concatenate(data, axis=-1)
for indices in batch_generator:
# indices = np.array(indices)
# `indices` is 2D: each row is a permutation of the indices.
# We use it to index `data` along its last axis, which corresponds
# with observations.
# After indexing, the second to last axis of `data_batch` corresponds
# with permutations, and the last axis corresponds with observations.
data_batch = data[..., indices]
# Move the permutation axis to the front: we'll concatenate a list
# of batched statistic values along this zeroth axis to form the
# null distribution.
data_batch = np.moveaxis(data_batch, -2, 0)
data_batch = np.split(data_batch, n_obs_ic[:-1], axis=-1)
null_distribution.append(statistic(*data_batch, axis=-1))
null_distribution = np.concatenate(null_distribution, axis=0)
return null_distribution, n_permutations, exact_test
def _all_partitions_concatenated(ns):
"""
Generate all partitions of indices of groups of given sizes, concatenated
`ns` is an iterable of ints.
"""
def all_partitions(z, n):
for c in combinations(z, n):
x0 = set(c)
x1 = z - x0
yield [x0, x1]
def all_partitions_n(z, ns):
if len(ns) == 0:
yield [z]
return
for c in all_partitions(z, ns[0]):
for d in all_partitions_n(c[1], ns[1:]):
yield c[0:1] + d
z = set(range(np.sum(ns)))
for partitioning in all_partitions_n(z, ns[:]):
x = np.concatenate([list(partition)
for partition in partitioning]).astype(int)
yield x
# allocate memory for two normal distributions
n = 2000 # number of samples
m = 5000 # number of vectorized iterations
rng = np.random.default_rng()
arr1 = rng.normal(0, 1, (n, m))
# set up permutation test parameters
kwargs = {'n_resamples': 1000, 'statistic': mean_diff, 'batch': 1000,
'alternative': 'greater', 'vectorized': True, 'axis': 0,
'rng': rng}
samplings = ((1000, 1000), (500, 1500), (250, 1750), (100, 1900))
fig, axss = plt.subplots(3, 4)
stds = (0.5, 1, 2)
# iterate over the standard deviations
for i, axs in zip(stds, axss):
# group 2 has different std
arr2 = rng.normal(0, i, (n, m))
# iterate over the sampling proportions
for (prop1, prop2), ax in zip(samplings, axs):
# combine the distributions and boxcox transform
data = np.concatenate([arr2[:prop1], arr1[:prop2]], axis=0)
# data_fixed = cox(data)
inputs = [data[:prop1], data[prop1:]]
# run the permutation test
res = permutation_test(inputs, **kwargs)[1]
# plot the histogram of p-values
ax.hist(res, bins=20)
ax.set_ylim(0, 200)
if prop1 == 1000:
ax.set_ylabel(f'Count (grp2 std: {i})')
else:
ax.set_yticklabels([])
if i == 2:
ax.set_xlabel('p-value')
else:
if i == 0.5:
ax.set_title(f'{prop1}/{prop2}')
ax.set_xticklabels([])
fig.suptitle("Effect of grp2/grp1 sampling and variance on perm test p-values")
fig.show()