Beispiel #1
0
 def test_get_random_paired_permutation(self):
     paired = True
     n1 = n2 = 4
     #to test for different permutations
     for _ in range(10):
         perm = permute.get_permutation(paired, n1, n2, self.rng)
         self.assert_paired_permutation_validity(perm, n1)
Beispiel #2
0
 def test_get_paired_permutation(self):
     paired = True
     perms = []
     n1 = n2 = 4
     for i in range(2**n1):
         perm = permute.get_permutation(paired, n1, n2, None, i)
         self.assert_paired_permutation_validity(perm, n1)
         for other_perm in perms:
             assert not (other_perm == perm).all()
         perms.append(perm)
Beispiel #3
0
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
Beispiel #4
0
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
Beispiel #5
0
 def test_get_normal_permutation(self):
     paired = False
     n1 = n2 = 4
     for i in range(10):
         p = permute.get_permutation(paired, n1, n2, rng=self.rng)
         self.assert_permutation_validity(p, n1+n2)