-
Notifications
You must be signed in to change notification settings - Fork 4
/
tomlinson_2023_figure_3_updated.py
152 lines (116 loc) · 4.96 KB
/
tomlinson_2023_figure_3_updated.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
"""
Show the winner distributions and bias of different voting methods with uniform
distribution of voters and candidates.
Similar to Figure 3
The distributions of the winning position with k = 3, 4, 5 candidates and
continuous 1-Euclidean voters (both uniformly distributed) under plurality and
IRV.
from
Kiran Tomlinson, Johan Ugander, Jon Kleinberg (2023) Moderation in instant
runoff voting https://arxiv.org/abs/2303.09734
"""
import pickle
from collections import defaultdict
import matplotlib.pyplot as plt
import numpy as np
from joblib import Parallel, delayed
from seaborn import histplot
from elsim.elections import normed_dist_utilities
from elsim.methods import (approval, black, borda, coombs, fptp, irv, runoff,
star)
from elsim.strategies import (approval_optimal, honest_normed_scores,
honest_rankings, vote_for_k)
n_elections = 100_000 # Roughly 1 minute on a 2019 6-core i7-9750H
n_voters = 1_000
n_cands = 5
cand_dist = 'uniform'
u_width = 5
disp = 1
vote_for = n_cands//2
# Simulate more than just one election per worker to improve efficiency
batch_size = 100
n_batches = n_elections // batch_size
assert n_batches * batch_size == n_elections
def ceildiv(a, b):
return -(a // -b)
def human_format(num):
for unit in ['', 'k', 'M', 'B', 'T']:
if abs(num) < 1000:
return f"{num:.3g}{unit}"
num /= 1000.0
def simulate_batch(n_cands):
winners = defaultdict(list)
for iteration in range(batch_size):
# v, c = normal_electorate(n_voters, n_cands, dims=1, disp=disp)
if cand_dist == 'uniform':
# Replace with uniform distribution of candidates of same shape
v = np.random.uniform(-u_width/2, +u_width/2, n_voters)
v = np.atleast_2d(v).T
c = np.random.uniform(-u_width/2, +u_width/2, n_cands)
c = np.atleast_2d(c).T
# FPTP voting method
utilities = normed_dist_utilities(v, c)
rankings = honest_rankings(utilities)
winner = fptp(rankings, tiebreaker='random')
winners['First Past The Post / Plurality'].append(c[winner][0])
# Top-two runoff
utilities = normed_dist_utilities(v, c)
rankings = honest_rankings(utilities)
winner = runoff(rankings, tiebreaker='random')
winners['Top-Two Runoff/Primary / Two-Round System / '
'Contingent Vote'].append(c[winner][0])
# Instant-runoff
winner = irv(rankings, tiebreaker='random')
winners['Ranked-Choice Voting (Hare) / '
'Alternative Vote / Instant-Runoff'].append(c[winner][0])
# Approval voting
winner = approval(approval_optimal(utilities), tiebreaker='random')
winners['Approval Voting ("optimal" strategy)'].append(c[winner][0])
# Approval voting
winner = approval(vote_for_k(utilities, vote_for), tiebreaker='random')
winners[f'Approval Voting "(Vote-for-{vote_for}"'
' strategy)'].append(c[winner][0])
# STAR voting
ballots = honest_normed_scores(utilities)
winner = star(ballots, tiebreaker='random')
winners['STAR Voting'].append(c[winner][0])
# Borda count
winner = borda(rankings, tiebreaker='random')
winners['Borda count'].append(c[winner][0])
# Coombs method
winner = coombs(rankings, tiebreaker='random')
winners["Coombs' method"].append(c[winner][0])
# Condorcet RCV
winner = black(rankings, tiebreaker='random')
winners['Condorcet Ranked-Choice Voting (Black)'].append(c[winner][0])
# Ideal winner method. Votes don't matter at all; pick the center.
winner = np.argmin(abs(c))
winners['Best possible winner (nearest center)'].append(c[winner][0])
return winners
jobs = [delayed(simulate_batch)(n_cands)] * n_batches
print(f'{len(jobs)} tasks total:')
results = Parallel(n_jobs=-3, verbose=5)(jobs)
winners = {k: [v for d in results for v in d[k]] for k in results[0]}
title = f'{human_format(n_elections)} 1D elections, '
title += f'{human_format(n_voters)} voters, '
title += f'{human_format(n_cands)} candidates'
title += ', both ' + cand_dist
fig, ax = plt.subplots(nrows=ceildiv(len(winners), 2), ncols=2, num=title,
sharex=True, constrained_layout=True,
figsize=(11, 9.5))
fig.suptitle(title)
ax = ax.T.flatten() # Flatten the ax array for easier indexing
for n, method in enumerate(winners.keys()):
histplot(winners[method], ax=ax[n], label='Winners', stat='density')
ax[n].set_title(method, loc='left')
ax[n].set_yticklabels([]) # Don't care about numbers
ax[n].set_ylabel("") # No "density"
tmp = ax[n].twinx()
tmp.set_yticklabels([]) # Don't care about numbers
tmp.set_ylabel("") # No "density"
ax[0].set_xlim(-2.5, 2.5)
ax[0].legend()
# These take so long and kernel crashes!
plt.savefig(title + '.png')
with open(title + '.pkl', "wb") as file:
pickle.dump(winners, file)