Skip to content

Instantly share code, notes, and snippets.

@glesica
Last active August 29, 2015 13:57
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 glesica/9353004 to your computer and use it in GitHub Desktop.
Save glesica/9353004 to your computer and use it in GitHub Desktop.
Simulation demonstrating the characteristics of the Secretary Problem (https://en.wikipedia.org/wiki/Secretary_problem) using Julia (http://julialang.org/).
# Do K simulation runs each with N items. The goal is to find the largest
# item in the list, without "remembering" and carrying along some previous
# maximum.
N = 10000
K = 1000
diffs = zeros(Int, K)
pct_diffs = zeros(Float64, K)
for i = 1:K
scores = randn(N) * 100 |> int |> abs
max_score = maximum(scores)
drop_count = N / e |> round |> int
max_dropped = maximum(scores[1:drop_count])
max_chosen = minimum(scores)
for v = scores[drop_count+1:end]
max_chosen = v
if v > max_dropped
break
end
end
diffs[i] = max_score - max_chosen
pct_diffs[i] = (max_score - max_chosen) / max_score
end
pct_correct = count(x -> x == 0, diffs) / K
mean_error = mean(pct_diffs)
println("% correct: ", pct_correct)
println("mean % error: ", mean_error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment