Skip to content

Instantly share code, notes, and snippets.

@hakank
Created April 27, 2011 18:28
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 hakank/944856 to your computer and use it in GitHub Desktop.
Save hakank/944856 to your computer and use it in GitHub Desktop.
Birthday Paradox ("Surprise") in K (Kona)
/ This is a simulation of the "Birthday paradox"
/ (what I call "Birthday surprise")
/ See http://en.wikipedia.org/wiki/Birthday_problem
/ Take 20 samples of 0..364 with replacement
/ This represents the birthday of 20 persons.
20 ? 365
/ sample result
/ 195 361 24 274 145 108 157 259 51 228 330 88 180 228 86 197 121 182 336 133
/ count how many unique values there are
/ ? here means the unique values of a vector
/ # here means the size
#?20?365
/ sample result: 19
/ check how many non unique values there are
{20-(#?20 ? 365)}
/ sample result: 1
/ Now run this 10 times.
/ The phrase
/ {...}' !10
/ means that we run the function in {}
/ 10 times (for 0..9)
{20-(#?20 ? 365)}' !10
/ sample result:
/ 0 0 1 0 0 0 1 1 0 0
/ Here we check how many times
/ there is at least one double (triple, etc),
/ and run for 10000 simulations.
/ 20 person
{+/x % #x}({(20-(#?20 ? 365))>0}' !10000)
/ sample result: 0.4147
/ 23 persons
{+/x % #x}({(23-(#?23 ? 365))>0}' !10000)
/ sample result: 0.5093
/ Later comment:
/ Scott Vokes (silentbicycle) suggested this more
/ direct approach (at twitter)
/ """
/ About your birthday surprise K pg., you can also say
/ 10 f/init
/ for "run f 10x, starting w/ init"
/ or
/ 10 f\init
/ to save intermed. vals
/ """
/ Here's a simulation for 20 people and 1000 trials
bsup:{~x=#?x?365}
par:{(+/x {bsup[20]}\1)%x}
par[1000]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment