Skip to content

Instantly share code, notes, and snippets.

@DominikPeters
Last active May 4, 2022 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DominikPeters/e9a6272d7f761915338dc26fa2d4d4cd to your computer and use it in GitHub Desktop.
Save DominikPeters/e9a6272d7f761915338dc26fa2d4d4cd to your computer and use it in GitHub Desktop.
Sample from a Mallows distribution
# Adapted from https://github.com/martinlackner/abcvoting/blob/6aacadb0e3e70e4f83a82225b05730f72a6230d4/abcvoting/generate.py#L166
def mallows(reference_ranking, dispersion):
distributions = []
for i in range(len(reference_ranking)):
distributions.append([dispersion ** (i - j) for j in range(i + 1)])
while True:
vote = []
for i in range(len(reference_ranking)):
pos = random.choices(range(i + 1), weights=distributions[i], k=1)[0]
vote.insert(pos, reference_ranking[i])
yield vote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment