-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathround_sizes.py
397 lines (308 loc) · 15.4 KB
/
round_sizes.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""
The functions in this file are used to find the minimum round sizes that
produce a desired probability of stopping. Contest-wide Minerva and R2 Bravo
audits as well as 2-strata audits with either Minerva or R2 Bravo are
included.
Additionally there are functions for computing the probablility distribution
over possible pvalues for given round sizes in a 2-strata audit.
Oliver Broadrick 2020
"""
import time
import numpy as np
import scipy as sp
import scipy.stats
import scipy.optimize
from ballot_comparison import ballot_comparison_pvalue
from sprt import ballot_polling_sprt
import matplotlib.pyplot as plt
import numpy.testing
from contest import ContestType
from contest import Contest
from minerva_s import Minerva_S
from fishers_combination import create_modulus, maximize_fisher_combined_pvalue, calculate_lambda_range
from scipy.stats import binom
import math
import matplotlib.pyplot as plt
from simulations import minerva_pvalue_direct_count, r2bravo_pvalue_direct_count
def compute_dist_over_pvalues(N_w1, N_l1, N_w2, N_l2, n1, n2, alpha, underlying=None):
"""
Computes and returns lists of k values, their associated combined pvalue,
and their probability under the null hypothesis for a 2-strata audit using
Minerva in the ballot polling stratum. Assumes no errors in the comparisons.
Args:
N_w1 (int): reported number of votes for the winner in the comparison stratum
N_l1 (int): reported number of votes for the loser in the comparison stratum
N_w2 (int): reported number of votes for the winner in the polling stratum
N_l2 (int): reported number of votes for the loser in the polling stratum
n1 (int): number of comparisons
n2 (int): first round size in the polling stratum
alpha (float): risk limit
underlying (dict): feature not yet implemented (coming soon to a repo near you!)
Return {}:
possible_winner_votes ([int]): possible number of winner votes in the polling sample
dist_over_winner_votes ([float]): probability of each possible number of winner votes
pvalues ([float]): combined pvalue resulting from each possible number of winner votes
"""
N_1 = N_w1 + N_l1
N_2 = N_w2 + N_l2
margin = N_w1 + N_w2 - N_l1 - N_l2
feasible_lambda_range=calculate_lambda_range(N_w1, N_l1, N_1, N_w2, N_l2, N_2)
possible_winner_votes = range(0, n2 + 1)
dist_over_winner_votes = binom.pmf(possible_winner_votes, n2, N_w2 / N_2)
pvalues = []
for k, pr_k in zip(possible_winner_votes, dist_over_winner_votes):
cvr_pvalue = lambda alloc: ballot_comparison_pvalue(n=n1, gamma=1.03905, \
o1=0, u1=0, o2=0, u2=0,
reported_margin=margin, N=N_1,
null_lambda=alloc)
mod = create_modulus(n1, n2, k, n2 - k, N_1, margin, 1.03905)
nocvr_pvalue = lambda alloc: \
minerva_pvalue_direct_count(winner_votes=k, n=n2, popsize=N_2, alpha=alpha, \
Vw=N_w2, Vl=N_l2, \
null_margin=(N_w2-N_l2) - alloc*margin)
pvalue = maximize_fisher_combined_pvalue(N_w1, N_l1, \
N_1, N_w2, N_l2, N_2, \
pvalue_funs=[cvr_pvalue, nocvr_pvalue], \
modulus=mod, alpha=alpha, \
feasible_lambda_range=feasible_lambda_range)['max_pvalue']
pvalues.append(pvalue)
#print("for k="+str(k)+" pval="+str(pvalue))
return {
"possible_winner_votes":possible_winner_votes,
"dist_over_winner_votes":dist_over_winner_votes,
"pvalues":pvalues
}
def compute_stopping_probability_whole_dist(N_w1, N_l1, N_w2, N_l2, n1, n2, alpha, underlying=None):
"""
Computes the stopping probability for the given polling stratum first
round sizes in a 2-strata audit with Minerva.
Computes the full probability distribution over pvalues to do so. (AKA real slow)
Note/Plan: Come back and search for kmin then find pr[k >= kmin | alt]
Should work and be faster...
Args:
N_w1 (int): reported number of votes for the winner in the comparison stratum
N_l1 (int): reported number of votes for the loser in the comparison stratum
N_w2 (int): reported number of votes for the winner in the polling stratum
N_l2 (int): reported number of votes for the loser in the polling stratum
n1 (int): number of comparisons
n2 (int): first round size in the polling stratum
alpha (float): risk limit
underlying (dict): feature not yet implemented (coming soon to a repo near you!)
Return (float):
the probability of stopping for the given round sizes
"""
results = compute_dist_over_pvalues(N_w1, N_l1, N_w2, N_l2, n1, n2, alpha, underlying=None)
possible_winner_votes = results["possible_winner_votes"]
dist_over_winner_votes = results["dist_over_winner_votes"]
pvalues = results["pvalues"]
# find the index of the first pvalue that passes the stopping condition
index = None
for i,pvalue in zip(range(0, n2 + 1), pvalues):
if (pvalue <= alpha):
index = i
break
# if there is not such index then the probability of stopping is 0
if (index is None):
return 0
prob_stop = sum(dist_over_winner_votes[index:])
return prob_stop
def compute_stopping_probability(N_w1, N_l1, N_w2, N_l2, n1, n2, alpha, underlying=None):
"""
Computes the stopping probability for the given strata sample sizes for
a 2-strata audit with Minerva for the polling stratum by finding
kmin, then finding Pr[k >= kmin | alt].
NOT YET DONE
Args:
N_w1 (int): reported number of votes for the winner in the comparison stratum
N_l1 (int): reported number of votes for the loser in the comparison stratum
N_w2 (int): reported number of votes for the winner in the polling stratum
N_l2 (int): reported number of votes for the loser in the polling stratum
n1 (int): number of comparisons
n2 (int): first round size in the polling stratum
alpha (float): risk limit
underlying (dict): feature not yet implemented (coming soon to a repo near you!)
Return (float):
the probability of stopping for the given round sizes
"""
results = compute_dist_over_pvalues(N_w1, N_l1, N_w2, N_l2, n1, n2, alpha, underlying=None)
possible_winner_votes = results["possible_winner_votes"]
dist_over_winner_votes = results["dist_over_winner_votes"]
pvalues = results["pvalues"]
# find the index of the first pvalue that passes the stopping condition
index = None
for i,pvalue in zip(range(0, n2 + 1), pvalues):
if (pvalue <= alpha):
index = i
break
# if there is not such index then the probability of stopping is 0
if (index is None):
return 0
prob_stop = sum(dist_over_winner_votes[index:])
return prob_stop
def find_sample_size_for_stopping_prob_efficiently(stopping_probability, N_w1, N_l1, N_w2, N_l2, n1, alpha, underlying=None, right=None):
"""
This function will also compute minimum round size for the
passed stopping probability, but it will do so much more
efficiently. At each point in the search only one pvalue
will be computed. Should have done it this way to begin with.
Uses Minerva for the ballot polling stratum.
"""
N_1 = N_w1 + N_l1
N_2 = N_w2 + N_l2
margin = N_w1 + N_w2 - N_l1 - N_l2
feasible_lambda_range = calculate_lambda_range(N_w1, N_l1, N_1, N_w2, N_l2, N_2)
left = 1
if (right is None):
right = N_2
print("right: "+str(right))
while(1):
n2 = math.ceil((left + right) / 2 )
# compute the 1 - stopping_probability quantile of the alt dist
# kmax where pr[k >= kmax | alt] = stopping_probability
# floor because we need to ensure at least a stopping_probability prob of stopping
kmax = math.floor(binom.ppf(1 - stopping_probability, n2, N_w2 / N_2))
# compute pvalue for this kmax
cvr_pvalue = lambda alloc: ballot_comparison_pvalue(n=n1, gamma=1.03905, \
o1=0, u1=0, o2=0, u2=0,
reported_margin=margin, N=N_1,
null_lambda=alloc)
mod = create_modulus(n1, n2, kmax, n2 - kmax, N_1, margin, 1.03905)
nocvr_pvalue = lambda alloc: \
minerva_pvalue_direct_count(winner_votes=kmax, n=n2, popsize=N_2, alpha=alpha, \
Vw=N_w2, Vl=N_l2, \
null_margin=(N_w2-N_l2) - alloc*margin)
combination_results = maximize_fisher_combined_pvalue(N_w1, N_l1, \
N_1, N_w2, N_l2, N_2, \
pvalue_funs=[cvr_pvalue, nocvr_pvalue], \
modulus=mod, alpha=alpha, \
feasible_lambda_range=feasible_lambda_range)
pvalue = combination_results['max_pvalue']
pvalue_comparison = combination_results['pvalue1']
pvalue_polling = combination_results['pvalue2']
alloc_lambda = combination_results['allocation lambda']
# update binary search bounds
if (pvalue > alpha):
left = n2
elif (pvalue <= alpha):
right = n2
# if left = right then the initial right bound was too small
if (right == left):
print("required round size is too larger")
return None # not sure if returning None is proper thing to do here...
# when and right converge, right is the minimum round size that achieves stopping_probability
if (left == right - 1 and n2 == right):
print(combination_results['refined'])
return {
"round_size":right,
"combined_pvalue":pvalue,
"comparison_pvalue":pvalue_comparison,
"polling_pvalue":pvalue_polling,
"alloc_lambda":alloc_lambda
}
def find_sample_size_for_stopping_prob_minerva(stopping_probability, N_w, N_l, alpha, underlying=None, right=None):
"""
Finds the first round size that achieves the passed stopping_probability
for a Minerva audit (with no stratification).
"""
N = N_w + N_l
left = 1
if (right is None):
right = N
while(1):
n = math.ceil((left + right) / 2)
# compute the 1 - stopping_probability quantile of the alt dist
# kmax where pr[k >= kmax | alt] = stopping_probability
# floor because we need to ensure at least a stopping_probability prob of stopping
kmax = math.floor(binom.ppf(1 - stopping_probability, n, N_w / N))
# compute pvalue for this kmax
pvalue = minerva_pvalue_direct_count(winner_votes=kmax, n=n, popsize=N, alpha=alpha, Vw=N_w, Vl=N_l, null_margin=0)
# update binary search bounds
if (pvalue > alpha):
left = n
elif (pvalue <= alpha):
right = n
# when and right converge, right is the minimum round size that achieves stopping_probability
if (left == right - 1):
if (right == N):
print("required round size is greater than stratum size")
return right
def find_sample_size_for_stopping_prob_r2bravo(stopping_probability, N_w, N_l, alpha, underlying=None, right=None):
"""
Finds the first round size that achieves the passed stopping_probability
for an R2 Bravo audit (with no stratification).
"""
N = N_w + N_l
left = 1
right = N
while(1):
n = math.ceil((left + right) / 2)
# compute the 1 - stopping_probability quantile of the alt dist
# kmax where pr[k >= kmax | alt] = stopping_probability
# floor because we need to ensure at least a stopping_probability prob of stopping
kmax = math.floor(binom.ppf(1 - stopping_probability, n, N_w / N))
# compute pvalue for this kmax
pvalue = r2bravo_pvalue_direct_count(winner_votes=kmax, n=n, popsize=N, alpha=alpha, Vw=N_w, Vl=N_l, null_margin=0)
# update binary search bounds
if (pvalue > alpha):
left = n
elif (pvalue <= alpha):
right = n
# when and right converge, right is the minimum round size that achieves stopping_probability
if (left == right - 1):
if (right == N):
print("required round size is greater than stratum size")
return right
def find_sample_size_for_stopping_prob_efficiently_r2bravo(stopping_probability, N_w1, N_l1, N_w2, N_l2, n1, alpha, underlying=None, right=None):
"""
This function will also compute minimum round size for the
passed stopping probability, but it will do so much more
efficiently. At each point in the search only one pvalue
will be computed. Should have done it this way to begin with.
"""
N_1 = N_w1 + N_l1
N_2 = N_w2 + N_l2
margin = N_w1 + N_w2 - N_l1 - N_l2
feasible_lambda_range=calculate_lambda_range(N_w1, N_l1, N_1, N_w2, N_l2, N_2)
left = 1
right = N_2
while(1):
n2 = math.ceil((left + right) / 2)
# compute the 1 - stopping_probability quantile of the alt dist
# kmax where pr[k >= kmax | alt] = stopping_probability
# floor because we need to ensure at least a stopping_probability prob of stopping
kmax = math.floor(binom.ppf(1 - stopping_probability, n2, N_w2 / N_2))
# compute pvalue for this kmax
cvr_pvalue = lambda alloc: ballot_comparison_pvalue(n=n1, gamma=1.03905, \
o1=0, u1=0, o2=0, u2=0,
reported_margin=margin, N=N_1,
null_lambda=alloc)
mod = create_modulus(n1, n2, kmax, n2 - kmax, N_1, margin, 1.03905)
nocvr_pvalue = lambda alloc: \
r2bravo_pvalue_direct_count(winner_votes=kmax, n=n2, popsize=N_2, alpha=alpha, \
Vw=N_w2, Vl=N_l2, \
null_margin=(N_w2-N_l2) - alloc*margin)
combination_results = maximize_fisher_combined_pvalue(N_w1, N_l1, \
N_1, N_w2, N_l2, N_2, \
pvalue_funs=[cvr_pvalue, nocvr_pvalue], \
modulus=mod, alpha=alpha, \
feasible_lambda_range=feasible_lambda_range)
pvalue = combination_results['max_pvalue']
pvalue_comparison = combination_results['pvalue1']
pvalue_polling = combination_results['pvalue2']
alloc_lambda = combination_results['allocation lambda']
# update binary search bounds
if (pvalue > alpha):
left = n2
elif (pvalue <= alpha):
right = n2
# when and right converge, right is the minimum round size that achieves stopping_probability
if (left == right - 1 and n2 == right):
if (right == N_2):
print("requried round size is greater than stratum size")
return {
"round_size":right,
"combined_pvalue":pvalue,
"comparison_pvalue":pvalue_comparison,
"polling_pvalue":pvalue_polling,
"alloc_lambda":alloc_lambda
}