forked from Rezayovan/RLA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BayesianPolling.py
60 lines (53 loc) · 2.52 KB
/
BayesianPolling.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
from .shared_objects.arrange_candidates import get_winners
from .bptool.bptool import compute_win_probs
class BayesianPolling():
""" Bayesian ballot-polling audit in Python
A wrapper class around Ron Rivest's [2018-bptool](https://github.com/ron-rivest/2018-bptool).
"""
def __init__(self, votes_array, num_ballots, num_winners,\
risk_limit, seed, sample_tallies, num_trials):
super().__init__()
assert all(votes >= 0 for votes in votes_array)
self.votes_array = votes_array
assert num_ballots >= sum(votes_array)
self.num_ballots = num_ballots
assert num_winners < len(votes_array)
self.num_winners = num_winners
self.sample_tallies = sample_tallies
assert 0. < risk_limit <= 1.
self.risk_limit = risk_limit
self.seed = seed
self.num_trials = num_trials
def bayesian_polling_audit(self):
"""
Uses compute_win_probs to verify the election results.
Returns true IFF the reported winners and Bayesian projected
winners match AND the probability of each projected winner
winning is greater than the significance level.
"""
reported_winners = get_winners(self.votes_array, self.num_winners)
bayesian_win_probs = compute_win_probs([self.sample_tallies],\
[self.num_ballots],\
self.seed,\
self.num_trials,\
self.votes_array,\
self.num_winners)
bayesian_win_probs.sort(key=lambda t: t[1], reverse=True)
bayesian_winners = bayesian_win_probs[:self.num_winners]
reported_set = {(w+1) for w in reported_winners}
if not len(bayesian_winners) == len(reported_winners):
return False
for projected in bayesian_winners:
if not projected[0] in reported_set:
return False
if not projected[1] >= 1 - self.risk_limit:
return False
return True
def run_audit(self):
try:
audit_result = self.bayesian_polling_audit()
if audit_result:
return "Audit completed: the results stand.", "success"
return "Failed to confirm the results. Sample a larger portion of the ballots. This may indicate that your reported winners are incorrect.", "danger"
except:
return "Exception raised"