Last active
September 22, 2020 13:49
-
-
Save scribu/2270193986ddbf3c14481540533f5054 to your computer and use it in GitHub Desktop.
Poll Result Analysis
This file contains hidden or 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
# Blog post: https://scribu.net/twitter-poll-analysis.html | |
import sys | |
import math | |
positive = int(sys.argv[1]) | |
z = 1.9599 # 95% confidence | |
n = 7 # number of votes | |
classes = 3 # number of possible answers | |
def standard_error(p, n): | |
"""The estimate of a proportion p out of n samples | |
Arguments: | |
p: proportion | |
n: number of trials | |
""" | |
return np.sqrt(p * (1 - p) / n) | |
def maximum_error(z, n, prior=0.5): | |
""" | |
Maximum error, used for calculating confidence intervals | |
Arguments: | |
z: confidence level | |
n: number of trials | |
""" | |
return z * standard_error(prior, n) | |
e = maximum_error(z, n, 1 / classes) | |
p = positive / n | |
low = max(0, p - e) | |
high = min(1, p + e) | |
# r is the likelyhood that the next vote would be "disagree" | |
print(f"{low:.0%} <= r <= {high:.0%}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment