Last active
September 10, 2015 19:51
-
-
Save yabberyabber/ae589aeabdc5d65ffe1c to your computer and use it in GitHub Desktop.
Every day at work we get a random cat fact in our slack chat, each fact has a number on it. Assuming those numbers start at 1, are unique, and are consecutive, how many cat facts total would you guess there are?
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 math | |
def frequentist(facts): | |
m = max(facts) | |
k = len(facts) | |
return m + (float(m) / k) - 1 | |
def bayesian(facts): | |
m = float(max(facts)) | |
k = float(len(facts)) | |
mean = (m - 1) * (k - 1) / (k - 2) | |
variance = math.sqrt((k - 1) * (m - 1) * (m - k + 1) / (k - 3) / (k - 2) / (k - 2)) | |
return "%(mean)f +/- %(variance)f" % \ | |
{"mean": mean, "variance": variance} | |
estimators = [frequentist, bayesian] | |
seen_facts = [1, 6, 9, 23, 43, 32, 53, 16, 79, 14, 72, 41, 5] | |
print "Cat facts seen so far: " + str(seen_facts) | |
for estimator in estimators: | |
print "Estimation using method '" + estimator.__name__ + "':", | |
print estimator(seen_facts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cat facts seen so far: [1, 6, 9, 23, 43, 32, 53, 16, 79, 14, 72, 41, 5]
Estimation using method 'frequentist': 84.0769230769
Estimation using method 'bayesian': 85.090909 +/- 7.199174