r2b2.tests.test_audit

Module Contents

class r2b2.tests.test_audit.SimpleAudit(alpha: float, beta: float, max_fraction_to_draw: int, replacement: bool, contest: r2b2.contest.Contest)

Bases: r2b2.audit.Audit

For testing purposes only.

Create an instance of an Audit.

Note

This should only be called when initializing a subclass as the Audit class is an abstract class.

get_min_sample_size(self, sub_audit: r2b2.audit.PairwiseAudit)

Get the minimum valid sample size in a sub audit

Parameters

sub_audit (PairwiseAudit) – Get minimum sample size for this sub_audit.

next_sample_size(self)

Generate estimates of possible next sample sizes.

Note: To be used during live/interactive audit execution.

stopping_condition_pairwise(self, pair: str, verbose: bool)bool

Determine if pairwise subcontest meets stopping condition.

Parameters

pair (str) – Dictionary key referencing pairwise audit in audit’s sub_audits.

Returns

bool – If the pairwise subaudit has stopped.

next_min_winner_ballots_pairwise(self, sub_audit: r2b2.audit.PairwiseAudit)

Compute next stopping size of given (actual) sample size for a subaudit.

Parameters

sub_audit (PairwiseAudit) – Compute next stopping size for this subaudit.

Note: To be used during live/interactive audit execution.

compute_risk(self, sub_audit: r2b2.audit.PairwiseAudit)

Compute the current risk level of a given subaudit.

Returns

Current risk level of the audit (as defined per audit implementation).

compute_min_winner_ballots(self, sub_audit: r2b2.audit.PairwiseAudit)

Compute the stopping size(s) for any number of sample sizes for a given subaudit.

compute_all_min_winner_ballots(self, sub_audit: r2b2.audit.PairwiseAudit)

Compute all stopping sizes from the minimum sample size on for a given subaudit.

get_risk_level(self)

Find the risk level of the audit, that is, the smallest risk limit for which the audit as it has panned out would have already stopped.

Returns

float – Risk level of the realization of the audit.

__repr__(self)

String representation of Audit class.

Note

Can (and perhaps should) be overwritten in subclass.

__str__(self)

Human readable string (i.e. printable) representation of Audit class.

Note

Can (and perhaps should) be overwritten in subclass.

current_dist_null(self)

Update distribution_null for each sub audit for current round.

_current_dist_null_pairwise(self, sub_audit: PairwiseAudit, bulk_use_round_size=False)

Update distribution_null for a single PairwiseAudit

Parameters
  • sub_audit (PairwiseAudit) – Pairwise subaudit for which to update distribution.

  • bulk_use_round_size (bool) – Optional argument used by bulk methods. Since the bulk methods do not sample ballots for candidates during the rounds, this flag simply uses the round schedule as the round draw (instead of the pairwise round draw) for updating the distribution. Default is False.

current_dist_reported(self)

Update distribution_reported_tally for each subaudit for current round.

_current_dist_reported_pairwise(self, sub_audit: PairwiseAudit, bulk_use_round_size=False)

Update dist_reported for a single PairwiseAudit.

Parameters
  • sub_audit (PairwiseAudit) – Pairwise subaudit for which to update distriution.

  • bulk_use_round_size (bool) – Optional argument used by bulk methods. Since the bulk methods do not sample ballots for candidates during the rounds, this flag simply uses the round schedule as the round draw (instead of the pairwise round draw) for updating the distribution. Default is False.

truncate_dist_null(self)

Update risk schedule and truncate null distribution for each subaudit.

_truncate_dist_null_pairwise(self, pair: str)

Update risk schedule and truncate null distribution for a single subaudit.

Parameters

pair (str) – Dictionary key for subaudit (within the audit’s subaudits) to truncate distribution and update risk schedule.

truncate_dist_reported(self)

Update stopping prob schedule and truncate reported distribution for each subaudit.

_truncate_dist_reported_pairwise(self, pair)

Update stopping prob schedule and truncate reported distribution for a single subaudit.

Parameters

pair (str) – Dictionary key for subaudit (within the audit’s subaudits) to truncate distribution and update stopping prob schedule.

__get_interval(self, dist: List[float])

Get relevant interval [l, u] of given distribution.

Find levels l and u such that cdf(l) < tolerance and 1 - cdf(u) < tolerance. The purpose of this is to improve efficiency in the current_dist_* functions for audits without replacement where almost all of the hypergeometric distribution falls in a fraction of its range, i.e. between l and u.

Note

cdf() in this context does not require cdf(infinity) = 1, although the distribution should sum very closely to 1.

asn(self, pair: str)

Compute ASN as described in BRAVO paper for pair of candidates.

Given the fractional margin for the reported winner and the risk limit (alpha) produce the average number of ballots sampled during the audit.

Parameters

pair (str) – Dictionary key referencing pairwise audit in audit’s subaudits.

Returns

int – ASN value.

execute_round(self, sample_size: int, sample: dict, verbose: bool = False)bool

Execute a single, non-interactive audit round.

Executes 1 round of the audit, given its current state. If the audit is stopped, its state will not be modified.

Warning: This method assumes the audit is in the correct state to be executed. If multiple

executions of a full audit will be run the same audit object, make sure to call reset on the audit object between full executions.

Parameters
  • sample_size (int) – Total ballots sampled by the end of this round (cumulative).

  • sample (dict) – Sample counts for each candidate by the end of this round (cumulative).

Returns

bool – True if the audit met its stopping condition by this round.

run(self, verbose: bool = False)

Begin interactive audit execution.

Begins the interactive version of the audit. While computations for different audits will vary, the process for executing each one is the same. This provides a process for selecting a sample size, determining if the ballots found for the reported winner in that sample size meet the stopping condition(s), and if not continuing with the audit. As the audit proceeds, data including round sizes, ballots for the winner in each round size, and per round risk and stopping probability are stored.

_reset(self)

Reset attributes modified during run().

stopping_condition(self, verbose: bool = False)bool

Determine if the audits stopping condition has been met.

The audit stopping condition is met if and only if each pairwise stopping condition is met.

next_min_winner_ballots(self, verbose: bool = False)

Compute next stopping size of given (actual) sample sizes for all subaudits.

r2b2.tests.test_audit.test_simple_audit()

Tests creation of a basic Audit object.

r2b2.tests.test_audit.test_simple_audit_execution()

Test basic properties of updating attributes.

r2b2.tests.test_audit.test_simple_audit_execute_rounds()

Test execute_round method.

r2b2.tests.test_audit.test_initialization_errors()

Tests exceptions are raised correctly by __init__().