Skip to content

Instantly share code, notes, and snippets.

@scribu
Last active September 22, 2020 13: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 scribu/2270193986ddbf3c14481540533f5054 to your computer and use it in GitHub Desktop.
Save scribu/2270193986ddbf3c14481540533f5054 to your computer and use it in GitHub Desktop.
Poll Result Analysis
# 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