public
Created

Birthday Paradox ("Surprise") in K (Kona)

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/ 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]
 
 

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.