Skip to content

Commit

Permalink
Plot distribution of voters and candidates
Browse files Browse the repository at this point in the history
alongside the other methods, in the same way

Move ideal winner "method" to first column along with them.
  • Loading branch information
endolith committed Aug 6, 2024
1 parent 2f8ec55 commit bd33e03
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions examples/distributions_by_method_2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ def simulate_batch(n_cands):
c = np.atleast_2d(c).T
raise SystemExit

# Voter distribution
winners['Voters'].append(v)

# Candidate distribution
winners['Candidates'].append(c)

# Ideal winner method. Votes don't matter at all; pick the center.
# winner = np.argmin(abs(c)) # 1D
winner = np.argmin(np.sum(c**2, axis=1)) # 2D
winners['Best possible winner'].append(c[winner])

# FPTP voting method
utilities = normed_dist_utilities(v, c)
rankings = honest_rankings(utilities)
Expand Down Expand Up @@ -114,11 +125,6 @@ def simulate_batch(n_cands):
winner = black(rankings, tiebreaker='random')
winners['Condorcet RCV (Black)'].append(c[winner])

# Ideal winner method. Votes don't matter at all; pick the center.
# winner = np.argmin(abs(c)) # 1D
winner = np.argmin(np.sum(c**2, axis=1)) # 2D
winners['Best possible winner'].append(c[winner])

# This could just accumulate winner numbers and then get the coordinates later
# no because c only exists here
# Or it could just accumulate directly to the histogram heatmap in the job
Expand All @@ -134,10 +140,16 @@ def simulate_batch(n_cands):

# %% Measure distributions

winners['Voters'] = winners['Voters'].reshape(-1, winners['Voters'].shape[-1])
winners['Candidates'] = winners['Candidates'].reshape(
-1, winners['Candidates'].shape[-1])

winners_stats = {method: (np.mean(winners[method], axis=0),
np.std(winners[method], axis=0)
) for method in winners.keys()}

assert np.allclose(winners_stats['Voters'][1], [1, 1], rtol=1e-2)

for method, (mean, std) in winners_stats.items():
print(f"{method}:")
print(f"{len(winners[method])} samples")
Expand Down

0 comments on commit bd33e03

Please sign in to comment.