Search code examples
pythonpython-3.xp-value

Receiving error message when trying to calculate p_value


Getting a, "TypeError: unsupported operand type(s) for /: 'generator' and 'int'",

Problem is when I try and calculate the p_value, not sure what I am doing wrong. Forgive me if my question is a bit vague

import numpy as np
import random
beer = [27, 19, 20, 20, 23, 17, 21, 24, 31, 26, 28, 20, 27, 19, 25, 31, 24, 28, 24, 29, 21, 21, 18, 27, 20]
water = [21, 19, 13, 22, 15, 22, 15, 22, 20, 12, 24, 24, 21, 19, 18, 16, 23, 20]

#running a permutation test
def permutation_test():
  combined = beer + water
  random.shuffle(combined)

#slice function to create 2 groups of the same length as the beer test group
  split = len(beer)
  group_one,group_two = combined[:split], combined[split:] #first25, last25
  return np.mean(group_one)-np.mean(group_two)

#monte carlo method to run the permutation test 100 000 times  
iterate = [permutation_test() for _ in range(100000)]

#calculating effect size, standard score
effect_size = np.median(beer) - np.median(water)
standard_score = (effect_size - np.mean(iterate))/np.std(iterate)

#calculating p-value to assess whether the observed effect size is an anomaly
p_value = np.mean(test >= effect_size for test in iterate)
print(standard_score, p_value)

Solution

  • Your list comprehension expression is not correctly defined:

    Use this to solve the problem:

     p_value = np.mean([(test >= effect_size) for test in iterate])