Last active
July 10, 2017 11:13
-
-
Save lou1306/1041ed6cd4eed433cfabf45f666bf298 to your computer and use it in GitHub Desktop.
People exchanging money with random others
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
from random import random | |
import matplotlib.pyplot as plt | |
PLAYERS = 100 | |
WEALTH = 100 | |
ROUNDS = 5000 | |
MAX_PERCENT = 20 | |
def myrandrange(start, stop): | |
"""Quick (and probably dirty) randrange() implementation""" | |
return int((stop - start) * random() + start) | |
def pick_another_player(n): | |
out = n | |
while out == n: | |
out = myrandrange(0, PLAYERS) | |
return out | |
def gini(l): | |
"""Gini coefficient of list l""" | |
n = len(l) | |
out = -2 * sum((n + 1 - i) * y for i, y in enumerate(sorted(l))) | |
out /= sum(l) | |
out += n + 1 | |
out /= n | |
return out | |
gini_list = [] | |
for PERCENT in range(MAX_PERCENT): | |
bank = [WEALTH] * PLAYERS | |
for i in range(ROUNDS): | |
# Players that have some money | |
non_broke = (i for i in range(PLAYERS) if bank[i] > 0) | |
for player in non_broke: | |
wealth = bank[player] | |
max_pay = max(1, wealth * PERCENT // 100) | |
target = pick_another_player(player) | |
payment = myrandrange(1, max_pay + 1) | |
# Do the payment | |
bank[player] -= payment | |
bank[target] += payment | |
g = gini(bank) | |
print(PERCENT, g) | |
gini_list.append(g) | |
# Draw/update plot | |
plt.clf() | |
# fig = plt.gcf() | |
title = "\$1" if PERCENT == 0 else \ | |
"{}% of wealth".format(PERCENT) | |
plt.title( | |
"Wealth distribution \ | |
\n Random payments from \$1 to {} \ | |
\n Gini coefficient: {:f}".format(title, g)) | |
sorted_bank = sorted(bank) | |
plt.bar(range(PLAYERS), sorted_bank, 1) | |
plt.show(block=False) | |
plt.pause(1e-6) | |
min_val = min(gini_list) | |
max_val = max(gini_list) | |
max_keys = [i for i in range(len(gini_list)) if gini_list[i] == max_val] | |
min_keys = [i for i in range(len(gini_list)) if gini_list[i] == min_val] | |
print("Worst Gini coefficient: {} at {}%".format(min_val, min_keys)) | |
print("Best Gini coefficient: {} at {}%".format(max_val, max_keys)) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Random exchange of money
Starting from this article, I wrote a generalized version.
At each round, each player gives away m dollars: m is picked uniformly at random and goes from $1 to a fixed percentage of the player's wealth.
At the end, the scripts computes the Gini coefficient of the resulting wealth distribution.
Sample run
Results
When the percentage is 0, people always pay $1 to each other. As seen in the original article, the resulting wealth distribution is surprisingly unbalanced. We see that the inequality is greatly reduced when players are willing to pay a small percentage of their wealth to each other (roughly, between 2 and 10%); higher percentages lead to a growing trend for the coefficient.