Skip to content
DeepForgeby svx
All problems
HardML Fundamentalsml-284

Permutation Inference

Problem

Compute a permutation p-value for the difference in means. The observed statistic is |mean(treated) - mean(control)|; then with random.seed(seed) shuffle the pooled sample num_permutations times and count how often a permuted absolute difference is at least the observed one.

Return the fraction; invalid input returns 1.0.

Hint

Permutation tests make no distributional assumptions beyond exchangeability.

Test cases

case 1
in: [[2,4,6],[1,3],200,42]
expected: 0.385
case 2
in: [[1,2,3,4],[2,2],100,7]
expected: 0.78
case 3
in: [[1,1],[1,1],100,7]
expected: 1
case 4
in: [[],[],10,0]
expected: 1
Starter codePython
import random
def permutation_p_value(treated, control, num_permutations, seed):
    # Your code here
    pass

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