Skip to content

Instantly share code, notes, and snippets.

@acoppock
Created November 19, 2018 20:08
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 acoppock/39175fd72fe96163cb116686ef490450 to your computer and use it in GitHub Desktop.
Save acoppock/39175fd72fe96163cb116686ef490450 to your computer and use it in GitHub Desktop.
rm(list = ls())
library(tidyverse)
fate <-
function(stopping_rule) {
# the order you meet people
the_shuffle = sample(1:100)
# everybody you've met
the_exes <- the_shuffle[1:stopping_rule]
# how good was the best so far
the_bar = max(the_exes)
# everbody who's left to meet
the_potentials <- the_shuffle[stopping_rule:100]
# the first one who's good enough
partner = the_potentials[the_potentials >= the_bar][1]
# sometimes no one's good enough
if(is.na(partner)) {partner <- -99}
return(data.frame(partner = partner))
}
run_fate <- function(stopping_rule, sims = 1000) {
map_df(1:sims, ~ fate(stopping_rule))
}
simulations <- map_df(1:100, ~run_fate(.), .id = "stopping_rule")
gg_df <-
simulations %>%
mutate(stopping_rule = as.numeric(stopping_rule)) %>%
group_by(stopping_rule) %>%
summarize(probability_alone = mean(partner == -99),
how_good_if_not_alone = mean(partner[partner != -99])) %>%
gather(key, value, -stopping_rule)
ggplot(gg_df, aes(stopping_rule, value)) +
geom_point() +
geom_line() +
geom_vline(xintercept = 37, color = "red") +
theme_bw() +
theme(strip.background = element_blank(), axis.title.y = element_blank()) +
facet_wrap(~key, scales = "free")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment