Created
May 29, 2020 18:27
-
-
Save eteq/cbb1d9cd5271964c5dd96196c5efcd24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import collections\n", | |
"import random" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class VoteComparison:\n", | |
" def __init__(self, Nslots, Nvoters, Ncand):\n", | |
" self.Nslots = Nslots\n", | |
" self.Nvoters = Nvoters\n", | |
" self.Ncand = Ncand\n", | |
" \n", | |
" def random_votes(self):\n", | |
" return np.array([np.random.permutation(self.Ncand) for i in range(self.Nvoters)])\n", | |
" \n", | |
" def first_vote_winners(self, votes):\n", | |
" return collections.Counter(votes[:, 0]).most_common()[:self.Nslots]\n", | |
" \n", | |
" def n_vote_winners(self, votes, nstocount=None): #None means take all of the votes\n", | |
" all_votes = []\n", | |
" if nstocount is None:\n", | |
" for voteset in votes:\n", | |
" all_votes.extend(voteset)\n", | |
" else:\n", | |
" for voteset, ntocount in zip(votes, nstocount):\n", | |
" all_votes.extend(voteset[:ntocount])\n", | |
" return collections.Counter(all_votes).most_common()[:self.Nslots]\n", | |
" \n", | |
" def ranked_vote_winners(self, votes):\n", | |
" winners = []\n", | |
" losers = []\n", | |
" removed_cands = []\n", | |
" while len(winners) < self.Nslots:\n", | |
" cand_votes = [0 for j in range(self.Ncand)]\n", | |
" for vote in votes:\n", | |
" for v in vote:\n", | |
" if v not in removed_cands:\n", | |
" cand_votes[v] += 1\n", | |
" break\n", | |
" counts = {j:cand_votes[j] for j in range(self.Ncand)}\n", | |
" ranked_counts = [(j, counts[j]) for j in np.argsort(list(counts.values()))[::-1]]\n", | |
" if ranked_counts[0][1] != ranked_counts[0][0]:\n", | |
" #winner!\n", | |
" winners.append(ranked_counts[0])\n", | |
" removed_cands.append(ranked_counts[0][0])\n", | |
" else:\n", | |
" losers.append(ranked_counts[-1])\n", | |
" removed_cands.append(ranked_counts[-1][0])\n", | |
" if len(removed_cands) >= self.Nslots:\n", | |
" break\n", | |
" return winners" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Differences 1 to 2! 0\n", | |
"[(1, 12), (3, 11), (4, 9)]\n", | |
"[(1, 12), (3, 15), (0, 19)]\n", | |
"Differences 1 to 3! 0\n", | |
"[(1, 12), (3, 11), (4, 9)]\n", | |
"[(3, 26), (1, 23), (2, 22)]\n", | |
"Differences 2 to 3! 0\n", | |
"[(1, 12), (3, 15), (0, 19)]\n", | |
"[(3, 26), (1, 23), (2, 22)]\n", | |
"Differences 1 to 3! 1\n", | |
"[(0, 12), (1, 12), (4, 11)]\n", | |
"[(1, 27), (3, 19), (0, 18)]\n", | |
"Differences 2 to 3! 1\n", | |
"[(1, 12), (4, 14), (0, 22)]\n", | |
"[(1, 27), (3, 19), (0, 18)]\n", | |
"Differences 1 to 3! 2\n", | |
"[(4, 13), (2, 11), (3, 10)]\n", | |
"[(2, 26), (3, 24), (1, 22)]\n", | |
"Differences 2 to 3! 2\n", | |
"[(4, 13), (2, 15), (3, 20)]\n", | |
"[(2, 26), (3, 24), (1, 22)]\n", | |
"Differences 1 to 2! 3\n", | |
"[(3, 14), (2, 11), (4, 9)]\n", | |
"[(3, 14), (0, 16), (2, 19)]\n", | |
"Differences 1 to 3! 3\n", | |
"[(3, 14), (2, 11), (4, 9)]\n", | |
"[(3, 25), (0, 20), (2, 20)]\n", | |
"Differences 1 to 3! 4\n", | |
"[(1, 12), (0, 10), (3, 10)]\n", | |
"[(4, 24), (2, 24), (1, 23)]\n", | |
"Differences 2 to 3! 4\n", | |
"[(1, 12), (0, 15), (3, 19)]\n", | |
"[(4, 24), (2, 24), (1, 23)]\n", | |
"Differences 1 to 3! 5\n", | |
"[(0, 19), (3, 10), (1, 9)]\n", | |
"[(0, 24), (1, 22), (2, 21)]\n", | |
"Differences 2 to 3! 5\n", | |
"[(0, 19), (3, 15), (1, 24)]\n", | |
"[(0, 24), (1, 22), (2, 21)]\n", | |
"Differences 1 to 3! 6\n", | |
"[(2, 12), (4, 11), (3, 10)]\n", | |
"[(3, 22), (0, 19), (4, 18)]\n", | |
"Differences 2 to 3! 6\n", | |
"[(2, 12), (4, 14), (3, 20)]\n", | |
"[(3, 22), (0, 19), (4, 18)]\n", | |
"Differences 1 to 2! 7\n", | |
"[(1, 13), (0, 10), (3, 9)]\n", | |
"[(1, 13), (4, 14), (3, 18)]\n", | |
"Differences 1 to 3! 7\n", | |
"[(1, 13), (0, 10), (3, 9)]\n", | |
"[(0, 22), (4, 20), (1, 19)]\n", | |
"Differences 2 to 3! 7\n", | |
"[(1, 13), (4, 14), (3, 18)]\n", | |
"[(0, 22), (4, 20), (1, 19)]\n", | |
"Differences 1 to 3! 8\n", | |
"[(4, 13), (2, 12), (0, 11)]\n", | |
"[(2, 25), (3, 20), (0, 20)]\n", | |
"Differences 2 to 3! 8\n", | |
"[(4, 13), (2, 15), (0, 20)]\n", | |
"[(2, 25), (3, 20), (0, 20)]\n", | |
"Differences 1 to 2! 9\n", | |
"[(4, 13), (1, 12), (0, 11)]\n", | |
"[(4, 13), (1, 18), (3, 17)]\n", | |
"Differences 1 to 3! 9\n", | |
"[(4, 13), (1, 12), (0, 11)]\n", | |
"[(0, 24), (2, 22), (3, 21)]\n", | |
"Differences 2 to 3! 9\n", | |
"[(4, 13), (1, 18), (3, 17)]\n", | |
"[(0, 24), (2, 22), (3, 21)]\n", | |
"Differences 1 to 3! 10\n", | |
"[(4, 14), (2, 12), (1, 12)]\n", | |
"[(0, 23), (1, 20), (4, 20)]\n", | |
"Differences 2 to 3! 10\n", | |
"[(4, 14), (2, 14), (1, 19)]\n", | |
"[(0, 23), (1, 20), (4, 20)]\n", | |
"Differences 1 to 2! 12\n", | |
"[(3, 12), (1, 11), (4, 10)]\n", | |
"[(3, 12), (1, 15), (2, 18)]\n", | |
"Differences 1 to 3! 12\n", | |
"[(3, 12), (1, 11), (4, 10)]\n", | |
"[(1, 27), (0, 24), (2, 21)]\n", | |
"Differences 2 to 3! 12\n", | |
"[(3, 12), (1, 15), (2, 18)]\n", | |
"[(1, 27), (0, 24), (2, 21)]\n", | |
"Differences 1 to 2! 13\n", | |
"[(2, 13), (4, 12), (0, 10)]\n", | |
"[(2, 13), (4, 15), (3, 19)]\n", | |
"Differences 1 to 3! 13\n", | |
"[(2, 13), (4, 12), (0, 10)]\n", | |
"[(3, 24), (4, 22), (2, 22)]\n", | |
"Differences 1 to 3! 15\n", | |
"[(4, 15), (2, 14), (1, 9)]\n", | |
"[(4, 25), (3, 24), (2, 24)]\n", | |
"Differences 2 to 3! 15\n", | |
"[(4, 15), (2, 20), (1, 20)]\n", | |
"[(4, 25), (3, 24), (2, 24)]\n", | |
"Differences 1 to 2! 17\n", | |
"[(1, 19), (3, 10), (4, 9)]\n", | |
"[(1, 19), (3, 19), (2, 21)]\n", | |
"Differences 1 to 3! 17\n", | |
"[(1, 19), (3, 10), (4, 9)]\n", | |
"[(2, 24), (1, 22), (3, 22)]\n", | |
"Differences 1 to 3! 20\n", | |
"[(4, 13), (2, 11), (3, 10)]\n", | |
"[(2, 22), (3, 20), (1, 17)]\n", | |
"Differences 2 to 3! 20\n", | |
"[(4, 13), (2, 16), (3, 18)]\n", | |
"[(2, 22), (3, 20), (1, 17)]\n", | |
"Differences 1 to 3! 21\n", | |
"[(3, 12), (0, 11), (4, 10)]\n", | |
"[(4, 21), (3, 20), (1, 20)]\n", | |
"Differences 2 to 3! 21\n", | |
"[(3, 12), (0, 16), (4, 19)]\n", | |
"[(4, 21), (3, 20), (1, 20)]\n", | |
"Differences 1 to 2! 22\n", | |
"[(1, 13), (2, 12), (3, 10)]\n", | |
"[(1, 13), (2, 15), (0, 18)]\n", | |
"Differences 1 to 3! 22\n", | |
"[(1, 13), (2, 12), (3, 10)]\n", | |
"[(3, 20), (0, 19), (4, 19)]\n", | |
"Differences 2 to 3! 22\n", | |
"[(1, 13), (2, 15), (0, 18)]\n", | |
"[(3, 20), (0, 19), (4, 19)]\n", | |
"Differences 1 to 3! 23\n", | |
"[(4, 14), (1, 13), (0, 12)]\n", | |
"[(1, 24), (4, 21), (2, 19)]\n", | |
"Differences 2 to 3! 23\n", | |
"[(4, 14), (1, 16), (0, 21)]\n", | |
"[(1, 24), (4, 21), (2, 19)]\n", | |
"Differences 1 to 2! 24\n", | |
"[(1, 16), (2, 11), (3, 8)]\n", | |
"[(1, 16), (0, 14), (3, 20)]\n", | |
"Differences 1 to 3! 24\n", | |
"[(1, 16), (2, 11), (3, 8)]\n", | |
"[(1, 28), (3, 21), (0, 19)]\n", | |
"Differences 1 to 3! 25\n", | |
"[(3, 14), (0, 13), (1, 11)]\n", | |
"[(1, 21), (3, 20), (2, 18)]\n", | |
"Differences 2 to 3! 25\n", | |
"[(3, 14), (0, 17), (1, 19)]\n", | |
"[(1, 21), (3, 20), (2, 18)]\n", | |
"Differences 1 to 3! 26\n", | |
"[(1, 14), (4, 13), (0, 9)]\n", | |
"[(0, 25), (4, 24), (3, 22)]\n", | |
"Differences 2 to 3! 26\n", | |
"[(1, 14), (4, 15), (0, 20)]\n", | |
"[(0, 25), (4, 24), (3, 22)]\n", | |
"Differences 1 to 2! 27\n", | |
"[(1, 13), (2, 12), (0, 11)]\n", | |
"[(1, 13), (2, 17), (4, 18)]\n", | |
"Differences 1 to 3! 27\n", | |
"[(1, 13), (2, 12), (0, 11)]\n", | |
"[(2, 21), (4, 20), (1, 20)]\n", | |
"Differences 1 to 3! 29\n", | |
"[(1, 13), (2, 13), (3, 9)]\n", | |
"[(1, 21), (3, 21), (4, 19)]\n", | |
"Differences 2 to 3! 29\n", | |
"[(2, 13), (1, 19), (3, 25)]\n", | |
"[(1, 21), (3, 21), (4, 19)]\n", | |
"Differences 1 to 2! 30\n", | |
"[(4, 13), (3, 11), (2, 9)]\n", | |
"[(4, 13), (3, 16), (1, 18)]\n", | |
"Differences 1 to 3! 30\n", | |
"[(4, 13), (3, 11), (2, 9)]\n", | |
"[(4, 24), (3, 19), (1, 19)]\n", | |
"Differences 1 to 3! 31\n", | |
"[(0, 13), (4, 12), (2, 11)]\n", | |
"[(1, 21), (4, 20), (2, 20)]\n", | |
"Differences 2 to 3! 31\n", | |
"[(0, 13), (4, 16), (2, 18)]\n", | |
"[(1, 21), (4, 20), (2, 20)]\n", | |
"Differences 1 to 3! 32\n", | |
"[(3, 13), (2, 11), (0, 10)]\n", | |
"[(3, 26), (1, 22), (2, 22)]\n", | |
"Differences 2 to 3! 32\n", | |
"[(3, 13), (2, 14), (0, 18)]\n", | |
"[(3, 26), (1, 22), (2, 22)]\n", | |
"Differences 1 to 2! 33\n", | |
"[(0, 12), (4, 11), (3, 10)]\n", | |
"[(0, 12), (3, 13), (2, 18)]\n", | |
"Differences 1 to 3! 33\n", | |
"[(0, 12), (4, 11), (3, 10)]\n", | |
"[(3, 27), (0, 23), (2, 21)]\n", | |
"Differences 1 to 3! 34\n", | |
"[(1, 12), (4, 11), (3, 10)]\n", | |
"[(4, 22), (1, 19), (2, 19)]\n", | |
"Differences 2 to 3! 34\n", | |
"[(1, 12), (4, 15), (3, 20)]\n", | |
"[(4, 22), (1, 19), (2, 19)]\n", | |
"Differences 1 to 2! 35\n", | |
"[(3, 15), (0, 11), (2, 9)]\n", | |
"[(3, 15), (1, 15), (4, 17)]\n", | |
"Differences 1 to 3! 35\n", | |
"[(3, 15), (0, 11), (2, 9)]\n", | |
"[(3, 21), (1, 21), (2, 20)]\n", | |
"Differences 2 to 3! 35\n", | |
"[(3, 15), (1, 15), (4, 17)]\n", | |
"[(3, 21), (1, 21), (2, 20)]\n", | |
"Differences 1 to 3! 36\n", | |
"[(0, 14), (1, 14), (3, 9)]\n", | |
"[(0, 22), (4, 21), (1, 21)]\n", | |
"Differences 2 to 3! 36\n", | |
"[(1, 14), (0, 18), (3, 19)]\n", | |
"[(0, 22), (4, 21), (1, 21)]\n", | |
"Differences 1 to 2! 38\n", | |
"[(4, 16), (1, 11), (2, 8)]\n", | |
"[(4, 16), (1, 17), (0, 18)]\n", | |
"Differences 1 to 3! 38\n", | |
"[(4, 16), (1, 11), (2, 8)]\n", | |
"[(1, 24), (4, 23), (3, 21)]\n", | |
"Differences 2 to 3! 38\n", | |
"[(4, 16), (1, 17), (0, 18)]\n", | |
"[(1, 24), (4, 23), (3, 21)]\n", | |
"Differences 1 to 2! 39\n", | |
"[(1, 15), (2, 11), (0, 9)]\n", | |
"[(1, 15), (0, 17), (3, 19)]\n", | |
"Differences 2 to 3! 39\n", | |
"[(1, 15), (0, 17), (3, 19)]\n", | |
"[(1, 23), (2, 20), (0, 20)]\n", | |
"Differences 1 to 2! 43\n", | |
"[(3, 12), (1, 10), (0, 10)]\n", | |
"[(3, 12), (4, 13), (2, 19)]\n", | |
"Differences 1 to 3! 43\n", | |
"[(3, 12), (1, 10), (0, 10)]\n", | |
"[(4, 24), (2, 23), (0, 22)]\n", | |
"Differences 2 to 3! 43\n", | |
"[(3, 12), (4, 13), (2, 19)]\n", | |
"[(4, 24), (2, 23), (0, 22)]\n", | |
"Differences 1 to 2! 44\n", | |
"[(3, 14), (0, 12), (4, 11)]\n", | |
"[(3, 14), (1, 15), (2, 18)]\n", | |
"Differences 1 to 3! 44\n", | |
"[(3, 14), (0, 12), (4, 11)]\n", | |
"[(1, 21), (3, 21), (2, 20)]\n", | |
"Differences 1 to 2! 45\n", | |
"[(3, 16), (0, 10), (2, 9)]\n", | |
"[(3, 16), (1, 14), (4, 18)]\n", | |
"Differences 2 to 3! 45\n", | |
"[(3, 16), (1, 14), (4, 18)]\n", | |
"[(3, 30), (0, 23), (2, 20)]\n", | |
"Differences 1 to 3! 46\n", | |
"[(4, 15), (1, 14), (0, 8)]\n", | |
"[(3, 26), (4, 26), (0, 25)]\n", | |
"Differences 2 to 3! 46\n", | |
"[(4, 15), (1, 19), (0, 21)]\n", | |
"[(3, 26), (4, 26), (0, 25)]\n", | |
"Differences 1 to 2! 50\n", | |
"[(1, 11), (3, 11), (4, 11)]\n", | |
"[(4, 11), (0, 15), (1, 19)]\n", | |
"Differences 1 to 3! 50\n", | |
"[(1, 11), (3, 11), (4, 11)]\n", | |
"[(1, 25), (3, 23), (0, 23)]\n", | |
"Differences 2 to 3! 50\n", | |
"[(4, 11), (0, 15), (1, 19)]\n", | |
"[(1, 25), (3, 23), (0, 23)]\n", | |
"Differences 1 to 2! 51\n", | |
"[(4, 16), (3, 10), (0, 10)]\n", | |
"[(4, 16), (0, 16), (1, 21)]\n", | |
"Differences 1 to 3! 51\n", | |
"[(4, 16), (3, 10), (0, 10)]\n", | |
"[(4, 21), (0, 19), (1, 18)]\n", | |
"Differences 1 to 3! 52\n", | |
"[(0, 13), (4, 11), (2, 11)]\n", | |
"[(0, 28), (1, 23), (2, 23)]\n", | |
"Differences 2 to 3! 52\n", | |
"[(0, 13), (2, 15), (4, 17)]\n", | |
"[(0, 28), (1, 23), (2, 23)]\n", | |
"Differences 1 to 2! 53\n", | |
"[(2, 14), (1, 11), (0, 10)]\n", | |
"[(2, 14), (1, 16), (3, 23)]\n", | |
"Differences 1 to 3! 53\n", | |
"[(2, 14), (1, 11), (0, 10)]\n", | |
"[(3, 23), (1, 20), (2, 19)]\n", | |
"Differences 1 to 2! 54\n", | |
"[(2, 13), (1, 10), (0, 10)]\n", | |
"[(2, 13), (0, 15), (3, 18)]\n", | |
"Differences 2 to 3! 54\n", | |
"[(2, 13), (0, 15), (3, 18)]\n", | |
"[(2, 23), (0, 19), (1, 18)]\n", | |
"Differences 1 to 2! 55\n", | |
"[(0, 18), (2, 12), (4, 8)]\n", | |
"[(0, 18), (2, 15), (3, 17)]\n", | |
"Differences 1 to 3! 55\n", | |
"[(0, 18), (2, 12), (4, 8)]\n", | |
"[(0, 23), (1, 20), (2, 17)]\n", | |
"Differences 2 to 3! 55\n", | |
"[(0, 18), (2, 15), (3, 17)]\n", | |
"[(0, 23), (1, 20), (2, 17)]\n", | |
"Differences 1 to 2! 57\n", | |
"[(0, 14), (3, 12), (2, 9)]\n", | |
"[(0, 14), (1, 15), (3, 19)]\n", | |
"Differences 1 to 3! 57\n", | |
"[(0, 14), (3, 12), (2, 9)]\n", | |
"[(0, 27), (1, 22), (4, 22)]\n", | |
"Differences 2 to 3! 57\n", | |
"[(0, 14), (1, 15), (3, 19)]\n", | |
"[(0, 27), (1, 22), (4, 22)]\n", | |
"Differences 1 to 2! 58\n", | |
"[(3, 12), (0, 11), (1, 10)]\n", | |
"[(3, 12), (1, 16), (4, 18)]\n", | |
"Differences 2 to 3! 58\n", | |
"[(3, 12), (1, 16), (4, 18)]\n", | |
"[(3, 22), (1, 21), (0, 20)]\n", | |
"Differences 1 to 3! 59\n", | |
"[(2, 14), (3, 10), (0, 9)]\n", | |
"[(2, 26), (4, 23), (3, 22)]\n", | |
"Differences 2 to 3! 59\n", | |
"[(2, 14), (0, 14), (3, 20)]\n", | |
"[(2, 26), (4, 23), (3, 22)]\n", | |
"Differences 1 to 2! 61\n", | |
"[(1, 11), (2, 11), (0, 10)]\n", | |
"[(2, 11), (4, 15), (0, 21)]\n", | |
"Differences 1 to 3! 61\n", | |
"[(1, 11), (2, 11), (0, 10)]\n", | |
"[(0, 23), (3, 21), (4, 19)]\n", | |
"Differences 2 to 3! 61\n", | |
"[(2, 11), (4, 15), (0, 21)]\n", | |
"[(0, 23), (3, 21), (4, 19)]\n", | |
"Differences 1 to 2! 62\n", | |
"[(4, 12), (2, 11), (1, 9)]\n", | |
"[(4, 12), (0, 14), (3, 17)]\n", | |
"Differences 1 to 3! 62\n", | |
"[(4, 12), (2, 11), (1, 9)]\n", | |
"[(3, 22), (0, 20), (4, 19)]\n", | |
"Differences 1 to 3! 64\n", | |
"[(4, 17), (3, 11), (0, 8)]\n", | |
"[(3, 23), (0, 21), (2, 21)]\n", | |
"Differences 2 to 3! 64\n", | |
"[(4, 17), (3, 17), (0, 18)]\n", | |
"[(3, 23), (0, 21), (2, 21)]\n", | |
"Differences 1 to 3! 65\n", | |
"[(0, 13), (3, 11), (4, 9)]\n", | |
"[(2, 23), (0, 21), (3, 20)]\n", | |
"Differences 2 to 3! 65\n", | |
"[(0, 13), (3, 14), (4, 19)]\n", | |
"[(2, 23), (0, 21), (3, 20)]\n", | |
"Differences 1 to 2! 66\n", | |
"[(3, 12), (2, 10), (4, 10)]\n", | |
"[(3, 12), (1, 16), (0, 18)]\n", | |
"Differences 2 to 3! 66\n", | |
"[(3, 12), (1, 16), (0, 18)]\n", | |
"[(4, 19), (3, 18), (2, 17)]\n", | |
"Differences 1 to 3! 67\n", | |
"[(1, 12), (3, 11), (4, 10)]\n", | |
"[(2, 23), (4, 20), (3, 20)]\n", | |
"Differences 2 to 3! 67\n", | |
"[(1, 12), (3, 16), (4, 20)]\n", | |
"[(2, 23), (4, 20), (3, 20)]\n", | |
"Differences 1 to 2! 68\n", | |
"[(3, 15), (2, 11), (4, 9)]\n", | |
"[(3, 15), (0, 16), (4, 20)]\n", | |
"Differences 1 to 3! 68\n", | |
"[(3, 15), (2, 11), (4, 9)]\n", | |
"[(4, 20), (0, 18), (3, 17)]\n", | |
"Differences 1 to 2! 69\n", | |
"[(4, 16), (0, 12), (2, 8)]\n", | |
"[(4, 16), (0, 19), (1, 19)]\n", | |
"Differences 1 to 3! 69\n", | |
"[(4, 16), (0, 12), (2, 8)]\n", | |
"[(4, 26), (1, 23), (0, 20)]\n", | |
"Differences 1 to 2! 70\n", | |
"[(0, 17), (4, 12), (2, 8)]\n", | |
"[(0, 17), (4, 17), (1, 21)]\n", | |
"Differences 1 to 3! 70\n", | |
"[(0, 17), (4, 12), (2, 8)]\n", | |
"[(0, 28), (4, 22), (1, 21)]\n", | |
"Differences 1 to 3! 71\n", | |
"[(2, 12), (3, 11), (4, 9)]\n", | |
"[(3, 25), (2, 21), (1, 19)]\n", | |
"Differences 2 to 3! 71\n", | |
"[(2, 12), (4, 14), (3, 20)]\n", | |
"[(3, 25), (2, 21), (1, 19)]\n", | |
"Differences 1 to 2! 72\n", | |
"[(1, 16), (3, 10), (4, 9)]\n", | |
"[(1, 16), (4, 15), (2, 20)]\n", | |
"Differences 2 to 3! 72\n", | |
"[(1, 16), (4, 15), (2, 20)]\n", | |
"[(1, 26), (3, 23), (4, 21)]\n", | |
"Differences 1 to 3! 73\n", | |
"[(4, 14), (2, 13), (1, 11)]\n", | |
"[(3, 19), (4, 19), (2, 19)]\n", | |
"Differences 2 to 3! 73\n", | |
"[(4, 14), (2, 16), (1, 17)]\n", | |
"[(3, 19), (4, 19), (2, 19)]\n", | |
"Differences 1 to 2! 75\n", | |
"[(1, 14), (3, 12), (2, 10)]\n", | |
"[(1, 14), (2, 15), (4, 19)]\n", | |
"Differences 1 to 3! 75\n", | |
"[(1, 14), (3, 12), (2, 10)]\n", | |
"[(4, 21), (1, 21), (2, 20)]\n", | |
"Differences 1 to 3! 76\n", | |
"[(1, 12), (3, 11), (4, 11)]\n", | |
"[(3, 27), (2, 25), (0, 23)]\n", | |
"Differences 2 to 3! 76\n", | |
"[(1, 12), (4, 14), (3, 18)]\n", | |
"[(3, 27), (2, 25), (0, 23)]\n", | |
"Differences 1 to 2! 77\n", | |
"[(0, 12), (1, 11), (3, 10)]\n", | |
"[(0, 12), (4, 13), (1, 20)]\n", | |
"Differences 1 to 3! 77\n", | |
"[(0, 12), (1, 11), (3, 10)]\n", | |
"[(4, 21), (3, 20), (2, 19)]\n", | |
"Differences 2 to 3! 77\n", | |
"[(0, 12), (4, 13), (1, 20)]\n", | |
"[(4, 21), (3, 20), (2, 19)]\n", | |
"Differences 1 to 2! 78\n", | |
"[(4, 15), (2, 12), (3, 8)]\n", | |
"[(4, 15), (1, 15), (2, 19)]\n", | |
"Differences 1 to 3! 78\n", | |
"[(4, 15), (2, 12), (3, 8)]\n", | |
"[(2, 26), (1, 24), (4, 22)]\n", | |
"Differences 1 to 2! 79\n", | |
"[(2, 13), (1, 12), (4, 10)]\n", | |
"[(2, 13), (1, 14), (3, 18)]\n", | |
"Differences 1 to 3! 79\n", | |
"[(2, 13), (1, 12), (4, 10)]\n", | |
"[(4, 22), (2, 20), (3, 20)]\n", | |
"Differences 2 to 3! 79\n", | |
"[(2, 13), (1, 14), (3, 18)]\n", | |
"[(4, 22), (2, 20), (3, 20)]\n", | |
"Differences 1 to 2! 82\n", | |
"[(4, 15), (3, 10), (1, 10)]\n", | |
"[(4, 15), (0, 14), (3, 20)]\n", | |
"Differences 1 to 3! 82\n", | |
"[(4, 15), (3, 10), (1, 10)]\n", | |
"[(0, 21), (4, 21), (1, 16)]\n", | |
"Differences 2 to 3! 82\n", | |
"[(4, 15), (0, 14), (3, 20)]\n", | |
"[(0, 21), (4, 21), (1, 16)]\n", | |
"Differences 1 to 2! 83\n", | |
"[(2, 13), (1, 12), (0, 10)]\n", | |
"[(2, 13), (3, 14), (1, 21)]\n", | |
"Differences 1 to 3! 83\n", | |
"[(2, 13), (1, 12), (0, 10)]\n", | |
"[(1, 25), (3, 24), (2, 22)]\n", | |
"Differences 1 to 2! 85\n", | |
"[(0, 14), (1, 12), (2, 10)]\n", | |
"[(0, 14), (1, 14), (4, 18)]\n", | |
"Differences 1 to 3! 85\n", | |
"[(0, 14), (1, 12), (2, 10)]\n", | |
"[(4, 24), (2, 24), (3, 23)]\n", | |
"Differences 2 to 3! 85\n", | |
"[(0, 14), (1, 14), (4, 18)]\n", | |
"[(4, 24), (2, 24), (3, 23)]\n", | |
"Differences 1 to 2! 86\n", | |
"[(4, 15), (2, 13), (1, 11)]\n", | |
"[(4, 15), (2, 16), (3, 20)]\n", | |
"Differences 1 to 3! 86\n", | |
"[(4, 15), (2, 13), (1, 11)]\n", | |
"[(4, 29), (3, 23), (2, 20)]\n", | |
"Differences 1 to 2! 88\n", | |
"[(0, 13), (4, 11), (2, 10)]\n", | |
"[(0, 13), (2, 15), (3, 17)]\n", | |
"Differences 1 to 3! 88\n", | |
"[(0, 13), (4, 11), (2, 10)]\n", | |
"[(2, 28), (3, 25), (0, 22)]\n", | |
"Differences 1 to 3! 90\n", | |
"[(4, 14), (3, 12), (2, 11)]\n", | |
"[(4, 24), (1, 24), (3, 20)]\n", | |
"Differences 2 to 3! 90\n", | |
"[(4, 14), (2, 15), (3, 18)]\n", | |
"[(4, 24), (1, 24), (3, 20)]\n", | |
"Differences 1 to 3! 91\n", | |
"[(1, 20), (2, 11), (4, 8)]\n", | |
"[(0, 25), (1, 25), (2, 22)]\n", | |
"Differences 2 to 3! 91\n", | |
"[(1, 20), (2, 15), (4, 24)]\n", | |
"[(0, 25), (1, 25), (2, 22)]\n", | |
"Differences 1 to 2! 92\n", | |
"[(1, 13), (2, 12), (3, 10)]\n", | |
"[(1, 13), (2, 14), (4, 19)]\n", | |
"Differences 1 to 3! 92\n", | |
"[(1, 13), (2, 12), (3, 10)]\n", | |
"[(1, 23), (4, 22), (0, 21)]\n", | |
"Differences 2 to 3! 92\n", | |
"[(1, 13), (2, 14), (4, 19)]\n", | |
"[(1, 23), (4, 22), (0, 21)]\n", | |
"Differences 1 to 3! 93\n", | |
"[(2, 15), (0, 12), (3, 9)]\n", | |
"[(2, 22), (1, 19), (0, 17)]\n", | |
"Differences 2 to 3! 93\n", | |
"[(2, 15), (0, 15), (3, 18)]\n", | |
"[(2, 22), (1, 19), (0, 17)]\n", | |
"Differences 1 to 2! 94\n", | |
"[(1, 14), (2, 13), (3, 10)]\n", | |
"[(1, 14), (2, 16), (4, 19)]\n", | |
"Differences 1 to 3! 94\n", | |
"[(1, 14), (2, 13), (3, 10)]\n", | |
"[(4, 27), (1, 27), (3, 23)]\n", | |
"Differences 2 to 3! 94\n", | |
"[(1, 14), (2, 16), (4, 19)]\n", | |
"[(4, 27), (1, 27), (3, 23)]\n", | |
"Differences 1 to 3! 95\n", | |
"[(3, 14), (0, 11), (4, 10)]\n", | |
"[(4, 23), (0, 19), (1, 17)]\n", | |
"Differences 2 to 3! 95\n", | |
"[(3, 14), (0, 18), (4, 20)]\n", | |
"[(4, 23), (0, 19), (1, 17)]\n", | |
"Differences 1 to 3! 96\n", | |
"[(2, 14), (3, 12), (1, 12)]\n", | |
"[(1, 33), (3, 24), (4, 20)]\n", | |
"Differences 2 to 3! 96\n", | |
"[(2, 14), (1, 19), (3, 22)]\n", | |
"[(1, 33), (3, 24), (4, 20)]\n", | |
"Differences 1 to 2! 99\n", | |
"[(0, 13), (1, 10), (2, 10)]\n", | |
"[(0, 13), (4, 14), (1, 19)]\n", | |
"Differences 2 to 3! 99\n", | |
"[(0, 13), (4, 14), (1, 19)]\n", | |
"[(1, 23), (2, 22), (0, 21)]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"(0.44, 0.69, 0.57)" | |
] | |
}, | |
"execution_count": 76, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ntest = 100\n", | |
"diffs12 = diffs23 = diffs13 = 0\n", | |
"\n", | |
"vc = VoteComparison(3, 50, 5)\n", | |
"\n", | |
"v1 = []\n", | |
"v2 = []\n", | |
"v3 = []\n", | |
"for i in range(ntest):\n", | |
" votes = vc.random_votes()\n", | |
" nstocount = np.random.randint(vc.Ncand, size=vc.Nvoters)\n", | |
" v1.append(vc.first_vote_winners(votes))\n", | |
" v2.append(vc.ranked_vote_winners(votes))\n", | |
" v3.append(vc.n_vote_winners(votes, nstocount))\n", | |
" winners1 = [v[0] for v in v1[-1]]\n", | |
" winners2 = [v[0] for v in v2[-1]]\n", | |
" winners3 = [v[0] for v in v3[-1]]\n", | |
" if set(winners1) != set(winners2):\n", | |
" print('Differences 1 to 2!', i)\n", | |
" diffs12 +=1\n", | |
" print(v1[-1])\n", | |
" print(v2[-1])\n", | |
" if set(winners1) != set(winners3):\n", | |
" print('Differences 1 to 3!', i)\n", | |
" diffs13 +=1\n", | |
" print(v1[-1])\n", | |
" print(v3[-1])\n", | |
" if set(winners2) != set(winners3):\n", | |
" print('Differences 2 to 3!', i)\n", | |
" diffs23 +=1\n", | |
" print(v2[-1])\n", | |
" print(v3[-1])\n", | |
" \n", | |
"diffs12/ntest, diffs13/ntest, diffs23/ntest" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Differences 1 to 2! 0\n", | |
"[(3, 13), (2, 13), (0, 12)]\n", | |
"[(3, 13), (0, 17), (1, 23)]\n", | |
"Differences 1 to 3! 0\n", | |
"[(3, 13), (2, 13), (0, 12)]\n", | |
"[(0, 21), (1, 21), (2, 19)]\n", | |
"Differences 2 to 3! 0\n", | |
"[(3, 13), (0, 17), (1, 23)]\n", | |
"[(0, 21), (1, 21), (2, 19)]\n", | |
"Differences 1 to 3! 2\n", | |
"[(1, 16), (2, 12), (0, 10)]\n", | |
"[(1, 26), (0, 25), (3, 14)]\n", | |
"Differences 2 to 3! 2\n", | |
"[(1, 16), (0, 20), (2, 23)]\n", | |
"[(1, 26), (0, 25), (3, 14)]\n", | |
"Differences 1 to 3! 3\n", | |
"[(0, 19), (1, 12), (2, 9)]\n", | |
"[(0, 22), (1, 21), (3, 12)]\n", | |
"Differences 2 to 3! 3\n", | |
"[(0, 19), (1, 20), (2, 22)]\n", | |
"[(0, 22), (1, 21), (3, 12)]\n", | |
"Differences 1 to 2! 6\n", | |
"[(1, 15), (2, 11), (0, 11)]\n", | |
"[(1, 15), (0, 18), (3, 22)]\n", | |
"Differences 2 to 3! 6\n", | |
"[(1, 15), (0, 18), (3, 22)]\n", | |
"[(1, 22), (2, 21), (0, 20)]\n", | |
"Differences 1 to 2! 7\n", | |
"[(0, 21), (1, 15), (3, 6)]\n", | |
"[(0, 21), (1, 20), (2, 27)]\n", | |
"Differences 1 to 3! 7\n", | |
"[(0, 21), (1, 15), (3, 6)]\n", | |
"[(0, 26), (1, 18), (2, 14)]\n", | |
"Differences 1 to 2! 13\n", | |
"[(0, 16), (2, 12), (3, 8)]\n", | |
"[(0, 16), (3, 13), (1, 20)]\n", | |
"Differences 1 to 3! 13\n", | |
"[(0, 16), (2, 12), (3, 8)]\n", | |
"[(0, 22), (2, 14), (1, 13)]\n", | |
"Differences 2 to 3! 13\n", | |
"[(0, 16), (3, 13), (1, 20)]\n", | |
"[(0, 22), (2, 14), (1, 13)]\n", | |
"Differences 1 to 2! 14\n", | |
"[(0, 14), (1, 13), (2, 11)]\n", | |
"[(0, 14), (1, 19), (3, 20)]\n", | |
"Differences 2 to 3! 14\n", | |
"[(0, 14), (1, 19), (3, 20)]\n", | |
"[(1, 28), (2, 19), (0, 18)]\n", | |
"Differences 1 to 2! 15\n", | |
"[(0, 19), (1, 11), (3, 9)]\n", | |
"[(0, 19), (2, 16), (1, 25)]\n", | |
"Differences 1 to 3! 15\n", | |
"[(0, 19), (1, 11), (3, 9)]\n", | |
"[(0, 27), (2, 19), (1, 16)]\n", | |
"Differences 1 to 2! 16\n", | |
"[(0, 16), (1, 13), (3, 11)]\n", | |
"[(0, 16), (1, 19), (2, 24)]\n", | |
"Differences 1 to 3! 16\n", | |
"[(0, 16), (1, 13), (3, 11)]\n", | |
"[(0, 30), (1, 28), (2, 21)]\n", | |
"Differences 1 to 3! 21\n", | |
"[(0, 19), (1, 12), (2, 10)]\n", | |
"[(1, 26), (0, 23), (3, 17)]\n", | |
"Differences 2 to 3! 21\n", | |
"[(0, 19), (1, 21), (2, 22)]\n", | |
"[(1, 26), (0, 23), (3, 17)]\n", | |
"Differences 1 to 2! 25\n", | |
"[(0, 17), (1, 15), (3, 10)]\n", | |
"[(0, 17), (1, 21), (2, 21)]\n", | |
"Differences 2 to 3! 25\n", | |
"[(0, 17), (1, 21), (2, 21)]\n", | |
"[(0, 25), (1, 22), (3, 20)]\n", | |
"Differences 1 to 2! 30\n", | |
"[(0, 18), (1, 17), (3, 6)]\n", | |
"[(0, 18), (1, 24), (2, 24)]\n", | |
"Differences 2 to 3! 30\n", | |
"[(0, 18), (1, 24), (2, 24)]\n", | |
"[(0, 28), (1, 26), (3, 16)]\n", | |
"Differences 1 to 2! 31\n", | |
"[(0, 18), (1, 16), (3, 8)]\n", | |
"[(0, 18), (1, 24), (2, 25)]\n", | |
"Differences 1 to 3! 31\n", | |
"[(0, 18), (1, 16), (3, 8)]\n", | |
"[(1, 34), (0, 24), (2, 19)]\n", | |
"Differences 1 to 2! 35\n", | |
"[(0, 21), (3, 11), (2, 8)]\n", | |
"[(0, 21), (1, 17), (2, 26)]\n", | |
"Differences 1 to 3! 35\n", | |
"[(0, 21), (3, 11), (2, 8)]\n", | |
"[(0, 33), (1, 24), (2, 21)]\n", | |
"Differences 1 to 2! 36\n", | |
"[(0, 17), (1, 12), (3, 7)]\n", | |
"[(0, 17), (1, 21), (2, 23)]\n", | |
"Differences 1 to 3! 36\n", | |
"[(0, 17), (1, 12), (3, 7)]\n", | |
"[(0, 25), (1, 19), (2, 19)]\n", | |
"Differences 1 to 2! 39\n", | |
"[(0, 15), (3, 12), (1, 11)]\n", | |
"[(0, 15), (1, 17), (2, 23)]\n", | |
"Differences 1 to 3! 39\n", | |
"[(0, 15), (3, 12), (1, 11)]\n", | |
"[(0, 27), (1, 19), (2, 18)]\n", | |
"Differences 1 to 2! 42\n", | |
"[(0, 21), (1, 15), (3, 5)]\n", | |
"[(0, 21), (1, 26), (2, 20)]\n", | |
"Differences 1 to 3! 42\n", | |
"[(0, 21), (1, 15), (3, 5)]\n", | |
"[(0, 27), (1, 23), (2, 14)]\n", | |
"Differences 1 to 2! 43\n", | |
"[(0, 18), (1, 15), (2, 7)]\n", | |
"[(0, 18), (1, 23), (3, 24)]\n", | |
"Differences 1 to 3! 43\n", | |
"[(0, 18), (1, 15), (2, 7)]\n", | |
"[(0, 21), (3, 13), (2, 11)]\n", | |
"Differences 2 to 3! 43\n", | |
"[(0, 18), (1, 23), (3, 24)]\n", | |
"[(0, 21), (3, 13), (2, 11)]\n", | |
"Differences 1 to 2! 51\n", | |
"[(0, 20), (2, 13), (4, 7)]\n", | |
"[(0, 20), (2, 21), (1, 29)]\n", | |
"Differences 1 to 3! 51\n", | |
"[(0, 20), (2, 13), (4, 7)]\n", | |
"[(0, 21), (1, 21), (2, 17)]\n", | |
"Differences 1 to 2! 52\n", | |
"[(0, 19), (1, 13), (2, 10)]\n", | |
"[(0, 19), (1, 20), (3, 22)]\n", | |
"Differences 2 to 3! 52\n", | |
"[(0, 19), (1, 20), (3, 22)]\n", | |
"[(0, 21), (1, 20), (2, 15)]\n", | |
"Differences 1 to 2! 55\n", | |
"[(2, 15), (1, 11), (3, 10)]\n", | |
"[(2, 15), (1, 17), (0, 28)]\n", | |
"Differences 1 to 3! 55\n", | |
"[(2, 15), (1, 11), (3, 10)]\n", | |
"[(0, 22), (2, 18), (1, 18)]\n", | |
"Differences 1 to 2! 57\n", | |
"[(0, 20), (1, 9), (3, 8)]\n", | |
"[(0, 20), (1, 16), (2, 23)]\n", | |
"Differences 1 to 3! 57\n", | |
"[(0, 20), (1, 9), (3, 8)]\n", | |
"[(0, 27), (1, 24), (2, 20)]\n", | |
"Differences 1 to 2! 59\n", | |
"[(2, 16), (0, 13), (4, 9)]\n", | |
"[(2, 16), (0, 22), (1, 23)]\n", | |
"Differences 1 to 3! 59\n", | |
"[(2, 16), (0, 13), (4, 9)]\n", | |
"[(0, 26), (2, 18), (1, 17)]\n", | |
"Differences 1 to 2! 68\n", | |
"[(0, 21), (3, 10), (2, 9)]\n", | |
"[(0, 21), (2, 18), (1, 23)]\n", | |
"Differences 1 to 3! 68\n", | |
"[(0, 21), (3, 10), (2, 9)]\n", | |
"[(0, 32), (2, 21), (1, 19)]\n", | |
"Differences 1 to 2! 69\n", | |
"[(1, 17), (0, 16), (3, 7)]\n", | |
"[(1, 17), (0, 27), (2, 22)]\n", | |
"Differences 1 to 3! 69\n", | |
"[(1, 17), (0, 16), (3, 7)]\n", | |
"[(0, 27), (1, 23), (2, 20)]\n", | |
"Differences 1 to 2! 70\n", | |
"[(0, 19), (1, 15), (3, 10)]\n", | |
"[(0, 19), (1, 25), (2, 21)]\n", | |
"Differences 2 to 3! 70\n", | |
"[(0, 19), (1, 25), (2, 21)]\n", | |
"[(1, 29), (0, 23), (3, 17)]\n", | |
"Differences 1 to 2! 71\n", | |
"[(0, 12), (2, 11), (3, 10)]\n", | |
"[(0, 12), (1, 16), (2, 22)]\n", | |
"Differences 1 to 3! 71\n", | |
"[(0, 12), (2, 11), (3, 10)]\n", | |
"[(0, 26), (1, 24), (2, 20)]\n", | |
"Differences 1 to 3! 72\n", | |
"[(0, 19), (1, 15), (3, 8)]\n", | |
"[(1, 30), (0, 22), (2, 15)]\n", | |
"Differences 2 to 3! 72\n", | |
"[(0, 19), (1, 24), (3, 22)]\n", | |
"[(1, 30), (0, 22), (2, 15)]\n", | |
"Differences 1 to 2! 77\n", | |
"[(1, 17), (0, 13), (3, 9)]\n", | |
"[(1, 17), (0, 22), (2, 24)]\n", | |
"Differences 1 to 3! 77\n", | |
"[(1, 17), (0, 13), (3, 9)]\n", | |
"[(1, 27), (0, 25), (2, 20)]\n", | |
"Differences 1 to 2! 82\n", | |
"[(2, 15), (0, 14), (3, 11)]\n", | |
"[(2, 15), (0, 19), (1, 28)]\n", | |
"Differences 1 to 3! 82\n", | |
"[(2, 15), (0, 14), (3, 11)]\n", | |
"[(0, 26), (1, 21), (2, 20)]\n", | |
"Differences 1 to 3! 84\n", | |
"[(0, 17), (1, 17), (2, 8)]\n", | |
"[(0, 26), (1, 25), (3, 13)]\n", | |
"Differences 2 to 3! 84\n", | |
"[(1, 17), (0, 23), (2, 22)]\n", | |
"[(0, 26), (1, 25), (3, 13)]\n", | |
"Differences 1 to 2! 91\n", | |
"[(0, 22), (1, 13), (3, 7)]\n", | |
"[(0, 22), (1, 18), (2, 26)]\n", | |
"Differences 1 to 3! 91\n", | |
"[(0, 22), (1, 13), (3, 7)]\n", | |
"[(0, 23), (2, 21), (1, 17)]\n", | |
"Differences 1 to 3! 93\n", | |
"[(0, 20), (1, 12), (3, 8)]\n", | |
"[(0, 26), (1, 18), (2, 15)]\n", | |
"Differences 2 to 3! 93\n", | |
"[(0, 20), (1, 17), (3, 22)]\n", | |
"[(0, 26), (1, 18), (2, 15)]\n", | |
"Differences 1 to 2! 95\n", | |
"[(0, 16), (1, 15), (3, 11)]\n", | |
"[(0, 16), (1, 21), (2, 23)]\n", | |
"Differences 1 to 3! 95\n", | |
"[(0, 16), (1, 15), (3, 11)]\n", | |
"[(1, 24), (0, 21), (2, 19)]\n", | |
"Differences 1 to 2! 96\n", | |
"[(0, 23), (1, 9), (3, 9)]\n", | |
"[(0, 23), (1, 20), (2, 22)]\n", | |
"Differences 1 to 3! 96\n", | |
"[(0, 23), (1, 9), (3, 9)]\n", | |
"[(0, 26), (1, 23), (2, 19)]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"(0.29, 0.29, 0.15)" | |
] | |
}, | |
"execution_count": 77, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def weighted_votes(Ncand, Nvoters):\n", | |
" votes = []\n", | |
" for i in range(Nvoters):\n", | |
" w = [5, 4,3,2,1]\n", | |
" votes.append(np.random.choice(5, 3, replace=False, p=w/np.sum(w)))\n", | |
" return np.array(votes)\n", | |
"\n", | |
"ntest = 100\n", | |
"diffs12 = diffs23 = diffs13 = 0\n", | |
"\n", | |
"vc = VoteComparison(3, 50, 5)\n", | |
"\n", | |
"v1 = []\n", | |
"v2 = []\n", | |
"v3 = []\n", | |
"for i in range(ntest):\n", | |
" votes = weighted_votes(vc.Ncand, vc.Nvoters)\n", | |
" nstocount = np.random.randint(vc.Ncand, size=vc.Nvoters)\n", | |
" v1.append(vc.first_vote_winners(votes))\n", | |
" v2.append(vc.ranked_vote_winners(votes))\n", | |
" v3.append(vc.n_vote_winners(votes, nstocount))\n", | |
" winners1 = [v[0] for v in v1[-1]]\n", | |
" winners2 = [v[0] for v in v2[-1]]\n", | |
" winners3 = [v[0] for v in v3[-1]]\n", | |
" if set(winners1) != set(winners2):\n", | |
" print('Differences 1 to 2!', i)\n", | |
" diffs12 +=1\n", | |
" print(v1[-1])\n", | |
" print(v2[-1])\n", | |
" if set(winners1) != set(winners3):\n", | |
" print('Differences 1 to 3!', i)\n", | |
" diffs13 +=1\n", | |
" print(v1[-1])\n", | |
" print(v3[-1])\n", | |
" if set(winners2) != set(winners3):\n", | |
" print('Differences 2 to 3!', i)\n", | |
" diffs23 +=1\n", | |
" print(v2[-1])\n", | |
" print(v3[-1])\n", | |
" \n", | |
"diffs12/ntest, diffs13/ntest, diffs23/ntest" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Differences 1 to 2! 0\n", | |
"[(1, 20), (0, 10), (3, 9)]\n", | |
"[(1, 20), (0, 18), (2, 25)]\n", | |
"Differences 1 to 3! 0\n", | |
"[(1, 20), (0, 10), (3, 9)]\n", | |
"[(0, 44), (1, 37), (2, 34)]\n", | |
"Differences 1 to 2! 8\n", | |
"[(0, 16), (2, 13), (3, 11)]\n", | |
"[(0, 16), (2, 18), (1, 23)]\n", | |
"Differences 1 to 3! 8\n", | |
"[(0, 16), (2, 13), (3, 11)]\n", | |
"[(0, 43), (1, 39), (2, 32)]\n", | |
"Differences 1 to 2! 10\n", | |
"[(0, 27), (2, 8), (3, 7)]\n", | |
"[(0, 27), (1, 18), (2, 22)]\n", | |
"Differences 1 to 3! 10\n", | |
"[(0, 27), (2, 8), (3, 7)]\n", | |
"[(0, 47), (1, 33), (2, 31)]\n", | |
"Differences 1 to 2! 11\n", | |
"[(0, 14), (1, 12), (3, 12)]\n", | |
"[(0, 14), (1, 19), (2, 24)]\n", | |
"Differences 1 to 3! 11\n", | |
"[(0, 14), (1, 12), (3, 12)]\n", | |
"[(0, 39), (1, 39), (2, 32)]\n", | |
"Differences 1 to 3! 12\n", | |
"[(0, 21), (1, 12), (3, 8)]\n", | |
"[(0, 39), (1, 37), (2, 30)]\n", | |
"Differences 2 to 3! 12\n", | |
"[(0, 21), (3, 16), (1, 22)]\n", | |
"[(0, 39), (1, 37), (2, 30)]\n", | |
"Differences 1 to 2! 19\n", | |
"[(0, 19), (1, 12), (3, 10)]\n", | |
"[(0, 19), (1, 19), (2, 26)]\n", | |
"Differences 1 to 3! 19\n", | |
"[(0, 19), (1, 12), (3, 10)]\n", | |
"[(0, 43), (1, 37), (2, 32)]\n", | |
"Differences 1 to 2! 24\n", | |
"[(1, 19), (0, 18), (3, 7)]\n", | |
"[(1, 19), (0, 26), (2, 23)]\n", | |
"Differences 1 to 3! 24\n", | |
"[(1, 19), (0, 18), (3, 7)]\n", | |
"[(0, 43), (1, 38), (2, 30)]\n", | |
"Differences 1 to 2! 25\n", | |
"[(0, 13), (2, 11), (3, 11)]\n", | |
"[(0, 13), (1, 16), (2, 23)]\n", | |
"Differences 1 to 3! 25\n", | |
"[(0, 13), (2, 11), (3, 11)]\n", | |
"[(0, 40), (1, 35), (2, 34)]\n", | |
"Differences 1 to 2! 29\n", | |
"[(0, 17), (2, 14), (4, 9)]\n", | |
"[(0, 17), (2, 20), (1, 24)]\n", | |
"Differences 1 to 3! 29\n", | |
"[(0, 17), (2, 14), (4, 9)]\n", | |
"[(1, 45), (0, 37), (2, 33)]\n", | |
"Differences 1 to 3! 32\n", | |
"[(1, 19), (0, 11), (3, 8)]\n", | |
"[(0, 39), (1, 37), (2, 30)]\n", | |
"Differences 2 to 3! 32\n", | |
"[(1, 19), (0, 17), (3, 21)]\n", | |
"[(0, 39), (1, 37), (2, 30)]\n", | |
"Differences 1 to 3! 34\n", | |
"[(0, 18), (1, 12), (4, 8)]\n", | |
"[(0, 42), (1, 38), (2, 26)]\n", | |
"Differences 2 to 3! 34\n", | |
"[(0, 18), (1, 17), (4, 17)]\n", | |
"[(0, 42), (1, 38), (2, 26)]\n", | |
"Differences 1 to 2! 35\n", | |
"[(0, 16), (1, 14), (3, 9)]\n", | |
"[(0, 16), (1, 20), (2, 22)]\n", | |
"Differences 1 to 3! 35\n", | |
"[(0, 16), (1, 14), (3, 9)]\n", | |
"[(0, 42), (1, 37), (2, 29)]\n", | |
"Differences 1 to 2! 38\n", | |
"[(3, 17), (0, 12), (2, 12)]\n", | |
"[(3, 17), (0, 18), (1, 24)]\n", | |
"Differences 1 to 3! 38\n", | |
"[(3, 17), (0, 12), (2, 12)]\n", | |
"[(2, 40), (0, 39), (1, 34)]\n", | |
"Differences 2 to 3! 38\n", | |
"[(3, 17), (0, 18), (1, 24)]\n", | |
"[(2, 40), (0, 39), (1, 34)]\n", | |
"Differences 1 to 2! 41\n", | |
"[(0, 18), (1, 12), (2, 7)]\n", | |
"[(0, 18), (1, 21), (3, 20)]\n", | |
"Differences 2 to 3! 41\n", | |
"[(0, 18), (1, 21), (3, 20)]\n", | |
"[(0, 43), (1, 42), (2, 27)]\n", | |
"Differences 1 to 2! 45\n", | |
"[(0, 13), (1, 11), (3, 9)]\n", | |
"[(0, 13), (1, 16), (2, 19)]\n", | |
"Differences 1 to 3! 45\n", | |
"[(0, 13), (1, 11), (3, 9)]\n", | |
"[(0, 39), (2, 31), (1, 30)]\n", | |
"Differences 1 to 3! 47\n", | |
"[(1, 15), (0, 12), (3, 11)]\n", | |
"[(0, 40), (1, 36), (2, 32)]\n", | |
"Differences 2 to 3! 47\n", | |
"[(1, 15), (0, 16), (3, 21)]\n", | |
"[(0, 40), (1, 36), (2, 32)]\n", | |
"Differences 1 to 2! 55\n", | |
"[(0, 17), (1, 14), (3, 8)]\n", | |
"[(0, 17), (1, 23), (2, 26)]\n", | |
"Differences 1 to 3! 55\n", | |
"[(0, 17), (1, 14), (3, 8)]\n", | |
"[(0, 38), (1, 36), (2, 35)]\n", | |
"Differences 1 to 3! 59\n", | |
"[(3, 13), (0, 13), (1, 12)]\n", | |
"[(0, 43), (1, 37), (2, 31)]\n", | |
"Differences 2 to 3! 59\n", | |
"[(3, 13), (0, 18), (1, 26)]\n", | |
"[(0, 43), (1, 37), (2, 31)]\n", | |
"Differences 1 to 2! 63\n", | |
"[(0, 16), (3, 12), (1, 9)]\n", | |
"[(0, 16), (1, 17), (2, 20)]\n", | |
"Differences 1 to 3! 63\n", | |
"[(0, 16), (3, 12), (1, 9)]\n", | |
"[(0, 43), (1, 35), (2, 28)]\n", | |
"Differences 1 to 2! 67\n", | |
"[(1, 17), (0, 15), (2, 7)]\n", | |
"[(1, 17), (0, 24), (3, 24)]\n", | |
"Differences 1 to 3! 67\n", | |
"[(1, 17), (0, 15), (2, 7)]\n", | |
"[(1, 42), (0, 39), (3, 29)]\n", | |
"Differences 1 to 2! 68\n", | |
"[(1, 18), (2, 14), (3, 9)]\n", | |
"[(1, 18), (0, 18), (2, 26)]\n", | |
"Differences 1 to 3! 68\n", | |
"[(1, 18), (2, 14), (3, 9)]\n", | |
"[(0, 41), (2, 36), (1, 32)]\n", | |
"Differences 1 to 2! 69\n", | |
"[(0, 19), (1, 11), (3, 10)]\n", | |
"[(0, 19), (3, 17), (2, 24)]\n", | |
"Differences 1 to 3! 69\n", | |
"[(0, 19), (1, 11), (3, 10)]\n", | |
"[(0, 46), (2, 38), (1, 30)]\n", | |
"Differences 2 to 3! 69\n", | |
"[(0, 19), (3, 17), (2, 24)]\n", | |
"[(0, 46), (2, 38), (1, 30)]\n", | |
"Differences 1 to 3! 72\n", | |
"[(0, 16), (3, 11), (1, 10)]\n", | |
"[(0, 43), (1, 35), (2, 30)]\n", | |
"Differences 2 to 3! 72\n", | |
"[(0, 16), (1, 21), (3, 23)]\n", | |
"[(0, 43), (1, 35), (2, 30)]\n", | |
"Differences 1 to 2! 77\n", | |
"[(1, 15), (0, 14), (3, 12)]\n", | |
"[(1, 15), (0, 19), (2, 24)]\n", | |
"Differences 1 to 3! 77\n", | |
"[(1, 15), (0, 14), (3, 12)]\n", | |
"[(0, 43), (1, 36), (2, 33)]\n", | |
"Differences 1 to 2! 79\n", | |
"[(0, 15), (1, 11), (3, 10)]\n", | |
"[(0, 15), (1, 19), (2, 21)]\n", | |
"Differences 1 to 3! 79\n", | |
"[(0, 15), (1, 11), (3, 10)]\n", | |
"[(1, 41), (0, 38), (2, 35)]\n", | |
"Differences 1 to 3! 81\n", | |
"[(0, 19), (1, 13), (2, 8)]\n", | |
"[(0, 41), (1, 37), (3, 28)]\n", | |
"Differences 2 to 3! 81\n", | |
"[(0, 19), (1, 18), (2, 22)]\n", | |
"[(0, 41), (1, 37), (3, 28)]\n", | |
"Differences 1 to 2! 84\n", | |
"[(1, 14), (0, 12), (3, 9)]\n", | |
"[(1, 14), (0, 20), (2, 19)]\n", | |
"Differences 1 to 3! 84\n", | |
"[(1, 14), (0, 12), (3, 9)]\n", | |
"[(0, 41), (1, 38), (2, 26)]\n", | |
"Differences 1 to 2! 86\n", | |
"[(0, 17), (1, 13), (2, 11)]\n", | |
"[(0, 17), (1, 19), (3, 24)]\n", | |
"Differences 1 to 3! 86\n", | |
"[(0, 17), (1, 13), (2, 11)]\n", | |
"[(0, 43), (1, 36), (3, 33)]\n", | |
"Differences 1 to 2! 91\n", | |
"[(1, 15), (0, 14), (3, 11)]\n", | |
"[(1, 15), (0, 20), (2, 21)]\n", | |
"Differences 1 to 3! 91\n", | |
"[(1, 15), (0, 14), (3, 11)]\n", | |
"[(1, 42), (0, 37), (2, 30)]\n", | |
"Differences 1 to 3! 93\n", | |
"[(2, 16), (1, 12), (0, 11)]\n", | |
"[(1, 43), (0, 34), (3, 30)]\n", | |
"Differences 2 to 3! 93\n", | |
"[(2, 16), (1, 18), (0, 24)]\n", | |
"[(1, 43), (0, 34), (3, 30)]\n", | |
"Differences 1 to 2! 97\n", | |
"[(0, 18), (1, 12), (3, 9)]\n", | |
"[(0, 18), (1, 18), (2, 23)]\n", | |
"Differences 1 to 3! 97\n", | |
"[(0, 18), (1, 12), (3, 9)]\n", | |
"[(1, 44), (0, 42), (2, 30)]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"(0.23, 0.3, 0.11)" | |
] | |
}, | |
"execution_count": 78, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# this differs from the above in that `nstocount` assumes everyone voted for their 3 referred one\n", | |
"def weighted_votes(Ncand, Nvoters):\n", | |
" votes = []\n", | |
" for i in range(Nvoters):\n", | |
" w = [5, 4, 3, 2, 1]\n", | |
" votes.append(np.random.choice(5, 3, replace=False, p=w/np.sum(w)))\n", | |
" return np.array(votes)\n", | |
"\n", | |
"ntest = 100\n", | |
"diffs12 = diffs23 = diffs13 = 0\n", | |
"\n", | |
"vc = VoteComparison(3, 50, 5)\n", | |
"\n", | |
"v1 = []\n", | |
"v2 = []\n", | |
"v3 = []\n", | |
"for i in range(ntest):\n", | |
" votes = weighted_votes(vc.Ncand, vc.Nvoters)\n", | |
" nstocount = None\n", | |
" v1.append(vc.first_vote_winners(votes))\n", | |
" v2.append(vc.ranked_vote_winners(votes))\n", | |
" v3.append(vc.n_vote_winners(votes, nstocount))\n", | |
" winners1 = [v[0] for v in v1[-1]]\n", | |
" winners2 = [v[0] for v in v2[-1]]\n", | |
" winners3 = [v[0] for v in v3[-1]]\n", | |
" if set(winners1) != set(winners2):\n", | |
" print('Differences 1 to 2!', i)\n", | |
" diffs12 +=1\n", | |
" print(v1[-1])\n", | |
" print(v2[-1])\n", | |
" if set(winners1) != set(winners3):\n", | |
" print('Differences 1 to 3!', i)\n", | |
" diffs13 +=1\n", | |
" print(v1[-1])\n", | |
" print(v3[-1])\n", | |
" if set(winners2) != set(winners3):\n", | |
" print('Differences 2 to 3!', i)\n", | |
" diffs23 +=1\n", | |
" print(v2[-1])\n", | |
" print(v3[-1])\n", | |
" \n", | |
"diffs12/ntest, diffs13/ntest, diffs23/ntest" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Conclusion: the APE0 scheme is close to STV if there's a clear break (everyone prefers the top 3 over the bottom 2, say), but closer to single-vote if it's more even." | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python (astro38)", | |
"language": "python", | |
"name": "astro38" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment