Created
October 24, 2019 18:17
-
-
Save SonOfLilit/a172bbdb50560c79f42eca8109896c93 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
""" | |
Implementation of https://arxiv.org/pdf/1802.07068.pdf by Aur Saraf | |
""" | |
import random | |
def histogram(items): | |
counts = {} | |
for item in items: | |
if item not in counts: | |
counts[item] = 0 | |
counts[item] += 1 | |
return counts | |
def by_key(hist): | |
if None in hist: | |
hist[0] = hist[None] | |
del hist[None] | |
total = sum(hist.values()) | |
for key in sorted(hist): | |
print(f'{key} {hist[key]} {hist[key] / total:.0%}') | |
event_likelihood = 2 * 500 / (100 * 100) # they say 500 on 201 by 201, but also event diameter 2, and there are 2 kinds | |
p_l = .5 | |
good_likelihood = event_likelihood * p_l | |
bad_likelihood = event_likelihood * (1 - p_l) | |
iterations = 80 | |
for P in [.6, .7, .2, .9]: | |
N = 1000 | |
wealth = [10] * N | |
talent = [random.gauss(P, .1) for _ in wealth] | |
good_luck = [0] * N | |
bad_luck = [0] * N | |
for i in range(iterations): | |
for j in range(N): | |
if good_likelihood >= random.random(): | |
if random.random() <= talent[j]: | |
wealth[j] *= 2 | |
good_luck[j] += 1 | |
if bad_likelihood >= random.random(): | |
wealth[j] /= 2 | |
bad_luck[j] += 1 | |
print('***', P) | |
#by_key(histogram(wealth)) | |
sorted_wealth = list(wealth) | |
sorted_wealth.sort() | |
total = sum(sorted_wealth) | |
rich = sum(sorted_wealth[800:]) | |
print(rich, total, f'{rich / total:.0%}') | |
import numpy as np | |
from sklearn.linear_model import LinearRegression | |
from sklearn.preprocessing import normalize | |
params = np.array([talent, good_luck, bad_luck]) | |
params = normalize(params, axis=1, norm='l2') | |
params = params.transpose() | |
reg = LinearRegression().fit(params, wealth) | |
print(reg.coef_, reg.intercept_, reg.score(params, wealth)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment