Saturday, November 14, 2015

Monty Hall problem simulation within Kdb+ q

Monty Hall problem is one of the paradox it would easily make you puzzled. If you are one of these puzzled one and still wondering how you would have 0.66 chance when you change your mind after a door is opened, following simulation in KDB+ q may help to persuade you:
mh:{[N]
        d:{.[000b;(1?3);:;1b]} each  til N; // create simulation with randomly setting car positions
        s:N?3; // choice of player
        i:(til N;s);  // index
        changed: sum not d @' s; // let see how many wins if player changed his mind
        :changed%N   // ratio  
  
 }

q)mh[100000]
0.66424