Created
July 22, 2020 09:11
-
-
Save kengggg/33def2af7f1d1a868e9c9f2f6c0e03e9 to your computer and use it in GitHub Desktop.
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 | |
# SUPPORTED CONFIDENCE LEVELS: 50%, 68%, 90%, 95%, and 99% | |
confidence_level_constant = [50,.67], [68,.99], [90,1.64], [95,1.96], [99,2.57] | |
# CALCULATE THE SAMPLE SIZE | |
def sample_size(population_size, confidence_level, confidence_interval): | |
Z = 0.0 | |
p = 0.5 | |
e = confidence_interval/100.0 | |
N = population_size | |
n_0 = 0.0 | |
n = 0.0 | |
# LOOP THROUGH SUPPORTED CONFIDENCE LEVELS AND FIND THE NUM STD | |
# DEVIATIONS FOR THAT CONFIDENCE LEVEL | |
for i in confidence_level_constant: | |
if i[0] == confidence_level: | |
Z = i[1] | |
if Z == 0.0: | |
return -1 | |
# CALC SAMPLE SIZE | |
n_0 = ((Z**2) * p * (1-p)) / (e**2) | |
# ADJUST SAMPLE SIZE FOR FINITE POPULATION | |
n = n_0 / (1 + ((n_0 - 1) / float(N)) ) | |
return int(math.ceil(n)) # THE SAMPLE SIZE | |
def main(): | |
sample_sz = 0 | |
population_sz = 172881 | |
confidence_level = 99.0 | |
confidence_interval = .5 | |
total_polling_stations = 227 | |
avg_voters_per_station = population_sz / total_polling_stations | |
sample_sz = sample_size(population_sz, confidence_level, confidence_interval) | |
vote62_sample_size = sample_sz / avg_voters_per_station | |
print("Number of polling stations required for .5% margin of error: {}".format(vote62_sample_size)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment