def collect_votes(self, votes): xtd_votes = add_totals(votes) xtd_shares = find_xtd_shares(xtd_votes) for c in range(self.num_constituencies+1): for p in range(self.num_parties+1): self.aggregate_list(-1, "sim_votes", c, p, xtd_votes[c][p]) self.aggregate_list(-1, "sim_shares", c, p, xtd_shares[c][p])
def beta_distribution( base_votes, #2d - votes for each list, std_param #distribution parameter in range (0,1) ): """ Generate a set of votes with beta distribution, using 'base_votes' as reference. """ assert (0 < std_param and std_param < 1) xtd_votes = add_totals(base_votes) xtd_shares = find_xtd_shares(xtd_votes) generated_votes = [] for c in range(len(base_votes)): s = 0 generated_votes.append([]) for p in range(len(base_votes[c])): mean_beta_distr = xtd_shares[c][p] assert (0 <= mean_beta_distr and mean_beta_distr <= 1) if 0 < mean_beta_distr and mean_beta_distr < 1: var_beta = std_param * mean_beta_distr * (1 - mean_beta_distr) alpha, beta = beta_params(mean_beta_distr, std_param) share = betavariate(alpha, beta) else: share = mean_beta_distr #either 0 or 1 generated_votes[c].append(int(share * xtd_votes[c][-1])) return generated_votes
def __init__(self, sim_rules, e_rules, m_votes, std_param=0.1): self.num_total_simulations = sim_rules["simulation_count"] self.num_rulesets = len(e_rules) self.num_constituencies = len(m_votes) self.num_parties = len(m_votes[0]) assert (all([len(c) == self.num_parties for c in m_votes])) assert (all([ self.num_constituencies == len(ruleset["constituency_names"]) and self.num_constituencies == len(ruleset["constituency_seats"]) and self.num_constituencies == len( ruleset["constituency_adjustment_seats"]) and self.num_parties == len(ruleset["parties"]) for ruleset in e_rules ])) self.sim_rules = sim_rules self.e_rules = e_rules self.base_votes = m_votes self.xtd_votes = add_totals(self.base_votes) self.xtd_vote_shares = find_xtd_shares(self.xtd_votes) self.variate = self.sim_rules["gen_method"] self.std_param = std_param self.iteration = 0 self.terminate = False self.iteration_time = timedelta(0) self.data = [] self.list_data = [] for ruleset in range(self.num_rulesets): self.data.append({}) for measure in MEASURES.keys(): self.data[ruleset][measure] = { aggr: 0 for aggr in AGGREGATES.keys() } self.list_data.append({}) for measure in LIST_MEASURES.keys(): self.list_data[ruleset][measure] = {} for aggr in AGGREGATES.keys(): self.list_data[ruleset][measure][aggr] = [] for c in range(self.num_constituencies + 1): self.list_data[ruleset][measure][aggr].append( [0] * (self.num_parties + 1)) self.data.append({}) self.list_data.append({}) for measure in VOTE_MEASURES.keys(): self.list_data[-1][measure] = {} for aggr in AGGREGATES.keys(): self.list_data[-1][measure][aggr] = [] for c in range(self.num_constituencies + 1): self.list_data[-1][measure][aggr].append( [0] * (self.num_parties + 1)) self.run_initial_elections()
def run_initial_elections(self): self.base_allocations = [] for r in range(self.num_rulesets): election = voting.Election(self.e_rules[r], self.base_votes) xtd_total_seats = add_totals(election.run()) xtd_const_seats = add_totals(election.m_const_seats_alloc) xtd_adj_seats = matrix_subtraction(xtd_total_seats, xtd_const_seats) xtd_seat_shares = find_xtd_shares(xtd_total_seats) self.base_allocations.append({ "xtd_const_seats": xtd_const_seats, "xtd_adj_seats": xtd_adj_seats, "xtd_total_seats": xtd_total_seats, "xtd_seat_shares": xtd_seat_shares, "step_info": election.adj_seats_info, })
def gen_votes(self): """ Generate votes similar to given votes using the given generating method. """ gen = GENERATING_METHODS[self.variate] while True: votes = gen(self.base_votes, self.std_param) xtd_votes = add_totals(votes) xtd_shares = find_xtd_shares(xtd_votes) for c in range(self.num_constituencies + 1): for p in range(self.num_parties + 1): self.aggregate_list(-1, "sim_votes", c, p, xtd_votes[c][p]) self.aggregate_list(-1, "sim_shares", c, p, xtd_shares[c][p]) yield votes