Last active
December 24, 2015 03:59
-
-
Save phimuemue/6740722 to your computer and use it in GitHub Desktop.
A hiring problem
This file contains hidden or 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
| from random import random | |
| import operator | |
| def E_M(m): | |
| """Computes E(max(X_1,...,X_m)), where X_i ~ Uni(0,1)""" | |
| return float(m)/(m+1) | |
| def hire1(l): | |
| """Scans list l from left to right and hires i if l[i] is greater than | |
| E(max(l[i+1],...,l[-1]))""" | |
| remaining = len(l) | |
| for i in xrange(len(l)): | |
| remaining = remaining - 1 | |
| if l[i] > E_M(remaining): | |
| return i | |
| return i | |
| def S1(m): | |
| def prod(i): | |
| return reduce(operator.mul, [(float(m-j))/(m-j+1) for j in xrange(1,i)], 1) | |
| def binom(i): | |
| return 0.5 * (1-(float(m-i)/(m-i+1))**2) | |
| return sum([binom(i)*prod(i) for i in xrange(1, m+1)]) | |
| N=20 | |
| # compute expectancy | |
| print S1(N) | |
| # simulate | |
| RUNS = 1000 | |
| total = 0 | |
| maxes = 0 | |
| for r in xrange(RUNS): | |
| l = [random() for _ in range(N)] | |
| h = hire1(l) | |
| total = total + l[h] | |
| if l[h] == max(l): | |
| maxes = maxes + 1 | |
| total = total / RUNS | |
| maxes = float(maxes) / RUNS | |
| print total, maxes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment