def two_sample(A, B, limit = 10000, progress_bar = None): """ Conducts a permutation test on the input data, transformed by fun. """ # apply transformation to input data (e.g. signed-rank for WMW) data = np.concatenate((A, B)) stat_ref = np.sum(A) # count permutation test statistics <=, >=, or ||>=|| than reference stat counts = np.array([0,0,0]) # (lesser, greater) total_perms = bincoef(len(data), len(A)) if not limit or total_perms < limit : limit = None comb_function = itertools.combinations else: comb_function = random_combination if progress_bar: progress_bar.max = limit or total_perms progress_bar.label = "of %d permutations" % progress_bar.max for binary_row in binary_combinations(len(data), len(A), comb_function=comb_function, limit=limit): #print binary_row stat_this = np.sum(np.array(data)*binary_row) counts = counts + stat_compare(stat_ref,stat_this) # if progress_bar: # progress_bar.advance() # return p-values for lower, upper, and two-tail tests (FP number) n_comb = np.multiply.reduce(np.array(range(len(data)-len(A)+1,len(data)+1)))\ / np.multiply.reduce(np.array(range(1,len(A)+1))) n_comb = limit or total_perms counts[2] = min(2*counts[0:2].min(),n_comb) # hack to define p.twotail as 2*smaller of 1 tail p's return counts / float(n_comb)
def random_combination(iterable, r, limit=None): """ Random selection from itertools.combinations(iterable, r). """ pool = tuple(iterable) n = len(pool) limiter = 0 comb = bincoef(len(iterable), r) print comb comb_indices = random.sample(xrange(comb), limit) while True if limit == None else limiter < limit: print comb_indices[limiter] perm = get_nth_perm(pool, comb_indices[limiter]) subpool = sorted(perm[:r]) indices = sorted(random.sample(xrange(n), r)) print tuple(pool[i] for i in indices), subpool, perm limiter = limiter + 1 yield tuple(pool[i] for i in indices)
def two_sample(A, B, limit=10000, progress_bar=None): """ Conducts a permutation test on the input data, transformed by fun. """ # apply transformation to input data (e.g. signed-rank for WMW) data = np.concatenate((A, B)) stat_ref = np.sum(A) # count permutation test statistics <=, >=, or ||>=|| than reference stat counts = np.array([0, 0, 0]) # (lesser, greater) total_perms = bincoef(len(data), len(A)) if not limit or total_perms < limit: limit = None comb_function = itertools.combinations else: comb_function = random_combination if progress_bar: progress_bar.max = limit or total_perms progress_bar.label = "of %d permutations" % progress_bar.max for binary_row in binary_combinations(len(data), len(A), comb_function=comb_function, limit=limit): #print binary_row stat_this = np.sum(np.array(data) * binary_row) counts = counts + stat_compare(stat_ref, stat_this) # if progress_bar: # progress_bar.advance() # return p-values for lower, upper, and two-tail tests (FP number) n_comb = np.multiply.reduce(np.array(range(len(data)-len(A)+1,len(data)+1)))\ / np.multiply.reduce(np.array(range(1,len(A)+1))) n_comb = limit or total_perms counts[2] = min( 2 * counts[0:2].min(), n_comb) # hack to define p.twotail as 2*smaller of 1 tail p's return counts / float(n_comb)