-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimalstopping.py
44 lines (37 loc) · 1.36 KB
/
optimalstopping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'''Tests Optimal Stopping Theory by generating 1000 random numbers between 1 and 100'''
import random
import math
#Set the seed to always generate same set of random numbers
random.seed(10)
choices = []
for i in range(0,1000):
choices.append(random.randint(1,100))
import matplotlib.pyplot as plt
plt.scatter(choices, range(0,1000))
plt.show()
def optimalstop():
#Optimal Stopping Theory says coast without picking until 1/e percent of observations have been observed ~ 37%
thresh = round((1 / math.e)*(len(choices)+1), 0)
observedChoices = []
#Theory says after the 1/e percent threshold is met, pick the next best observation that is better than everything seen so far
#Save observations up until threshold
for j in choices:
if len(observedChoices) < thresh:
observedChoices.append(j)
choices.remove(j)
else:
break
#Look at remaining choices and stop when the best one appears that beats the current best
bestSoFar = max(observedChoices)
best = max(observedChoices)
for k in choices:
if k <= best:
pass
else:
best = k
break
if best == bestSoFar:
print('The best value occurred in the first 37% and no better value was found. :-( ')
else:
print('The best value found after the first 37% was:', best)
optimalstop()