-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll.py
98 lines (67 loc) · 2.6 KB
/
poll.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
############################################
# MATHEMATICS #
############################################
# #
# MONFA-MATAS Patricica & ROZET Corentin #
# #
# Project : 209poll_2019 #
# #
############################################
from sys import argv
from math import sqrt
CONFIDENCE95_COEF = 1.96
CONFIDENCE99_COEF = 2.58
class Poll():
"""
Main class that allows computation and output printing.
"""
def __init__(self):
self._pSize = int(argv[1])
self._sSize = int(argv[2])
self._p = float(argv[3])
self._variance = 0
def showInputInfomations(self) -> None:
def showPopulationSize() -> None:
"""
Print out the population size.
"""
print("Population size:\t{}".format(self._pSize))
def showSampleSize() -> None:
"""
Print out the sample size.
"""
print("Sample size:\t\t{}".format(self._sSize))
def showVotingIntentions() -> None:
"""
Print out the votin intention percentage.
"""
print("Voting intentions:\t{:.2f}%".format(self._p))
showPopulationSize()
showSampleSize()
showVotingIntentions()
def variance(self) -> None:
"""
Compute and print the variance.
"""
variance = (self._p * (100-self._p)) / 10000
emean = (self._pSize - self._sSize) / (self._pSize - 1)
self._variance = (variance / self._sSize) * emean
print("Variance:\t\t{:.6f}".format(self._variance))
def confidence(self, coef, percentage) -> None:
"""
Compute and print the 'percentage' confidence interval amplitude.
"""
confidence = coef * sqrt(self._variance) * 100
lowestConfidence = 0 if self._p - confidence < 0 else self._p - confidence
HighestConfidence = 100 if self._p + confidence > 100 else self._p + confidence
print("{}% confidence interval: [{:.2f}%; {:.2f}%]".format(percentage,
lowestConfidence,
HighestConfidence))
def run(self) -> None:
"""
Run computations and process output printing.
"""
self.showInputInfomations()
self.variance()
self.confidence(CONFIDENCE95_COEF, 95)
self.confidence(CONFIDENCE99_COEF, 99)