Last active
May 4, 2022 13:04
-
-
Save DominikPeters/e9a6272d7f761915338dc26fa2d4d4cd to your computer and use it in GitHub Desktop.
Sample from a Mallows distribution
This file contains hidden or 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
# 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