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

前端 未结 1 804
轮回少年
轮回少年 2021-02-09 04:26

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,         


        
相关标签:
1条回答
  • 2021-02-09 05:06

    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
    
    0 讨论(0)
提交回复
热议问题