def aux_gamma_beta(self, i, gamma, j, beta): # logging.info("(%d,%d): g %.4f; b %.4f", i, j, gamma, beta) stv_kls = np.zeros(self.n_tests) sntv_liars_kls = np.zeros(self.n_tests) sntv_kls = np.zeros(self.n_tests) for test_nr in range(self.n_tests): true_preferences = PreferenceCreator(self.n, self.m, self.political_spectrum).create_preferences() poll_results = get_poll(true_preferences, gamma) man_ballot = manipulate(true_preferences, np.nonzero(poll_results < self.electoral_threshold)[0], beta) first_distribution = get_first_choice_dist(true_preferences) stv_scores, *_ = voting.STV_scores(true_preferences, self.electoral_threshold, percentage=True) stv_outcome = apportion.largest_remainder(stv_scores, self.seats) stv_kls[test_nr] = utils.kl_divergence(stv_outcome / self.seats, first_distribution) # SNTV outcome truthful sntv_scores, *_ = voting.SNTV_scores(true_preferences, self.electoral_threshold, percentage=True) sntv_outcome = apportion.largest_remainder(sntv_scores, self.seats) sntv_kls[test_nr] = utils.kl_divergence(sntv_outcome / self.seats, first_distribution) del sntv_scores, sntv_outcome # SNTV outcome liars sntv_scores, *_ = voting.SNTV_scores(man_ballot, self.electoral_threshold, percentage=True) sntv_outcome = apportion.largest_remainder(sntv_scores, self.seats) sntv_liars_kls[test_nr] = utils.kl_divergence(sntv_outcome / self.seats, first_distribution) logging.info("(%d,%d): g %.4f; b %.4f; kl-sntv_liars: %.4f", i, j, gamma, beta, sntv_liars_kls.mean()) return i, j, (stv_kls.mean(), sntv_kls.mean(), sntv_liars_kls.mean())
def lalala(electoral_threshold, m, n, n_tests, political_spectrum, seats, beta, poll_covid): stv_propor = [] sntv_propor = [] for test_nr in range(n_tests): true_preferences = PreferenceCreator(n, m, political_spectrum).create_preferences() poll_results = get_poll(true_preferences, poll_covid) man_ballot = manipulate(true_preferences, np.nonzero(poll_results < electoral_threshold)[0], beta) # STV outcome # TODO reestablish the natural order stv_scores, *_ = voting.STV_scores(true_preferences, electoral_threshold, percentage=True) stv_outcome = apportion.largest_remainder(stv_scores, seats) # SNTV outcome sntv_scores, *_ = voting.SNTV_scores(man_ballot, electoral_threshold, percentage=True) sntv_outcome = apportion.largest_remainder(sntv_scores, seats) first_distribution = get_first_choice_dist(true_preferences) # Proportionality stv_propor.append(utils.kl_divergence(stv_outcome / seats, first_distribution)) sntv_propor.append(utils.kl_divergence(sntv_outcome / seats, first_distribution)) return np.array(stv_propor).mean(), np.array(sntv_propor).mean()
def test_gamma_beta(electoral_threshold, m, n, n_tests, political_spectrum, seats): steps = 3 gammass = np.geomspace(0.0001, 1, num=steps) betass = np.linspace(0, 1, num=steps) results = {'stv': np.zeros((steps, steps)), 'sntv': np.zeros((steps, steps))} for i, gamma in enumerate(gammass): for j, beta in enumerate(betass): logging.info("g %.4f; b %.4f", gamma, beta) stv_kls = np.zeros(n_tests) sntv_kls = np.zeros(n_tests) for test_nr in range(n_tests): true_preferences = PreferenceCreator(n, m, political_spectrum).create_preferences() poll_results = get_poll(true_preferences, gamma) man_ballot = manipulate(true_preferences, np.nonzero(poll_results < electoral_threshold)[0], beta) first_distribution = get_first_choice_dist(true_preferences) stv_scores, *_ = voting.STV_scores(true_preferences, electoral_threshold, percentage=True) stv_outcome = apportion.largest_remainder(stv_scores, seats) stv_kls[test_nr] = utils.kl_divergence(stv_outcome / seats, first_distribution) # SNTV outcome sntv_scores, *_ = voting.SNTV_scores(man_ballot, electoral_threshold, percentage=True) sntv_outcome = apportion.largest_remainder(sntv_scores, seats) sntv_kls[test_nr] = utils.kl_divergence(sntv_outcome / seats, first_distribution) results['stv'][i, j] = stv_kls.mean() results['sntv'][i, j] = sntv_kls.mean() return np.meshgrid(gammass, betass), results
def test_alpha(electoral_threshold, m, n, n_tests, political_spectrum, seats, poll_covid): props = [] # progressbar = tqdm(total=50) alphass = np.linspace(0.01, 1, num=10) for i, alpha in enumerate(alphass): print(i) # progressbar.update() astv_propor = [] astv_l_propor = [] for test_nr in range(n_tests): true_preferences = PreferenceCreator(n, m, political_spectrum).create_preferences() poll_results = get_poll(true_preferences, poll_covid) man_ballot = manipulate(true_preferences, np.nonzero(poll_results < electoral_threshold)[0], 1.) first_distribution = get_first_choice_dist(true_preferences) # a-STV outcome astv_scores, *_ = voting.alpha_STV_scores(true_preferences, alpha, electoral_threshold, percentage=True) astv_outcome = apportion.largest_remainder(astv_scores, seats) # a-STV liars outcome astv_l_scores, *_ = voting.alpha_STV_scores(man_ballot, alpha, electoral_threshold, percentage=True) astv_l_outcome = apportion.largest_remainder(astv_l_scores, seats) astv_propor.append(utils.kl_divergence(astv_outcome / seats, first_distribution)) astv_l_propor.append(utils.kl_divergence(astv_l_outcome / seats, first_distribution)) a = np.array(astv_propor).mean() al = np.array(astv_l_propor).mean() # a = lalala(electoral_threshold, m, n, n_tests, political_spectrum, seats, beta, 0.01) props.append((a, al)) # progressbar.close() mean_astv_prop, mean_astv_l_prop = list(zip(*props)) a = np.array(mean_astv_prop) b = np.array(mean_astv_l_prop) # print(a, b) plt.plot(alphass, a, label='a-stv') plt.plot(alphass, b, label='a-stv-liars') plt.legend() plt.show()