Created
January 10, 2017 00:41
-
-
Save kylecorry31/659c5f4c45fa9e80b3af66db130f4368 to your computer and use it in GitHub Desktop.
Chi Squared Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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