Skip to content

Instantly share code, notes, and snippets.

@stringertheory
Last active April 19, 2019 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stringertheory/3a1a8c5a707bc4c3ee2befd67c312807 to your computer and use it in GitHub Desktop.
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)
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