Skip to content

Commit

Permalink
Permutation test
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxence-L authored Oct 3, 2020
1 parent a08f987 commit eebad34
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions bootstrap tests/permutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import numpy as np
import matplotlib.pyplot as plt

def permutation_test(first_array, second_array, resampling=30000, print_result=True, seed=0):

np.random.seed = seed

H0_diff = abs(first_array.mean() - second_array.mean())

total_array = np.hstack([first_array, second_array])

mean_diffs = []

for _ in range(resampling):

np.random.shuffle(total_array)

random_first_array = total_array[:first_array.shape[0]]
random_second_array = total_array[first_array.shape[0]:]

mean_diff = abs(random_first_array.mean() - random_second_array.mean())
mean_diffs.append(mean_diff)

nb_extreme_values = 0

for min_diff in mean_diffs:
if min_diff > abs(H0_diff):
nb_extreme_values += 1

p_value = nb_extreme_values / len(mean_diffs)

if print_result : print(f"P-value is {p_value:.2f}")
return p_value

0 comments on commit eebad34

Please sign in to comment.