Skip to content
DeepForgeby svx
All problems
HardStatisticsst-133

Permutation p-Value for Difference in Medians

Problem

Run a two-sided permutation test on the difference in medians between x and y using n_perm label shuffles driven by the seeded Lehmer RNG. Return (count + 1)/(n_perm + 1), where count is the number of permuted absolute median differences at least as large as the observed one; return 1.0 for empty input or n_perm <= 0. Unlike the mean version, this statistic is robust to outliers.

Hint

Shuffle labels with Fisher-Yates, then recompute the median difference.

Test cases

case 1
in: [[1,2,3,4,5],[6,7,8,9,10],100,42]
expected: 0.09900990099009901
case 2
in: [[1,2,3,4,5],[3,4,5,6,7],100,7]
expected: 0.32673267326732675
case 3
in: [[5,5,5],[1,2,3],50,3]
expected: 0.3137254901960784
case 4
in: [[],[1],10,1]
expected: 1
case 5
in: [[1],[2],0,1]
expected: 1
Starter codePython
def permutation_p_value_medians(x, y, n_perm, seed):
    # Your code here
    pass

5 test cases · run your code against them in the editor