Skip to content

Instantly share code, notes, and snippets.

@3catz
Created February 25, 2021 23:42
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 3catz/82455129b3a1776e5d5e9a7c15a0eb4b to your computer and use it in GitHub Desktop.
Save 3catz/82455129b3a1776e5d5e9a7c15a0eb4b to your computer and use it in GitHub Desktop.
Secretary Problem simulation
n_trials = 10000
n_candidates = 100
max_burn = int(0.66 * n_candidates)
for T in range(1, max_burn, 5):
global_maxes = []
diff_from_max = []
time_taken = []
final_choices = []
for i in range(n_trials):
candidates = np.random.poisson(lam = 100, size = n_candidates)
#candidates = weib.random_samples(100).round()
np.random.shuffle(candidates)
global_max = np.max(candidates)
global_maxes.append(global_max)
pool = candidates[:T]
remainder = candidates[T:]
pool_max = np.max(pool)
thresh = np.quantile(pool, q = 1.0)
for i in range(len(remainder)):
#print(i)
if remainder[i] > thresh:
time_taken.append(T + i)
diff = global_max - remainder[i]
diff_from_max.append(diff)
final_choices.append(remainder[i])
break
print("Total Candidates:", n_candidates, "|| Burn in Period = ", T, "|| Trials = ", n_trials)
print("Average Global Max = {:.2f}".format(np.mean(global_maxes)))
print("Average Choice = {:.2f}".format(np.mean(final_choices)))
print("Average diff from max {:.2f}".format(np.mean(diff_from_max)), "| Average time taken {:.2f}".format(np.mean(time_taken))), #"| Efficiency {:.2f}".format(np.mean(time_taken/np.mean(diff_from_max))))
print("------------------------------------------------------------------------------------------------------")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment