Skip to content

Instantly share code, notes, and snippets.

@kylecorry31
Created January 10, 2017 00:41
Show Gist options
  • Save kylecorry31/659c5f4c45fa9e80b3af66db130f4368 to your computer and use it in GitHub Desktop.
Save kylecorry31/659c5f4c45fa9e80b3af66db130f4368 to your computer and use it in GitHub Desktop.
Chi Squared Test
# chi_square_test : [(number, number)] -> number
# consumes a list of tuples containing the observed data and expected data
# produces the chi squared term of the dataset
def chi_square_test(data_points):
s = 0
for point in data_points:
o, e = point
s += (o - e)**2/float(e)
return s
# is_significant : number number -> boolean
# determines if a chi squared term is significant based on the degrees of freedom
# uses 95% confidence as the threshold
def is_significant(chi_term, degrees_of_freedom):
threshes = [3.841, 5.991, 7.815, 9.488, 11.070, 12.592, 14.067, 15.507, 16.919, 18.307, 19.675, 21.026, 22.362, 23.685, 24.996]
return chi_term >= threshes[degrees_of_freedom + 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment