Created
February 16, 2014 09:10
-
-
Save pixyj/9031525 to your computer and use it in GitHub Desktop.
Maximum Likelihood estimator for k heads in n trials. https://www.udacity.com/course/viewer#!/c-st101/l-48727700/e-48726376/m-48724282
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
0 | 0 | |
---|---|---|
0.01 | 0.000297 | |
0.02 | 0.001176 | |
0.03 | 0.002619 | |
0.04 | 0.004608 | |
0.05 | 0.007125 | |
0.06 | 0.010152 | |
0.07 | 0.013671 | |
0.08 | 0.017664 | |
0.09 | 0.022113 | |
0.1 | 0.027 | |
0.11 | 0.032307 | |
0.12 | 0.038016 | |
0.13 | 0.044109 | |
0.14 | 0.050568 | |
0.15 | 0.057375 | |
0.16 | 0.064512 | |
0.17 | 0.071961 | |
0.18 | 0.079704 | |
0.19 | 0.087723 | |
0.2 | 0.096 | |
0.21 | 0.104517 | |
0.22 | 0.113256 | |
0.23 | 0.122199 | |
0.24 | 0.131328 | |
0.25 | 0.140625 | |
0.26 | 0.150072 | |
0.27 | 0.159651 | |
0.28 | 0.169344 | |
0.29 | 0.179133 | |
0.3 | 0.189 | |
0.31 | 0.198927 | |
0.32 | 0.208896 | |
0.33 | 0.218889 | |
0.34 | 0.228888 | |
0.35 | 0.238875 | |
0.36 | 0.248832 | |
0.37 | 0.258741 | |
0.38 | 0.268584 | |
0.39 | 0.278343 | |
0.4 | 0.288 | |
0.41 | 0.297537 | |
0.42 | 0.306936 | |
0.43 | 0.316179 | |
0.44 | 0.325248 | |
0.45 | 0.334125 | |
0.46 | 0.342792 | |
0.47 | 0.351231 | |
0.48 | 0.359424 | |
0.49 | 0.367353 | |
0.5 | 0.375 | |
0.51 | 0.382347 | |
0.52 | 0.389376 | |
0.53 | 0.396069 | |
0.54 | 0.402408 | |
0.55 | 0.408375 | |
0.56 | 0.413952 | |
0.57 | 0.419121 | |
0.58 | 0.423864 | |
0.59 | 0.428163 | |
0.6 | 0.432 | |
0.61 | 0.435357 | |
0.62 | 0.438216 | |
0.63 | 0.440559 | |
0.64 | 0.442368 | |
0.65 | 0.443625 | |
0.66 | 0.444312 | |
0.67 | 0.444411 | |
0.68 | 0.443904 | |
0.69 | 0.442773 | |
0.7 | 0.441 | |
0.71 | 0.438567 | |
0.72 | 0.435456 | |
0.73 | 0.431649 | |
0.74 | 0.427128 | |
0.75 | 0.421875 | |
0.76 | 0.415872 | |
0.77 | 0.409101 | |
0.78 | 0.401544 | |
0.79 | 0.393183 | |
0.8 | 0.384 | |
0.81 | 0.373977 | |
0.82 | 0.363096 | |
0.83 | 0.351339 | |
0.84 | 0.338688 | |
0.85 | 0.325125 | |
0.86 | 0.310632 | |
0.87 | 0.295191 | |
0.88 | 0.278784 | |
0.89 | 0.261393 | |
0.9 | 0.243 | |
0.91 | 0.223587 | |
0.92 | 0.203136 | |
0.93 | 0.181629 | |
0.94 | 0.159048 | |
0.95 | 0.135375 | |
0.96 | 0.110592 | |
0.97 | 0.084681 | |
0.98 | 0.057624 | |
0.99 | 0.029403 |
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 itertools | |
import math | |
def combo(n, r): | |
"""nCr""" | |
return math.factorial(n) / math.factorial(n - r) / math.factorial(r) | |
def prob_exact(heads, tails, prob_h): | |
"""Binomial distribution of `heads` successes in `heads + tails` trials""" | |
n = heads + tails | |
return combo(n, heads) * pow(prob_h, heads) * pow(1 - prob_h, tails) | |
def seq(start, end, step): | |
"""Like range, for floating point steps""" | |
sample_count = (end - start) / step | |
return itertools.islice(itertools.count(start, step), sample_count) | |
def prob_seq(prob_head, seq): | |
"""Maximum likelihood estimator given a sequence of heads(1) and tails(0) and | |
probabilility of heads""" | |
heads = len(filter(lambda i: i == "1", seq)) | |
tails = len(seq) - heads | |
return prob_exact(heads, tails, prob_head) | |
def plot_distribution(): | |
"""Plot the MLE curve""" | |
probs = ((i, prob_seq(i, "101")) for i in seq(0, 1, 0.01)) | |
def prob_to_str(k, v): | |
return "{},{}".format(k, v) | |
prob_strs = (prob_to_str(k, v) for k, v in probs) | |
s = "\n".join(prob_strs) | |
with open("mle.csv", 'w') as f: | |
f.write(s) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment