Skip to content

Instantly share code, notes, and snippets.

@luccabb
Created April 5, 2020 19:16
Show Gist options
  • Save luccabb/9341556c595c32356765b4ed23911a7d to your computer and use it in GitHub Desktop.
Save luccabb/9341556c595c32356765b4ed23911a7d to your computer and use it in GitHub Desktop.
import random
import matplotlib.pyplot as plt
import math
def askingNewAssistant():
# getting new assistant ability
return random.random()
def hireAssistant(n):
# hire assistant for n applications
best = -1
hired =0
for a in range(n):
new_assistant = askingNewAssistant()
# if new assistant has more ability than the previous we hire it
if new_assistant > best:
best = new_assistant
hired+=1
# returning number of hired + number of applicants
return hired, n
hire1 = hireAssistant(100)
hire2 = hireAssistant(10)
hire3 = hireAssistant(5)
# tests
assert hire1[0] <= hire1[1]
assert hire2[0] <= hire2[1]
assert hire3[0] <= hire3[1]
def probabilityOne(n, numberApplicants):
# probability of only hiring one
one = 0
for a in range(n):
if hireAssistant(numberApplicants)[0] == 1:
one+=1
return one/n
def produceArray(starting, end):
applicants = []
hires = []
for a in range(starting,end):
app = hireAssistant(a)
applicants.append(app[1])
hires.append(app[0])
return hires, applicants
def produceProbOne(starting, end):
applicants = []
percentage = []
for a in range(starting, end):
app = probabilityOne(1000, a)
applicants.append(a)
percentage.append(app)
return percentage, applicants
def produceMeanArray(starting, end, n):
# producing the mean hire/number of applicant array to be graphed
applicants = []
hires = []
mean_hires = []
# range of number of applicants that we want to run
for a in range(starting,end):
mean_hires = []
# getting the average number of hired based on the output of
# running hireAssistant 'n' times
for b in range(n):
app = hireAssistant(a)
mean_hires.append(app[0])
# calculating the averages
hires.append(sum(mean_hires)/len(mean_hires))
applicants.append(a)
return hires, applicants
# graphing number of hired X applicants
graph = produceArray(1,1000)
graph1 = produceMeanArray(1,1000,1000)
plt.plot(graph[1], graph[0], 'ro')
plt.plot(graph1[1], graph1[0])
# graphing the theoretical line (ln n)
plt.plot([a for a in range(1,1000)], [math.log2(a) for a in range(1,1000)])
plt.legend(['Individual points', 'Mean of points', 'theoretical line'])
plt.title('Applicants X Hires')
plt.xlabel('Applicants')
plt.ylabel('Hired')
plt.show()
#graphing the probability of only hiring one applicant
graph = produceProbOne(1,100)
plt.plot(graph[1], graph[0])
plt.title('Applicants X Probability of one applicant being hired')
plt.xlabel('Number of Applicants')
plt.ylabel('Probability of only one applicant being hired')
plt.show()
@luccabb
Copy link
Author

luccabb commented Apr 5, 2020

image
image

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