def test_simple_minerva(): simple_minerva = Minerva(.1, .1, default_contest) assert simple_minerva.alpha == .1 assert simple_minerva.beta == 0.0 assert simple_minerva.max_fraction_to_draw == .1 assert len(simple_minerva.rounds) == 0 assert len(simple_minerva.sub_audits) == 1 assert simple_minerva.get_risk_level() is None simple_minerva.rounds.append(10) simple_minerva.stopped = True assert simple_minerva.next_sample_size() == 10 assert simple_minerva.next_sample_size(verbose=True) == (10, 0, 1)
def test_minerva_georgia_senate_2020(): ga_senate_race = Contest(2453876 + 2358432, { 'A': 2453876, 'B': 2358432 }, 1, ['A'], ContestType.PLURALITY) ga_senate_audit = Minerva(.1, 1.0, ga_senate_race) irrelevant_scale_up = 1.0238785631 estimates = [] for sprob in [.7, .8, .9]: estimates.append( math.ceil(irrelevant_scale_up * ga_senate_audit.next_sample_size(sprob=sprob))) assert estimates == [10486, 13205, 18005] ga_senate_audit.execute_round(9903, {'A': 4950, 'B': 9903 - 4950}) assert abs(ga_senate_audit.pvalue_schedule[-1] - 0.527638189598802) < .000001 ga_senate_audit.execute_round(24000, {'A': 11900, 'B': 24000 - 11900}) assert abs(ga_senate_audit.pvalue_schedule[-1] - 2.663358309286826) < .000001 ga_senate_audit.execute_round(45600, {'A': 24000, 'B': 45600 - 24000}) assert abs(ga_senate_audit.pvalue_schedule[-1]) < 0.000001 ga_senate_audit = Minerva(.1, 1.0, ga_senate_race) ga_senate_audit.execute_round(17605, {'A': 8900, 'B': 17605 - 8900}) assert abs(ga_senate_audit.get_risk_level() - 0.081750333563781) < .000001 ga_senate_audit = Minerva(.1, 1.0, ga_senate_race) ga_senate_audit.execute_round(17605, {'A': 17605, 'B': 0}) assert ga_senate_audit.get_risk_level() == 0 ga_senate_audit = Minerva(.1, 1.0, ga_senate_race) ga_senate_audit.execute_round(17605, {'A': 0, 'B': 17605}) assert abs(ga_senate_audit.get_risk_level() - 1) < 0.000001
def test_minerva_second_round_estimate(): contest1 = Contest(100000, { 'A': 60000, 'B': 40000 }, 1, ['A'], ContestType.MAJORITY) minerva1 = Minerva(.1, .1, contest1) minerva1.compute_min_winner_ballots(minerva1.sub_audits['A-B'], [100]) minerva1.sample_ballots['A'].append(54) minerva1.sample_ballots['B'].append(100 - 54) contest2 = Contest(4504975 + 4617886, { 'Trump': 4617886, 'Clinton': 4504975 }, 1, ['Trump'], ContestType.PLURALITY) minerva2 = Minerva(.1, 1.0, contest2) minerva2.compute_min_winner_ballots(minerva2.sub_audits['Trump-Clinton'], [45081]) minerva2.sample_ballots['Trump'].append(22634) minerva2.sample_ballots['Clinton'].append(45081 - 22634) assert minerva1.next_sample_size() == 305 assert minerva2.next_sample_size() == 111257
def test_minerva_first_round_estimate(): contest1 = Contest(100000, { 'A': 60000, 'B': 40000 }, 1, ['A'], ContestType.MAJORITY) minerva1 = Minerva(.1, .1, contest1) contest2 = Contest(100000, { 'A': 51000, 'B': 49000 }, 1, ['A'], ContestType.MAJORITY) minerva2 = Minerva(.1, .1, contest2) contest3 = Contest(10000000, { 'A': 5040799, 'B': 10000000 - 5040799 }, 1, ['A'], ContestType.MAJORITY) minerva3 = Minerva(.1, 1.0, contest3) assert minerva1.next_sample_size() == 179 assert minerva2.next_sample_size() == 17272 assert minerva3.next_sample_size() == 103483
def test_minerva_second_round_estimate_2016(): with open('tests/data/2016_pres_trials.json', 'r') as json_file: data = json.load(json_file) out = {} out['data_check'] = {} for state in data: out['data_check'][state] = {} clinton = data[state]['tally']['Clinton'] trump = data[state]['tally']['Trump'] print(state) tally = {"Clinton": clinton, "Trump": trump} margin = abs((clinton - trump) / (clinton + trump)) if margin < .10: continue contest = Contest(clinton + trump, tally, 1, [max(tally, key=tally.get)], ContestType.PLURALITY) if tally['Clinton'] > tally['Trump']: rep_winner = 'Clinton' rep_loser = 'Trump' else: rep_winner = 'Trump' rep_loser = 'Clinton' for sim_type in [ 'underlying_reported_first_5', 'underlying_reported_not_stop_5', 'underlying_tied_first_5' ]: out['data_check'][state][sim_type] = [] for trial in data[state][sim_type]: n = trial['relevant_sample_size'] k = trial['winner_ballots'] minerva = Minerva(.1, 1.0, contest) minerva.execute_round(n, {rep_winner: k, rep_loser: n - k}) p_value = minerva.get_risk_level() stop = minerva.stopped if stop: minerva.next_min_winner_ballots() minerva.truncate_dist_null() minerva.truncate_dist_reported() next_round_data = minerva.next_sample_size(verbose=True) out['data_check'][state][sim_type].append({ "n": n, "k": k, "p_value": p_value, "stop": bool(stop), "kmin": minerva.sub_audits[rep_winner + '-' + rep_loser].min_winner_ballots[-1], "next_round_size": next_round_data[0], "next_round_kmin": next_round_data[1], "next_round_sprob": next_round_data[2] }) with open( 'tests/data/test_minerva_second_round_estimate_2016.json', 'w') as output: json.dump(out, output, sort_keys=True, indent=4) # Now that the file has been generated, compare to PV version. with open('tests/data/gm_test_minerva_second_round_estimate_2016.json', 'r') as json_file: data_canonical = json.load(json_file) with open('tests/data/test_minerva_second_round_estimate_2016.json', 'r') as json_file: data_test = json.load(json_file) for state in data_canonical['data_check']: if data_canonical['data_check'][state] == {}: continue for sim_type in [ 'underlying_reported_first_5', 'underlying_reported_not_stop_5', 'underlying_tied_first_5' ]: for i in range(5): assert data_canonical['data_check'][state][sim_type][i][ 'n'] == data_test['data_check'][state][sim_type][i]['n'] assert data_canonical['data_check'][state][sim_type][i][ 'k'] == data_test['data_check'][state][sim_type][i]['k'] assert data_canonical['data_check'][state][sim_type][i][ 'kmin'] == data_test['data_check'][state][sim_type][i][ 'kmin'] assert data_canonical['data_check'][state][sim_type][i][ 'stop'] == data_test['data_check'][state][sim_type][i][ 'stop'] assert data_canonical['data_check'][state][sim_type][i]['next_round_size'] == \ data_test['data_check'][state][sim_type][i]['next_round_size'] assert abs(data_canonical['data_check'][state][sim_type][i] ['p_value'] - data_test['data_check'][state] [sim_type][i]['p_value']) < .000001 assert abs(data_canonical['data_check'][state][sim_type][i] ['next_round_sprob'] - data_test['data_check'] [state][sim_type][i]['next_round_sprob']) < .000001