def permutation_test(data, stat_func, permute_func, n1, n2, paired_study, n_permutations, seed=None): """ Performs a permutation test with user specified test statistics yielding an estimate of the pvalue, and the test statistic. See :py:func:`parallel_permtest` for explanations of input arguments. """ # all pairs are paired -> change n_permutations if n_permutations == 'all': # (n-1): count everything twice for simplicity (a bit foolish...) n_permutations = 2 ** n1 rng = None # no rng needed if deterministic permutations else: rng = permute.get_random_state(seed) orig_stat = stat_func(data, n1) leftNs = np.zeros(np.shape(orig_stat)) rightNs = np.zeros(np.shape(orig_stat)) for i in range(0, int(n_permutations)): perm = permute.get_permutation(paired_study, n1, n2, rng, i) permdata = permute_func(data, perm) stats = stat_func(permdata, n1) leftNs += (stats <= orig_stat) rightNs += (stats >= orig_stat) tailNs = np.minimum(leftNs, rightNs) # multiply by two to get two-sided p-values: if rng is None: return np.minimum(1.0, 2. * tailNs / n_permutations), orig_stat else: pvals = (tailNs + 1.0) / (n_permutations + 1.0) return np.minimum(1, 2 * pvals), orig_stat
def permutation_test(data, stat_func, permute_func, n1, n2, paired_study, n_permutations, seed=None): """ Performs a permutation test with user specified test statistics yielding an estimate of the pvalue, and the test statistic. See :py:func:`parallel_permtest` for explanations of input arguments. """ # all pairs are paired -> change n_permutations if n_permutations == 'all': # (n-1): count everything twice for simplicity (a bit foolish...) n_permutations = 2**n1 rng = None # no rng needed if deterministic permutations else: rng = permute.get_random_state(seed) orig_stat = stat_func(data, n1) leftNs = np.zeros(np.shape(orig_stat)) rightNs = np.zeros(np.shape(orig_stat)) for i in range(0, int(n_permutations)): perm = permute.get_permutation(paired_study, n1, n2, rng, i) permdata = permute_func(data, perm) stats = stat_func(permdata, n1) leftNs += (stats <= orig_stat) rightNs += (stats >= orig_stat) tailNs = np.minimum(leftNs, rightNs) # multiply by two to get two-sided p-values: if rng is None: return np.minimum(1.0, 2. * tailNs / n_permutations), orig_stat else: pvals = (tailNs + 1.0) / (n_permutations + 1.0) return np.minimum(1, 2 * pvals), orig_stat
def setUp(self): self.as_arr_alm_eq = np.testing.assert_almost_equal self.data = np.array([1., 2, 3, 4, 5, 6]) self.perm1 = [0, 1, 2, 3, 4, 5] self.perm2 = [1, 2, 3, 4, 5, 0] self.two_dim_data = np.array([ [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6] ]) self.matrix = np.array([[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44] ]) self.mat_perm_1 = np.array([3, 1, 2, 0]) self.mat_perm_2 = np.array([1, 2, 3, 0]) self.rng = permute.get_random_state(seed=123456)