Last active
April 19, 2019 21:49
-
-
Save stringertheory/3a1a8c5a707bc4c3ee2befd67c312807 to your computer and use it in GitHub Desktop.
A test of whether skittles are uniformly distributed (using data from https://github.com/possibly-wrong/skittles)
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
import sys | |
import collections | |
import random | |
N_TRIALS = 100000 | |
FLAVORS = ['Strawberry', 'Orange', 'Lemon', 'Apple', 'Grape'] | |
def chisquared(values): | |
mean = sum(values) / float(len(values)) | |
return sum((v - mean)**2/mean for v in values) | |
def read_file(filename='skittles.txt'): | |
with open(filename) as infile: | |
header = next(infile).strip().split() | |
for line in infile: | |
row = line.strip().split() | |
yield dict(zip(header, [int(i) for i in row])) | |
# ignore things not in FLAVORS (like "Uncounted") | |
observed = collections.Counter() | |
for row in read_file(): | |
for key, value in row.items(): | |
if key in FLAVORS: | |
observed[key] += value | |
print(observed, file=sys.stderr) | |
print(chisquared(observed.values()), file=sys.stderr) | |
for trial_number in range(N_TRIALS): | |
# simulate uniform skittle flavors | |
dist = collections.Counter() | |
for i in range(sum(observed.values())): | |
dist[FLAVORS[random.randint(0, len(FLAVORS) - 1)]] += 1 | |
print(chisquared(dist.values()), flush=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment