Skip to content

Instantly share code, notes, and snippets.

@lou1306
Last active July 10, 2017 11:13
Show Gist options
  • Save lou1306/1041ed6cd4eed433cfabf45f666bf298 to your computer and use it in GitHub Desktop.
Save lou1306/1041ed6cd4eed433cfabf45f666bf298 to your computer and use it in GitHub Desktop.
People exchanging money with random others
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()
@lou1306
Copy link
Author

lou1306 commented Jul 9, 2017

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

0 0.321858
1 0.3103660000000001
2 0.007481999999999971
3 0.03608599999999996
4 0.05310000000000002
5 0.06647400000000005
6 0.07476399999999998
7 0.06949399999999997
8 0.07878799999999998
9 0.08888000000000006
10 0.09867000000000005
11 0.09959800000000002
12 0.11010599999999997
13 0.12638999999999995
14 0.11386799999999994
15 0.11099599999999996
16 0.12308400000000005
17 0.14093999999999995
18 0.17063599999999993
19 0.14138000000000006

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment