Thursday, November 3, 2011

Monty Hall Simulation

The Monty Hall probability  proof always baffled me. Read more in Wikipedia for the description.

But for those who don't know, and don't want to know the whole deal I'll give the tl;dr friendly version.

Assume that you're presented with 3 boxes (A, B, C) and are asked to choose one. One of them contains a prize. After you've chosen, I'll open an empty box from the other two. Now, you have the option of sticking together with your choice, or, you may change your choice. According to math, your chances of winning are more if you switch.

Let me explain in an easier way, you choose box B. Then I open box C and show that it is empty. Again, I ask you to choose between A and B. Are you better off by choosing A?

Common sense wrongly suggests that switching between A and B makes no difference. (probability remains 1/3rd - wrong intuition). Psychology makes us stay with B (people are reluctant to change). But, probability shows that switching produces a definite boost to our chance of winning - double!

Though I understood the math, I really wanted to verify.

So here is some crappy C simulation.

Let us not switch for 100 times and see how much we win.
Later, we'll switch for 100 times after choosing, and see how much we win.

Code:

#include
#include
#include

int doMontyNoSwitch();
int doMontySwitch();

int main(void)
{
        int choice = 0,i;
        int winCountNS=0,winCountS=0;

        //Seed the randomizer
        srand(time(NULL));

        //100 times without switching
        for(i=0;i<100;i++)
        {
                //Increment win count for no switch if we win
                if(doMontyNoSwitch() == 1) winCountNS++;
        }

        for(i=0;i<100;i++)
        {
                //Increment win count for switch if we win
                if(doMontySwitch() == 1) winCountS++;
        }
        printf("\nNS:%d S:%d",winCountNS,winCountS);

        return 0;
}

//Here we'll switch
int doMontySwitch()
{
        int r1,correct,choice,reveal,switchedChoice,i;
        r1 = rand()%3;
   
        //Let me randomly set the correct box/door
        correct = r1;

        //Choose randomly
        choice = rand()%3;

        for(i=0;i<3;i++)
        {
                //Show the door/box which does not contain the prize and what I didn't choose
                if(i!=correct && i!=choice)
                {
                        reveal = i;
                        break;
                }
        }

        for(i=0;i<3;i++)
        {
                //Reveal the box which was not chosen and the wrong one
                if(i!=choice && i!=reveal)
                {
                        //Change our choice her, that is, "the switch"
                        choice = i;
                        break;
                }
        }

        if(choice == correct)
        {
                //return 1 to indicate we've won here        
                return 1;
        }
        else
        {
                //return 0 to indicate we've lost here
                return 0;
        }
}

//Without switching
int doMontyNoSwitch()
{
        int r1,correct,choice,reveal,switchedChoice,i;
        r1 = rand()%3;

        correct = r1;
        choice = rand()%3;
        for(i=0;i<3;i++)
        {
                if(i!=correct && i!=choice)
                {
                        reveal = i;
                        break;
                }
        }

        if(choice == correct)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

So, when I compile and run this thrice,


NS:31 S:63
NS:32 S:62
NS:25 S:60

The probability is right. So right! Ideally it should have been 33 and 66, but it is so damn close, wow!


So, next time I'm going to switch no matter what!


By the way, the code above is not well designed or thought out, I'll work on it later....

Tuesday, November 1, 2011

Why Indian Railways Online Ticketing is so bad

I found another rant on the state of Indian Railways' online ticket booking website in Hacker News. It is horrible and here I am going to tell you why.

Monopoly

We may have cleartrip, makemytrip, and yatra, but, these services cannot book special tatkal tickets which are open only from 8:00 AM - 9:00 AM three days before the day of travel. For frequent travelers, this is the only type of ticket which matters.

So, even though other sites exists, IRCTC as such is a monopoly.

API Access

For getting the API access, the company needs to have been around for 3 years. All startups banned by default. Nice.

Anyway when you annually make $15M profit by being a monopoly you want the status quo...m and when the status quo is being a monopoly, you don't mind being a wreck...