Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's implementation of Permutation Test with permutation number as input

R well-known library for permutation test i.e. perm. The example I'm interested in is this:

 x <- c(12.6, 11.4, 13.2, 11.2, 9.4, 12.0)
 y <- c(16.4, 14.1, 13.4, 15.4, 14.0, 11.3)
 permTS(x,y, alternative="two.sided", method="exact.mc", control=permControl(nmc=30000))$p.value

Which prints result with p-value: 0.01999933. Note there the function permTS allows us to input number of permutation = 30000. Is there such similar implmentation in Python?

I was looking at Python's perm_stat, but it's not what I'm looking for and seems to be buggy.

like image 651
pdubois Avatar asked Jul 17 '14 05:07

pdubois


People also ask

How do you find the permutation of a number in Python?

The number of permutations on a set of n elements is given by n!. For example, there are 2! = 2*1 = 2 permutations of {1, 2}, namely {1, 2} and {2, 1}, and 3! = 3*2*1 = 6 permutations of {1, 2, 3}, namely {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2} and {3, 2, 1}.

What is a permutation test Python?

Permutes targets to generate 'randomized data' and compute the empirical p-value against the null hypothesis that features and targets are independent. The p-value represents the fraction of randomized data sets where the estimator performed as well or better than in the original data.

How do you generate all permutations of a string in Python?

To find all possible permutations of a given string, you can use the itertools module which has a useful method called permutations(iterable[, r]). This method return successive r length permutations of elements in the iterable as tuples.


1 Answers

This is a possible implementation of permutation test using monte-carlo method:

def exact_mc_perm_test(xs, ys, nmc):
    n, k = len(xs), 0
    diff = np.abs(np.mean(xs) - np.mean(ys))
    zs = np.concatenate([xs, ys])
    for j in range(nmc):
        np.random.shuffle(zs)
        k += diff < np.abs(np.mean(zs[:n]) - np.mean(zs[n:]))
    return k / nmc

note that given the monte-carlo nature of the algorithm you will not get exact same number on each run:

>>> xs = np.array([12.6, 11.4, 13.2, 11.2, 9.4, 12.0])
>>> ys = np.array([16.4, 14.1, 13.4, 15.4, 14.0, 11.3])
>>> exact_mc_perm_test(xs, ys, 30000)
0.019466666666666667
like image 87
behzad.nouri Avatar answered Sep 25 '22 20:09

behzad.nouri