Skip to content

Instantly share code, notes, and snippets.

@iggisv9t
Created September 22, 2021 10:03
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 iggisv9t/50744dfeff2eb338b74f6566b852ee23 to your computer and use it in GitHub Desktop.
Save iggisv9t/50744dfeff2eb338b74f6566b852ee23 to your computer and use it in GitHub Desktop.
Partymaker
def one_run(max_size, max_iters, p, q, disable=False):
max_idea = 0
run_times = []
for size in tqdm(range(max_size), disable=disable):
ideas = [None for i in range(size)] + [None]
condition = True
run_time = 0
while condition:
run_time += 1
new_ideas = [a for a in ideas]
for i, idea in enumerate(ideas):
action = np.random.choice(['accept', 'decline', 'new'], p=[p, q, (1 - p - q)])
if action == 'accept':
new_ideas[i] = np.random.choice(ideas)
if action == 'decline':
new_ideas[i] = ideas[i]
if action == 'new':
max_idea += 1
new_ideas[i] = max_idea
ideas = new_ideas
condition = (not ((len(set(ideas)) == 1) and (not (ideas[0] is None)))) and (run_time < max_iters)
run_times.append(run_time)
return run_times
### Sample
NRUNS = 10
results = []
for p in tqdm(np.linspace(0.05, 0.95, 20)):
for q in np.linspace(0, 0.95, 20):
if p + q < 1:
for _ in range(NRUNS):
results.append([p, q] + one_run(50, 800, p, q, disable=True))
# Make DataFrame
results_df = pd.DataFrame(results, columns=['p', 'q'] + ['size_{}'.format(i) for i in range(50)])
grouped = results_df.groupby(['p', 'q']).mean().reset_index()
# Draw Results
col = 'size_10'
plt.figure(figsize=(10, 10))
plt.scatter(grouped['p'].values, grouped['q'].values,
grouped[col].values, marker='.',
c=grouped[col].values,
cmap='rainbow');
plt.title(col.replace('_', ': '))
plt.xlabel('Acceptance Rate')
plt.ylabel('Rejection Rate')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment